2023-11-12 15:49:02 +00:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { DashboardGrid } from './Grid';
|
|
|
|
import { DashboardItemAddButton } from './AddButton';
|
|
|
|
import { defaultBlankLayouts, useDashboardStore } from '../../store/dashboard';
|
|
|
|
import { useEvent } from '../../hooks/useEvent';
|
|
|
|
import { Layouts } from 'react-grid-layout';
|
|
|
|
import { Button, Empty, message } from 'antd';
|
|
|
|
import { DateFilter } from '../DateFilter';
|
|
|
|
import { trpc } from '../../api/trpc';
|
|
|
|
import { useCurrentWorkspace, useCurrentWorkspaceId } from '../../store/user';
|
2023-11-14 16:35:13 +00:00
|
|
|
import clsx from 'clsx';
|
2024-02-11 16:10:38 +00:00
|
|
|
import { useTranslation } from '@i18next-toolkit/react';
|
2023-11-12 15:49:02 +00:00
|
|
|
|
|
|
|
export const Dashboard: React.FC = React.memo(() => {
|
2024-02-11 16:10:38 +00:00
|
|
|
const { t } = useTranslation();
|
2023-11-12 15:49:02 +00:00
|
|
|
const { isEditMode, switchEditMode, layouts, items } = useDashboardStore();
|
|
|
|
const mutation = trpc.workspace.saveDashboardLayout.useMutation();
|
|
|
|
const workspaceId = useCurrentWorkspaceId();
|
|
|
|
const workspace = useCurrentWorkspace();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Init on mount
|
|
|
|
const { items = [], layouts = defaultBlankLayouts } =
|
|
|
|
workspace.dashboardLayout ?? {};
|
|
|
|
|
|
|
|
useDashboardStore.setState({
|
|
|
|
items,
|
|
|
|
layouts,
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleChangeLayouts = useEvent((layouts: Layouts) => {
|
|
|
|
useDashboardStore.setState({
|
|
|
|
layouts,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleSaveDashboardLayout = useEvent(async () => {
|
|
|
|
await mutation.mutateAsync({
|
|
|
|
workspaceId,
|
|
|
|
dashboardLayout: {
|
|
|
|
layouts,
|
|
|
|
items,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
switchEditMode();
|
2024-02-11 16:10:38 +00:00
|
|
|
message.success(t('Layout saved success'));
|
2023-11-12 15:49:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="py-4">
|
2023-11-14 16:35:13 +00:00
|
|
|
<div
|
|
|
|
className={clsx(
|
2023-12-08 13:49:49 +00:00
|
|
|
'flex gap-2 justify-end bg-white dark:bg-gray-900 py-2',
|
2023-11-14 16:35:13 +00:00
|
|
|
isEditMode && 'sticky top-0 z-10'
|
|
|
|
)}
|
|
|
|
>
|
2023-11-12 15:49:02 +00:00
|
|
|
{isEditMode ? (
|
|
|
|
<>
|
|
|
|
<DashboardItemAddButton />
|
|
|
|
<Button
|
|
|
|
className="w-32"
|
|
|
|
size="large"
|
|
|
|
loading={mutation.isLoading}
|
|
|
|
disabled={mutation.isLoading}
|
|
|
|
onClick={handleSaveDashboardLayout}
|
|
|
|
>
|
2024-02-11 16:10:38 +00:00
|
|
|
{t('Done')}
|
2023-11-12 15:49:02 +00:00
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<DateFilter />
|
|
|
|
<Button
|
|
|
|
className="w-32"
|
|
|
|
type="primary"
|
|
|
|
size="large"
|
|
|
|
onClick={switchEditMode}
|
|
|
|
>
|
2024-02-11 16:10:38 +00:00
|
|
|
{t('Edit')}
|
2023-11-12 15:49:02 +00:00
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<DashboardGrid
|
|
|
|
layouts={layouts}
|
|
|
|
onChangeLayouts={handleChangeLayouts}
|
|
|
|
items={items}
|
|
|
|
isEditMode={isEditMode}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{items.length === 0 && (
|
2024-02-11 16:10:38 +00:00
|
|
|
<Empty
|
|
|
|
description={t(
|
|
|
|
'You have not dashboard item yet, please enter edit mode and add you item.'
|
|
|
|
)}
|
|
|
|
/>
|
2023-11-12 15:49:02 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
Dashboard.displayName = 'Dashboard';
|