umami/src/app/(main)/dashboard/DashboardEdit.tsx

114 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-08-05 04:37:18 +00:00
import { useState, useMemo } from 'react';
2022-08-04 10:56:30 +00:00
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
2022-08-05 04:37:18 +00:00
import classNames from 'classnames';
2024-02-03 05:06:55 +00:00
import { Button, Loading } from 'react-basics';
import { firstBy } from 'thenby';
2022-08-05 04:37:18 +00:00
import useDashboard, { saveDashboard } from 'store/dashboard';
2024-02-03 05:06:55 +00:00
import { useMessages, useWebsites } from 'components/hooks';
2022-08-04 10:56:30 +00:00
import styles from './DashboardEdit.module.css';
2024-02-03 05:06:55 +00:00
const DRAG_ID = 'dashboard-website-ordering';
2022-08-04 10:56:30 +00:00
2024-02-03 05:06:55 +00:00
export function DashboardEdit({ teamId }: { teamId: string }) {
2022-08-04 10:56:30 +00:00
const settings = useDashboard();
const { websiteOrder } = settings;
2023-03-22 21:05:55 +00:00
const { formatMessage, labels } = useMessages();
2022-08-05 04:37:18 +00:00
const [order, setOrder] = useState(websiteOrder || []);
2024-02-03 05:06:55 +00:00
const {
result,
query: { isLoading },
} = useWebsites({ teamId });
const websites = result?.data;
2022-08-04 10:56:30 +00:00
2023-08-30 22:23:08 +00:00
const ordered = useMemo(() => {
if (websites) {
return websites
2024-02-03 05:06:55 +00:00
.map((website: { id: any }) => ({ ...website, order: order.indexOf(website.id) }))
2023-08-30 22:23:08 +00:00
.sort(firstBy('order'));
}
return [];
}, [websites, order]);
2022-08-04 10:56:30 +00:00
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);
2022-11-01 06:42:37 +00:00
setOrder(orderedWebsites.map(website => website?.id || 0));
2022-08-04 10:56:30 +00:00
}
function handleSave() {
saveDashboard({
editing: false,
websiteOrder: order,
});
}
function handleCancel() {
2022-08-04 21:30:09 +00:00
saveDashboard({ editing: false, websiteOrder });
2022-08-04 10:56:30 +00:00
}
function handleReset() {
2022-08-05 04:37:18 +00:00
setOrder([]);
2022-08-04 10:56:30 +00:00
}
2024-02-03 05:06:55 +00:00
if (isLoading) {
return <Loading />;
}
2022-08-04 10:56:30 +00:00
return (
2023-10-01 23:11:12 +00:00
<>
2022-08-04 10:56:30 +00:00
<div className={styles.buttons}>
2023-12-03 11:07:03 +00:00
<Button onClick={handleSave} variant="primary" size="sm">
2023-03-22 21:05:55 +00:00
{formatMessage(labels.save)}
2022-08-04 10:56:30 +00:00
</Button>
2023-12-03 11:07:03 +00:00
<Button onClick={handleCancel} size="sm">
2023-03-22 21:05:55 +00:00
{formatMessage(labels.cancel)}
2022-08-04 10:56:30 +00:00
</Button>
2023-12-03 11:07:03 +00:00
<Button onClick={handleReset} size="sm">
2023-03-22 21:05:55 +00:00
{formatMessage(labels.reset)}
2022-08-04 10:56:30 +00:00
</Button>
</div>
<div className={styles.dragActive}>
<DragDropContext onDragEnd={handleWebsiteDrag}>
2024-02-03 05:06:55 +00:00
<Droppable droppableId={DRAG_ID}>
2022-08-04 10:56:30 +00:00
{(provided, snapshot) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
style={{ marginBottom: snapshot.isDraggingOver ? 260 : null }}
>
2022-11-01 06:42:37 +00:00
{ordered.map(({ id, name, domain }, index) => (
2024-02-03 05:06:55 +00:00
<Draggable key={id} draggableId={`${DRAG_ID}-${id}`} index={index}>
2022-08-04 21:30:09 +00:00
{(provided, snapshot) => (
2022-08-04 10:56:30 +00:00
<div
ref={provided.innerRef}
2022-08-04 21:30:09 +00:00
className={classNames(styles.item, {
[styles.active]: snapshot.isDragging,
})}
2022-08-04 10:56:30 +00:00
{...provided.draggableProps}
{...provided.dragHandleProps}
>
2022-08-04 21:30:09 +00:00
<div className={styles.text}>
<h1>{name}</h1>
<h2>{domain}</h2>
</div>
2022-08-04 10:56:30 +00:00
</div>
)}
</Draggable>
))}
2022-08-05 04:37:18 +00:00
{provided.placeholder}
2022-08-04 10:56:30 +00:00
</div>
)}
</Droppable>
</DragDropContext>
</div>
2023-10-01 23:11:12 +00:00
</>
2022-08-04 10:56:30 +00:00
);
}
2023-04-21 15:00:42 +00:00
export default DashboardEdit;