umami/components/pages/dashboard/Dashboard.js

67 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-03-04 04:45:49 +01:00
import { useState } from 'react';
import { Button, Icon, Icons, Text, Flexbox } from 'react-basics';
import Link from 'next/link';
2022-03-04 04:45:49 +01:00
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
2023-01-10 08:59:26 +01:00
import WebsiteChartList from 'components/pages/websites/WebsiteChartList';
import DashboardSettingsButton from 'components/pages/dashboard/DashboardSettingsButton';
2023-01-11 23:47:38 +01:00
import DashboardEdit from 'components/pages/dashboard/DashboardEdit';
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
import useApi from 'hooks/useApi';
2022-08-04 12:56:30 +02:00
import useDashboard from 'store/dashboard';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2023-04-20 11:41:06 +02:00
import useLocale from 'hooks/useLocale';
2022-03-04 04:45:49 +01:00
2023-04-21 17:00:42 +02:00
export function Dashboard({ userId }) {
2023-03-22 22:05:55 +01:00
const { formatMessage, labels, messages } = useMessages();
2022-08-04 12:56:30 +02:00
const dashboard = useDashboard();
const { showCharts, limit, editing } = dashboard;
2022-03-04 04:45:49 +01:00
const [max, setMax] = useState(limit);
const { get, useQuery } = useApi();
const { data, isLoading, error } = useQuery(['websites'], () => get('/websites', { userId }));
const hasData = data && data.length !== 0;
2023-04-20 11:41:06 +02:00
const { dir } = useLocale();
2022-03-04 04:45:49 +01:00
function handleMore() {
setMax(max + limit);
}
return (
<Page loading={isLoading} error={error}>
<PageHeader title={formatMessage(labels.dashboard)}>
{!editing && hasData && <DashboardSettingsButton />}
2022-03-04 04:45:49 +01:00
</PageHeader>
{!hasData && (
2023-04-10 05:22:28 +02:00
<EmptyPlaceholder message={formatMessage(messages.noWebsitesConfigured)}>
<Link href="/settings/websites">
<Button>
2023-04-20 11:41:06 +02:00
<Icon rotate={dir === 'rtl' ? 180 : 0}>
<Icons.ArrowRight />
</Icon>
<Text>{formatMessage(messages.goToSettings)}</Text>
</Button>
</Link>
</EmptyPlaceholder>
)}
{hasData && (
<>
{editing && <DashboardEdit websites={data} />}
{!editing && <WebsiteChartList websites={data} showCharts={showCharts} limit={max} />}
{max < data.length && (
<Flexbox justifyContent="center">
<Button onClick={handleMore}>
2023-04-20 11:41:06 +02:00
<Icon rotate={dir === 'rtl' ? 180 : 0}>
<Icons.More />
</Icon>
<Text>{formatMessage(labels.more)}</Text>
</Button>
</Flexbox>
)}
</>
2022-03-04 04:45:49 +01:00
)}
</Page>
);
}
2023-04-21 17:00:42 +02:00
export default Dashboard;