2023-09-29 12:29:22 +00:00
|
|
|
'use client';
|
|
|
|
|
import { Button, Icon, Icons, Loading, Text } from 'react-basics';
|
2023-01-28 05:53:13 +00:00
|
|
|
import Link from 'next/link';
|
2022-03-04 03:45:49 +00:00
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
2023-08-30 22:23:08 +00:00
|
|
|
import Pager from 'components/common/Pager';
|
2023-10-03 23:05:17 +00:00
|
|
|
import WebsiteChartList from '../../(main)/websites/[id]/WebsiteChartList';
|
|
|
|
|
import DashboardSettingsButton from 'app/(main)/dashboard/DashboardSettingsButton';
|
|
|
|
|
import DashboardEdit from 'app/(main)/dashboard/DashboardEdit';
|
2023-01-28 05:53:13 +00:00
|
|
|
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
|
2023-08-21 09:06:09 +00:00
|
|
|
import useApi from 'components/hooks/useApi';
|
2022-08-04 10:56:30 +00:00
|
|
|
import useDashboard from 'store/dashboard';
|
2023-08-21 09:06:09 +00:00
|
|
|
import useMessages from 'components/hooks/useMessages';
|
|
|
|
|
import useLocale from 'components/hooks/useLocale';
|
2023-12-03 11:07:03 +00:00
|
|
|
import useFilterQuery from 'components/hooks/useFilterQuery';
|
2023-12-04 04:15:02 +00:00
|
|
|
import { useUser } from 'components/hooks';
|
2022-03-04 03:45:49 +00:00
|
|
|
|
2023-08-10 20:26:33 +00:00
|
|
|
export function Dashboard() {
|
2023-03-22 21:05:55 +00:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
2023-12-04 04:15:02 +00:00
|
|
|
const { user } = useUser();
|
2023-08-30 22:23:08 +00:00
|
|
|
const { showCharts, editing } = useDashboard();
|
|
|
|
|
const { dir } = useLocale();
|
2023-12-03 11:07:03 +00:00
|
|
|
const { get } = useApi();
|
2023-08-30 22:23:08 +00:00
|
|
|
const pageSize = 10;
|
2023-12-03 11:07:03 +00:00
|
|
|
|
|
|
|
|
const { query, params, setParams, result } = useFilterQuery({
|
|
|
|
|
queryKey: ['dashboard:websites'],
|
|
|
|
|
queryFn: (params: any) => {
|
2023-12-04 04:15:02 +00:00
|
|
|
return get(`/users/${user.id}/websites`, { ...params, includeTeams: true, pageSize });
|
2023-12-03 11:07:03 +00:00
|
|
|
},
|
2023-12-02 04:27:59 +00:00
|
|
|
});
|
2023-12-03 11:07:03 +00:00
|
|
|
|
|
|
|
|
const handlePageChange = (page: number) => {
|
|
|
|
|
setParams({ ...params, page });
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-30 22:23:08 +00:00
|
|
|
const { data, count } = result || {};
|
2023-12-03 11:07:03 +00:00
|
|
|
const hasData = !!(data as any)?.length;
|
|
|
|
|
const { page } = params;
|
2022-03-04 03:45:49 +00:00
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
if (query.isLoading) {
|
2023-12-06 03:22:14 +00:00
|
|
|
return <Loading />;
|
2023-09-29 12:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 03:45:49 +00:00
|
|
|
return (
|
2023-09-29 12:29:22 +00:00
|
|
|
<>
|
2023-01-28 05:53:13 +00:00
|
|
|
<PageHeader title={formatMessage(labels.dashboard)}>
|
|
|
|
|
{!editing && hasData && <DashboardSettingsButton />}
|
2022-03-04 03:45:49 +00:00
|
|
|
</PageHeader>
|
2023-01-28 05:53:13 +00:00
|
|
|
{!hasData && (
|
2023-04-10 03:22:28 +00:00
|
|
|
<EmptyPlaceholder message={formatMessage(messages.noWebsitesConfigured)}>
|
2023-01-28 05:53:13 +00:00
|
|
|
<Link href="/settings/websites">
|
|
|
|
|
<Button>
|
2023-04-20 09:41:06 +00:00
|
|
|
<Icon rotate={dir === 'rtl' ? 180 : 0}>
|
2023-01-28 05:53:13 +00:00
|
|
|
<Icons.ArrowRight />
|
|
|
|
|
</Icon>
|
|
|
|
|
<Text>{formatMessage(messages.goToSettings)}</Text>
|
|
|
|
|
</Button>
|
|
|
|
|
</Link>
|
|
|
|
|
</EmptyPlaceholder>
|
|
|
|
|
)}
|
|
|
|
|
{hasData && (
|
|
|
|
|
<>
|
2023-08-30 22:23:08 +00:00
|
|
|
{editing && <DashboardEdit />}
|
2023-08-10 20:26:33 +00:00
|
|
|
{!editing && (
|
2023-08-30 22:23:08 +00:00
|
|
|
<>
|
|
|
|
|
<WebsiteChartList websites={data} showCharts={showCharts} limit={pageSize} />
|
|
|
|
|
<Pager
|
|
|
|
|
page={page}
|
|
|
|
|
pageSize={pageSize}
|
|
|
|
|
count={count}
|
|
|
|
|
onPageChange={handlePageChange}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2023-01-28 05:53:13 +00:00
|
|
|
)}
|
|
|
|
|
</>
|
2022-03-04 03:45:49 +00:00
|
|
|
)}
|
2023-09-29 12:29:22 +00:00
|
|
|
</>
|
2022-03-04 03:45:49 +00:00
|
|
|
);
|
|
|
|
|
}
|
2023-04-21 15:00:42 +00:00
|
|
|
|
|
|
|
|
export default Dashboard;
|