Add personal dashboard flow and per-component website selection
This introduces a user-scoped dashboard with board-style view/edit pages while keeping it unavailable in team context, and moves website targeting to component config so dashboard components can each select their own website.
This commit is contained in:
1 parent
2633697585
commit
631cc46f7f
73 files changed
+418
-88
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import { Button, LoadingButton, Row } from '@umami/react-zen';
|
||||
import { PageHeader } from '@/components/common/PageHeader';
|
||||
import { useBoard, useMessages, useNavigation } from '@/components/hooks';
|
||||
|
||||
export function DashboardEditHeader() {
|
||||
const { saveBoard, isPending } = useBoard();
|
||||
const { t, labels } = useMessages();
|
||||
const { router, renderUrl } = useNavigation();
|
||||
|
||||
const handleSave = async () => {
|
||||
await saveBoard();
|
||||
router.push(renderUrl('/dashboard', false));
|
||||
};
|
||||
|
||||
return (
|
||||
<PageHeader title={t(labels.dashboard)}>
|
||||
<Row gap="3">
|
||||
<Button variant="quiet" onPress={() => router.push(renderUrl('/dashboard', false))}>
|
||||
{t(labels.cancel)}
|
||||
</Button>
|
||||
<LoadingButton variant="primary" onPress={handleSave} isLoading={isPending}>
|
||||
{t(labels.save)}
|
||||
</LoadingButton>
|
||||
</Row>
|
||||
</PageHeader>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
'use client';
|
||||
import { Column } from '@umami/react-zen';
|
||||
import { useEffect } from 'react';
|
||||
import { BoardEditBody } from '@/app/(main)/boards/[boardId]/BoardEditBody';
|
||||
import { PageBody } from '@/components/common/PageBody';
|
||||
import { useNavigation } from '@/components/hooks';
|
||||
import { DashboardEditHeader } from './DashboardEditHeader';
|
||||
import { DashboardProvider } from './DashboardProvider';
|
||||
|
||||
export function DashboardEditPage() {
|
||||
const { teamId, router } = useNavigation();
|
||||
|
||||
useEffect(() => {
|
||||
if (teamId) {
|
||||
router.replace('/dashboard/edit');
|
||||
}
|
||||
}, [teamId, router]);
|
||||
|
||||
if (teamId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<DashboardProvider editing>
|
||||
<PageBody>
|
||||
<Column>
|
||||
<DashboardEditHeader />
|
||||
<BoardEditBody requiresBoardWebsite={false} />
|
||||
</Column>
|
||||
</PageBody>
|
||||
</DashboardProvider>
|
||||
);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
'use client';
|
||||
import { Column } from '@umami/react-zen';
|
||||
import { PageBody } from '@/components/common/PageBody';
|
||||
import { PageHeader } from '@/components/common/PageHeader';
|
||||
import { useMessages } from '@/components/hooks';
|
||||
|
||||
export function DashboardPage() {
|
||||
const { t, labels } = useMessages();
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
<Column margin="2">
|
||||
<PageHeader title={t(labels.dashboard)}></PageHeader>
|
||||
</Column>
|
||||
</PageBody>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
'use client';
|
||||
import { Loading, useToast } from '@umami/react-zen';
|
||||
import { type ReactNode, useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { BoardContext, type LayoutGetter } from '@/app/(main)/boards/BoardProvider';
|
||||
import { getComponentDefinition } from '@/app/(main)/boards/boardComponentRegistry';
|
||||
import { useApi, useDashboardQuery, useMessages, useModified } from '@/components/hooks';
|
||||
import type { Board, BoardParameters } from '@/lib/types';
|
||||
|
||||
const createDefaultBoard = (): Partial<Board> => ({
|
||||
name: '',
|
||||
description: '',
|
||||
parameters: {
|
||||
rows: [{ id: uuid(), columns: [{ id: uuid(), component: null }] }],
|
||||
},
|
||||
});
|
||||
|
||||
function sanitizeBoardParameters(parameters?: BoardParameters): BoardParameters | undefined {
|
||||
if (!parameters?.rows) {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
return {
|
||||
...parameters,
|
||||
rows: parameters.rows.map(row => ({
|
||||
...row,
|
||||
columns: row.columns.map(column => {
|
||||
if (column.component && !getComponentDefinition(column.component.type)) {
|
||||
return {
|
||||
...column,
|
||||
component: null,
|
||||
};
|
||||
}
|
||||
|
||||
return column;
|
||||
}),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export function DashboardProvider({
|
||||
editing = false,
|
||||
children,
|
||||
}: {
|
||||
editing?: boolean;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const { data, isFetching, isLoading } = useDashboardQuery();
|
||||
const { post, useMutation } = useApi();
|
||||
const { touch } = useModified();
|
||||
const { toast } = useToast();
|
||||
const { t, labels, messages } = useMessages();
|
||||
const [board, setBoard] = useState<Partial<Board>>(data ?? createDefaultBoard());
|
||||
const layoutGetterRef = useRef<LayoutGetter | null>(null);
|
||||
|
||||
const registerLayoutGetter = useCallback((getter: LayoutGetter) => {
|
||||
layoutGetterRef.current = getter;
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setBoard({
|
||||
...data,
|
||||
parameters: sanitizeBoardParameters(data.parameters),
|
||||
});
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
const { mutateAsync, isPending } = useMutation({
|
||||
mutationFn: (boardData: Partial<Board>) => {
|
||||
return post('/dashboard', boardData);
|
||||
},
|
||||
});
|
||||
|
||||
const updateBoard = useCallback((data: Partial<Board>) => {
|
||||
setBoard(current => ({ ...current, ...data }));
|
||||
}, []);
|
||||
|
||||
const saveBoard = useCallback(async () => {
|
||||
const dashboardName = t(labels.dashboard);
|
||||
const layoutData = layoutGetterRef.current?.();
|
||||
const parameters = sanitizeBoardParameters(
|
||||
layoutData ? { ...board.parameters, ...layoutData } : board.parameters,
|
||||
);
|
||||
|
||||
const result = await mutateAsync({
|
||||
...board,
|
||||
name: dashboardName,
|
||||
description: '',
|
||||
parameters,
|
||||
});
|
||||
|
||||
toast(t(messages.saved));
|
||||
touch('dashboard');
|
||||
touch('boards');
|
||||
|
||||
return result;
|
||||
}, [board, labels.dashboard, messages.saved, mutateAsync, t, toast, touch]);
|
||||
|
||||
if (isFetching && isLoading) {
|
||||
return <Loading placement="absolute" />;
|
||||
}
|
||||
|
||||
return (
|
||||
<BoardContext.Provider
|
||||
value={{ board, editing, updateBoard, saveBoard, isPending, registerLayoutGetter }}
|
||||
>
|
||||
{children}
|
||||
</BoardContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { IconLabel } from '@/components/common/IconLabel';
|
||||
import { LinkButton } from '@/components/common/LinkButton';
|
||||
import { PageHeader } from '@/components/common/PageHeader';
|
||||
import { useMessages, useNavigation } from '@/components/hooks';
|
||||
import { Edit } from '@/components/icons';
|
||||
|
||||
export function DashboardViewHeader() {
|
||||
const { t, labels } = useMessages();
|
||||
const { renderUrl } = useNavigation();
|
||||
|
||||
return (
|
||||
<PageHeader title={t(labels.dashboard)}>
|
||||
<LinkButton href={renderUrl('/dashboard/edit', false)}>
|
||||
<IconLabel icon={<Edit />}>{t(labels.edit)}</IconLabel>
|
||||
</LinkButton>
|
||||
</PageHeader>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
'use client';
|
||||
import { Column } from '@umami/react-zen';
|
||||
import { useEffect } from 'react';
|
||||
import { BoardViewBody } from '@/app/(main)/boards/[boardId]/BoardViewBody';
|
||||
import { EmptyPlaceholder } from '@/components/common/EmptyPlaceholder';
|
||||
import { PageBody } from '@/components/common/PageBody';
|
||||
import { useBoard, useMessages, useNavigation } from '@/components/hooks';
|
||||
import { DashboardProvider } from './DashboardProvider';
|
||||
import { DashboardViewHeader } from './DashboardViewHeader';
|
||||
|
||||
function DashboardContent() {
|
||||
const { board } = useBoard();
|
||||
const { t, labels, messages } = useMessages();
|
||||
const rows = board?.parameters?.rows ?? [];
|
||||
const hasComponents = rows.some(row => row.columns?.some(column => !!column.component));
|
||||
|
||||
if (!hasComponents) {
|
||||
return (
|
||||
<EmptyPlaceholder title={t(labels.dashboard)} description={t(messages.emptyDashboard)} />
|
||||
);
|
||||
}
|
||||
|
||||
return <BoardViewBody />;
|
||||
}
|
||||
|
||||
export function DashboardViewPage() {
|
||||
const { teamId, router } = useNavigation();
|
||||
|
||||
useEffect(() => {
|
||||
if (teamId) {
|
||||
router.replace('/dashboard');
|
||||
}
|
||||
}, [teamId, router]);
|
||||
|
||||
if (teamId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<DashboardProvider>
|
||||
<PageBody>
|
||||
<Column>
|
||||
<DashboardViewHeader />
|
||||
<DashboardContent />
|
||||
</Column>
|
||||
</PageBody>
|
||||
</DashboardProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { Metadata } from 'next';
|
||||
import { DashboardEditPage } from '../DashboardEditPage';
|
||||
|
||||
export default function () {
|
||||
return <DashboardEditPage />;
|
||||
}
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Edit Dashboard',
|
||||
};
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { Metadata } from 'next';
|
||||
import { DashboardPage } from './DashboardPage';
|
||||
import { DashboardViewPage } from './DashboardViewPage';
|
||||
|
||||
export default async function () {
|
||||
return <DashboardPage />;
|
||||
return <DashboardViewPage />;
|
||||
}
|
||||
|
||||
export const metadata: Metadata = {
|
||||
|
||||
Reference in new issue
Block a user