umami/components/pages/WebsiteList.js

119 lines
3.9 KiB
JavaScript
Raw Normal View History

import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
2020-09-06 02:27:01 +02:00
import { FormattedMessage } from 'react-intl';
2020-10-02 02:32:49 +02:00
import Link from 'components/common/Link';
import WebsiteChart from 'components/metrics/WebsiteChart';
2020-08-12 07:24:41 +02:00
import Page from 'components/layout/Page';
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
import Arrow from 'assets/arrow-right.svg';
2020-07-30 08:25:52 +02:00
import styles from './WebsiteList.module.css';
import { orderByWebsiteMap } from 'lib/format';
import { useMemo } from 'react';
import useStore, { setDashboard } from 'store/app';
const selector = state => state.dashboard;
2022-03-04 04:45:49 +01:00
export default function WebsiteList({ websites, showCharts, limit }) {
const store = useStore(selector);
const { websiteOrdering, changeOrderMode } = store;
const ordered = useMemo(
() => orderByWebsiteMap(websites, websiteOrdering),
[websites, websiteOrdering],
);
const dragId = 'dashboard-website-ordering';
function handleWebsiteDrag({ destination, source }) {
if (!destination || destination.index === source.index) return;
const orderedWebsites = [...ordered];
const [removed] = orderedWebsites.splice(source.index, 1);
orderedWebsites.splice(destination.index, 0, removed);
setDashboard({
...store,
websiteOrdering: orderedWebsites
.map((i, k) => ({ [i.website_uuid]: k }))
.reduce((a, b) => ({ ...a, ...b })),
});
}
2022-03-04 04:45:49 +01:00
if (websites.length === 0) {
return (
<Page>
2020-09-06 02:27:01 +02:00
<EmptyPlaceholder
msg={
<FormattedMessage
2020-09-17 07:29:40 +02:00
id="message.no-websites-configured"
2020-09-06 02:27:01 +02:00
defaultMessage="You don't have any websites configured."
/>
}
>
2020-10-02 02:32:49 +02:00
<Link href="/settings" icon={<Arrow />} iconRight>
2020-09-26 07:31:18 +02:00
<FormattedMessage id="message.go-to-settings" defaultMessage="Go to settings" />
2020-10-02 02:32:49 +02:00
</Link>
</EmptyPlaceholder>
</Page>
);
}
return (
<div className={changeOrderMode && styles.websiteDragActive}>
{changeOrderMode ? (
<DragDropContext onDragEnd={handleWebsiteDrag}>
<Droppable droppableId={dragId}>
2022-07-29 02:16:03 +02:00
{(provided, snapshot) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
style={{ marginBottom: snapshot.isDraggingOver ? 260 : null }}
>
{ordered.map(({ website_id, name, domain }, index) =>
index < limit ? (
2022-07-29 02:16:03 +02:00
<Draggable
key={website_id}
draggableId={`${dragId}-${website_id}`}
index={index}
>
{provided => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
className={styles.website}
>
<WebsiteChart
websiteId={website_id}
title={name}
domain={domain}
showChart={changeOrderMode ? false : showCharts}
showLink
/>
</div>
)}
</Draggable>
) : null,
)}
</div>
)}
</Droppable>
</DragDropContext>
) : (
ordered.map(({ website_id, name, domain }, index) =>
index < limit ? (
<div key={website_id} className={styles.website}>
<WebsiteChart
websiteId={website_id}
title={name}
domain={domain}
showChart={showCharts}
showLink
/>
</div>
) : null,
)
)}
2022-03-04 04:45:49 +01:00
</div>
);
}