perf: improve optimistic queries
This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
import type { ReactNode } from "react";
|
||||
import React, { createContext, useContext, useState } from "react";
|
||||
|
||||
import type { GetBoardByIdOutput, NewListInput } from "@kan/api/types";
|
||||
import { generateUID } from "@kan/shared/utils";
|
||||
|
||||
import { usePopup } from "~/providers/popup";
|
||||
import { api } from "~/utils/api";
|
||||
|
||||
interface BoardContextProps {
|
||||
boardData: GetBoardByIdOutput;
|
||||
setBoardData: React.Dispatch<React.SetStateAction<GetBoardByIdOutput>>;
|
||||
addList: (params: NewListInput) => void;
|
||||
removeCard: (params: { cardPublicId: string }) => void;
|
||||
refetchBoard: () => Promise<void>;
|
||||
}
|
||||
|
||||
const initialBoardData: GetBoardByIdOutput = {
|
||||
name: "",
|
||||
publicId: "",
|
||||
lists: [],
|
||||
labels: [],
|
||||
workspace: {
|
||||
publicId: "",
|
||||
members: [],
|
||||
},
|
||||
};
|
||||
|
||||
const BoardContext = createContext<BoardContextProps | undefined>(undefined);
|
||||
|
||||
export const BoardProvider: React.FC<{ children: ReactNode }> = ({
|
||||
children,
|
||||
}) => {
|
||||
const utils = api.useUtils();
|
||||
const [boardData, setBoardData] =
|
||||
useState<GetBoardByIdOutput>(initialBoardData);
|
||||
|
||||
const { showPopup } = usePopup();
|
||||
|
||||
const refetchBoard = async () => {
|
||||
if (!boardData?.publicId) return;
|
||||
|
||||
try {
|
||||
await utils.board.byId.refetch();
|
||||
} catch (e) {
|
||||
showPopup({
|
||||
header: "Error fetching board",
|
||||
message: "Please try again later, or contact customer support.",
|
||||
icon: "error",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const addList = ({ name, boardPublicId }: NewListInput) => {
|
||||
if (!boardData) return;
|
||||
|
||||
const newList = {
|
||||
publicId: generateUID(),
|
||||
name,
|
||||
boardId: 1,
|
||||
boardPublicId,
|
||||
cards: [],
|
||||
index: boardData.lists.length,
|
||||
};
|
||||
|
||||
const updatedLists = [...boardData.lists, newList];
|
||||
|
||||
setBoardData({ ...boardData, lists: updatedLists });
|
||||
};
|
||||
|
||||
const removeCard = ({ cardPublicId }: { cardPublicId: string }) => {
|
||||
if (!boardData) return;
|
||||
|
||||
const updatedLists = boardData.lists.map((list) => {
|
||||
const updatedCards = list.cards.filter(
|
||||
(card) => card.publicId !== cardPublicId,
|
||||
);
|
||||
return { ...list, cards: updatedCards };
|
||||
});
|
||||
|
||||
setBoardData({ ...boardData, lists: updatedLists });
|
||||
};
|
||||
|
||||
return (
|
||||
<BoardContext.Provider
|
||||
value={{
|
||||
boardData,
|
||||
setBoardData,
|
||||
addList,
|
||||
removeCard,
|
||||
refetchBoard,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</BoardContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useBoard = () => {
|
||||
const context = useContext(BoardContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useBoard must be used within a BoardProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user