mirror of
https://github.com/umami-software/umami.git
synced 2026-02-13 00:55:37 +01:00
Add board state management with updateBoard and saveBoard methods.
BoardProvider now manages local board state and exposes updateBoard for editing and saveBoard for persisting to the database. Supports both create and edit modes with proper redirect after creation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
e08907d998
commit
68c56060b3
7 changed files with 176 additions and 48 deletions
|
|
@ -1,17 +1,86 @@
|
|||
'use client';
|
||||
import { Loading } from '@umami/react-zen';
|
||||
import { createContext, type ReactNode } from 'react';
|
||||
import { Loading, useToast } from '@umami/react-zen';
|
||||
import { createContext, type ReactNode, useCallback, useEffect, useState } from 'react';
|
||||
import { useApi, useMessages, useModified, useNavigation } from '@/components/hooks';
|
||||
import { useBoardQuery } from '@/components/hooks/queries/useBoardQuery';
|
||||
import type { Board } from '@/generated/prisma/client';
|
||||
|
||||
export const BoardContext = createContext<Board>(null);
|
||||
export interface BoardContextValue {
|
||||
board: Partial<Board>;
|
||||
updateBoard: (data: Partial<Board>) => void;
|
||||
saveBoard: () => Promise<Board>;
|
||||
isPending: boolean;
|
||||
}
|
||||
|
||||
export function BoardProvider({ boardId, children }: { boardId: string; children: ReactNode }) {
|
||||
const { data: board, isFetching, isLoading } = useBoardQuery(boardId);
|
||||
export const BoardContext = createContext<BoardContextValue>(null);
|
||||
|
||||
if (isFetching && isLoading) {
|
||||
const defaultBoard: Partial<Board> = {
|
||||
name: '',
|
||||
description: '',
|
||||
};
|
||||
|
||||
export function BoardProvider({ boardId, children }: { boardId?: string; children: ReactNode }) {
|
||||
const { data, isFetching, isLoading } = useBoardQuery(boardId);
|
||||
const { post, useMutation } = useApi();
|
||||
const { touch } = useModified();
|
||||
const { toast } = useToast();
|
||||
const { formatMessage, labels, messages } = useMessages();
|
||||
const { router, renderUrl } = useNavigation();
|
||||
|
||||
const [board, setBoard] = useState<Partial<Board>>(data ?? defaultBoard);
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setBoard(data);
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
const { mutateAsync, isPending } = useMutation({
|
||||
mutationFn: (boardData: Partial<Board>) => {
|
||||
if (boardData.id) {
|
||||
return post(`/boards/${boardData.id}`, boardData);
|
||||
}
|
||||
return post('/boards', { ...boardData, type: 'dashboard', slug: '' });
|
||||
},
|
||||
});
|
||||
|
||||
const updateBoard = useCallback((data: Partial<Board>) => {
|
||||
setBoard(current => ({ ...current, ...data }));
|
||||
}, []);
|
||||
|
||||
const saveBoard = useCallback(async () => {
|
||||
const defaultName = formatMessage(labels.untitled);
|
||||
const result = await mutateAsync({ ...board, name: board.name || defaultName });
|
||||
|
||||
toast(formatMessage(messages.saved));
|
||||
touch('boards');
|
||||
|
||||
if (board.id) {
|
||||
touch(`board:${board.id}`);
|
||||
} else if (result?.id) {
|
||||
router.push(renderUrl(`/boards/${result.id}`));
|
||||
}
|
||||
|
||||
return result;
|
||||
}, [
|
||||
board,
|
||||
mutateAsync,
|
||||
toast,
|
||||
formatMessage,
|
||||
labels.untitled,
|
||||
messages.saved,
|
||||
touch,
|
||||
router,
|
||||
renderUrl,
|
||||
]);
|
||||
|
||||
if (boardId && isFetching && isLoading) {
|
||||
return <Loading placement="absolute" />;
|
||||
}
|
||||
|
||||
return <BoardContext.Provider value={board}>{children}</BoardContext.Provider>;
|
||||
return (
|
||||
<BoardContext.Provider value={{ board, updateBoard, saveBoard, isPending }}>
|
||||
{children}
|
||||
</BoardContext.Provider>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue