feat: protect responsive board layouts
This commit is contained in:
1 parent
fa9444323b
commit
6728228387
46 files changed
+8933
-477
No files matched your search
@@ -11,13 +11,16 @@ import { updateBoardName } from "./updater";
|
||||
|
||||
const BoardContext = createContext<{
|
||||
board: RouterOutputs["board"]["getBoardByName"];
|
||||
layoutOverrideId: string | null;
|
||||
} | null>(null);
|
||||
|
||||
export const BoardProvider = ({
|
||||
children,
|
||||
initialBoard,
|
||||
layoutOverrideId = null,
|
||||
}: PropsWithChildren<{
|
||||
initialBoard: RouterOutputs["board"]["getBoardByName"];
|
||||
layoutOverrideId?: string | null;
|
||||
}>) => {
|
||||
const { data } = clientApi.board.getBoardByName.useQuery({ name: initialBoard.name }, { initialData: initialBoard });
|
||||
|
||||
@@ -39,6 +42,7 @@ export const BoardProvider = ({
|
||||
<BoardContext.Provider
|
||||
value={{
|
||||
board: data,
|
||||
layoutOverrideId,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
@@ -62,7 +66,14 @@ export const useOptionalBoard = () => {
|
||||
return context?.board ?? null;
|
||||
};
|
||||
|
||||
export const getCurrentLayout = (board: RouterOutputs["board"]["getBoardByName"]) => {
|
||||
export const useLayoutOverride = () => useContext(BoardContext)?.layoutOverrideId ?? null;
|
||||
|
||||
export const getCurrentLayout = (
|
||||
board: RouterOutputs["board"]["getBoardByName"],
|
||||
layoutOverrideId: string | null = null,
|
||||
) => {
|
||||
if (layoutOverrideId && board.layouts.some((layout) => layout.id === layoutOverrideId)) return layoutOverrideId;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
if (typeof window === "undefined") return board.layouts.at(0)!.id;
|
||||
|
||||
@@ -75,11 +86,12 @@ export const getCurrentLayout = (board: RouterOutputs["board"]["getBoardByName"]
|
||||
|
||||
export const useCurrentLayout = () => {
|
||||
const board = useRequiredBoard();
|
||||
const [currentLayout, setCurrentLayout] = useState(getCurrentLayout(board));
|
||||
const layoutOverrideId = useLayoutOverride();
|
||||
const [currentLayout, setCurrentLayout] = useState(getCurrentLayout(board, layoutOverrideId));
|
||||
|
||||
const onResize = useCallback(() => {
|
||||
setCurrentLayout(getCurrentLayout(board));
|
||||
}, [board]);
|
||||
setCurrentLayout(getCurrentLayout(board, layoutOverrideId));
|
||||
}, [board, layoutOverrideId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined") return;
|
||||
@@ -90,6 +102,8 @@ export const useCurrentLayout = () => {
|
||||
};
|
||||
}, [onResize]);
|
||||
|
||||
useEffect(() => onResize(), [onResize]);
|
||||
|
||||
return currentLayout;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
"use client";
|
||||
|
||||
import type { PropsWithChildren } from "react";
|
||||
import { createContext, useContext } from "react";
|
||||
import { createContext, useContext, useEffect } from "react";
|
||||
import { useDisclosure } from "@mantine/hooks";
|
||||
|
||||
const EditModeContext = createContext<ReturnType<typeof useDisclosure> | null>(null);
|
||||
|
||||
export const EditModeProvider = ({ children }: PropsWithChildren) => {
|
||||
const editModeDisclosure = useDisclosure(false);
|
||||
export const EditModeProvider = ({ children, initialOpen = false }: PropsWithChildren<{ initialOpen?: boolean }>) => {
|
||||
const editModeDisclosure = useDisclosure(initialOpen);
|
||||
const [, { open }] = editModeDisclosure;
|
||||
|
||||
useEffect(() => {
|
||||
if (initialOpen) open();
|
||||
}, [initialOpen, open]);
|
||||
|
||||
return <EditModeContext.Provider value={editModeDisclosure}>{children}</EditModeContext.Provider>;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user