2023-12-17 19:08:11 +08:00
|
|
|
import { Button } from 'antd';
|
2023-12-16 14:43:17 +08:00
|
|
|
import React, { useState } from 'react';
|
2023-12-16 14:08:20 +08:00
|
|
|
import { trpc } from '../../../api/trpc';
|
|
|
|
import { useAllowEdit } from './useAllowEdit';
|
2023-12-16 14:43:17 +08:00
|
|
|
import {
|
|
|
|
MonitorStatusPageEditForm,
|
|
|
|
MonitorStatusPageEditFormValues,
|
|
|
|
} from './EditForm';
|
|
|
|
import clsx from 'clsx';
|
|
|
|
import { useRequest } from '../../../hooks/useRequest';
|
|
|
|
import { useNavigate } from 'react-router';
|
2023-12-16 14:51:15 +08:00
|
|
|
import { ColorSchemeSwitcher } from '../../ColorSchemeSwitcher';
|
2023-12-17 19:08:11 +08:00
|
|
|
import { StatusPageServices } from './Services';
|
2024-02-12 00:10:38 +08:00
|
|
|
import { useTranslation } from '@i18next-toolkit/react';
|
2023-12-16 14:08:20 +08:00
|
|
|
|
|
|
|
interface MonitorStatusPageProps {
|
|
|
|
slug: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MonitorStatusPage: React.FC<MonitorStatusPageProps> = React.memo(
|
|
|
|
(props) => {
|
2024-02-12 00:10:38 +08:00
|
|
|
const { t } = useTranslation();
|
2023-12-16 14:08:20 +08:00
|
|
|
const { slug } = props;
|
|
|
|
|
|
|
|
const { data: info } = trpc.monitor.getPageInfo.useQuery({
|
|
|
|
slug,
|
|
|
|
});
|
2023-12-16 14:43:17 +08:00
|
|
|
const editPageMutation = trpc.monitor.editPage.useMutation();
|
|
|
|
const trpcUtils = trpc.useContext();
|
|
|
|
const navigate = useNavigate();
|
2023-12-16 14:08:20 +08:00
|
|
|
|
|
|
|
const allowEdit = useAllowEdit(info?.workspaceId);
|
2023-12-18 00:37:15 +08:00
|
|
|
const [editMode, setEditMode] = useState(() => {
|
|
|
|
return new URLSearchParams(window.location.search).has('edit');
|
|
|
|
});
|
2023-12-16 14:08:20 +08:00
|
|
|
|
|
|
|
const monitorList = info?.monitorList ?? [];
|
|
|
|
|
2023-12-16 14:43:17 +08:00
|
|
|
const [{ loading }, handleSave] = useRequest(
|
|
|
|
async (values: MonitorStatusPageEditFormValues) => {
|
|
|
|
if (!info) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newPageInfo = await editPageMutation.mutateAsync({
|
|
|
|
id: info.id,
|
|
|
|
workspaceId: info.workspaceId,
|
|
|
|
...values,
|
|
|
|
});
|
|
|
|
|
|
|
|
trpcUtils.monitor.getPageInfo.setData(
|
|
|
|
{
|
|
|
|
slug,
|
|
|
|
},
|
|
|
|
newPageInfo
|
|
|
|
);
|
|
|
|
setEditMode(false);
|
|
|
|
|
|
|
|
if (info.slug !== newPageInfo.slug) {
|
|
|
|
// if slug is changed, should to navigate to new url
|
|
|
|
navigate(`/status/${newPageInfo.slug}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-12-16 14:08:20 +08:00
|
|
|
return (
|
2024-03-23 00:45:21 +08:00
|
|
|
<div className="flex h-full w-full">
|
2023-12-16 14:43:17 +08:00
|
|
|
{editMode && (
|
2024-03-23 00:45:21 +08:00
|
|
|
<div className="w-1/3 overflow-auto border-r border-gray-300 px-4 py-8 dark:border-gray-600">
|
2023-12-16 14:43:17 +08:00
|
|
|
<MonitorStatusPageEditForm
|
|
|
|
isLoading={loading}
|
|
|
|
initialValues={info ?? {}}
|
|
|
|
onFinish={handleSave}
|
2023-12-16 14:51:15 +08:00
|
|
|
onCancel={() => setEditMode(false)}
|
2023-12-16 14:43:17 +08:00
|
|
|
/>
|
2023-12-16 14:08:20 +08:00
|
|
|
</div>
|
|
|
|
)}
|
2023-12-16 14:43:17 +08:00
|
|
|
<div
|
|
|
|
className={clsx(
|
2024-03-23 00:45:21 +08:00
|
|
|
'mx-auto overflow-auto px-4 py-8',
|
2023-12-16 14:43:17 +08:00
|
|
|
!editMode ? 'w-4/5' : 'w-2/3'
|
|
|
|
)}
|
|
|
|
>
|
2023-12-16 14:51:15 +08:00
|
|
|
<div className="flex">
|
2024-03-23 00:45:21 +08:00
|
|
|
<div className="mb-4 flex-1 text-2xl">{info?.title}</div>
|
2023-12-16 14:51:15 +08:00
|
|
|
|
|
|
|
<ColorSchemeSwitcher />
|
|
|
|
</div>
|
2023-12-16 14:43:17 +08:00
|
|
|
|
2023-12-17 18:46:07 +08:00
|
|
|
{allowEdit && !editMode && (
|
2023-12-17 19:27:32 +08:00
|
|
|
<div className="mb-4 flex gap-2">
|
2023-12-16 14:43:17 +08:00
|
|
|
<Button type="primary" onClick={() => setEditMode(true)}>
|
2024-02-12 00:10:38 +08:00
|
|
|
{t('Edit')}
|
2023-12-16 14:43:17 +08:00
|
|
|
</Button>
|
2023-12-17 19:27:32 +08:00
|
|
|
|
|
|
|
<Button type="default" onClick={() => navigate(`/`)}>
|
2024-02-12 00:10:38 +08:00
|
|
|
{t('Back to Admin')}
|
2023-12-17 19:27:32 +08:00
|
|
|
</Button>
|
2023-12-17 18:46:07 +08:00
|
|
|
</div>
|
|
|
|
)}
|
2023-12-16 14:43:17 +08:00
|
|
|
|
2024-03-23 00:45:21 +08:00
|
|
|
<div className="mb-2 text-lg">{t('Services')}</div>
|
2023-12-16 14:43:17 +08:00
|
|
|
|
|
|
|
{info && (
|
2023-12-17 19:08:11 +08:00
|
|
|
<StatusPageServices
|
|
|
|
workspaceId={info.workspaceId}
|
|
|
|
monitorList={monitorList}
|
|
|
|
/>
|
2023-12-16 14:43:17 +08:00
|
|
|
)}
|
|
|
|
</div>
|
2023-12-16 14:08:20 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
MonitorStatusPage.displayName = 'MonitorStatusPage';
|