umami/components/pages/Dashboard.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-03-04 04:45:49 +01:00
import { useState } from 'react';
2022-08-04 12:56:30 +02:00
import { defineMessages, useIntl } from 'react-intl';
2022-03-04 04:45:49 +01:00
import { useRouter } from 'next/router';
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
import WebsiteList from 'components/pages/WebsiteList';
import Button from 'components/common/Button';
import DashboardSettingsButton from 'components/settings/DashboardSettingsButton';
import useFetch from 'hooks/useFetch';
2022-08-04 12:56:30 +02:00
import useDashboard from 'store/dashboard';
import DashboardEdit from './DashboardEdit';
2022-03-04 04:45:49 +01:00
import styles from './WebsiteList.module.css';
2022-08-04 12:56:30 +02:00
const messages = defineMessages({
dashboard: { id: 'label.dashboard', defaultMessage: 'Dashboard' },
more: { id: 'label.more', defaultMessage: 'More' },
});
2022-03-04 04:45:49 +01:00
export default function Dashboard() {
const router = useRouter();
const { id } = router.query;
const userId = id?.[0];
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 { data } = useFetch('/websites', { params: { user_id: userId } });
2022-08-04 12:56:30 +02:00
const { formatMessage } = useIntl();
2022-03-04 04:45:49 +01:00
function handleMore() {
setMax(max + limit);
}
if (!data) {
return null;
}
return (
<Page>
<PageHeader>
2022-08-04 12:56:30 +02:00
<div>{formatMessage(messages.dashboard)}</div>
{!editing && <DashboardSettingsButton />}
2022-03-04 04:45:49 +01:00
</PageHeader>
2022-08-05 06:37:18 +02:00
{editing && <DashboardEdit websites={data} />}
{!editing && <WebsiteList websites={data} showCharts={showCharts} limit={max} />}
2022-03-04 04:45:49 +01:00
{max < data.length && (
<Button className={styles.button} onClick={handleMore}>
2022-08-04 12:56:30 +02:00
{formatMessage(messages.more)}
2022-03-04 04:45:49 +01:00
</Button>
)}
</Page>
);
}