From 38346be965d35769b289a89a91117bd2cef1285d Mon Sep 17 00:00:00 2001 From: Triston Date: Wed, 27 Mar 2024 22:39:42 -0500 Subject: [PATCH] fix various bugs related to app crashing --- src/HomePage.tsx | 8 +++++--- src/components/Board.tsx | 4 +++- src/components/ItemList.tsx | 8 ++++---- src/state/global.ts | 25 +++++++++++++------------ src/state/mouse.ts | 2 +- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/HomePage.tsx b/src/HomePage.tsx index 8de6914..c4d83d7 100644 --- a/src/HomePage.tsx +++ b/src/HomePage.tsx @@ -19,8 +19,8 @@ export function HomePage() { const [showArchived, setShowArchived] = useState(false) const [menuOpen, setMenuOpen] = useState(false) const { boards, addBoard } = useGlobal() - const activeBoards = boards.filter((b) => !b.archived) - const archivedBoards = boards.filter((b) => b.archived) + const activeBoards = boards?.filter((b) => !b.archived) ?? [] + const archivedBoards = boards?.filter((b) => b.archived) ?? [] return (
@@ -77,7 +77,9 @@ export function HomePage() { const data = await readFile(file) console.log("IMPORT", data) await JsonUtils.import(data) - window.location = "/" + const newLoc = new Location() + newLoc.replace("/") + window.location = newLoc }, }) input.click() diff --git a/src/components/Board.tsx b/src/components/Board.tsx index f184edf..57831e1 100644 --- a/src/components/Board.tsx +++ b/src/components/Board.tsx @@ -55,7 +55,9 @@ export function Board({ boardId }: { boardId: string }) { (b) => String(b.id) === boardId || b.uuid === boardId ) if (!board) { - window.location = "/" + const newLoc = new Location() + newLoc.replace("/") + window.location = newLoc return } selectBoard(board) diff --git a/src/components/ItemList.tsx b/src/components/ItemList.tsx index 407306d..c2f9529 100644 --- a/src/components/ItemList.tsx +++ b/src/components/ItemList.tsx @@ -227,10 +227,10 @@ function Item({ item, idx, listId }: ItemProps) { const { value: { tags, itemTags }, removeItemTag - } = useBoardTagsStore((state) => [ - ...state.tags, - ...state.itemTags.filter((it) => it.itemId === item.id), - ]) + } = useBoardTagsStore((state) => ({ + tags: state.tags, + itemTags: state.itemTags.filter((it) => it.itemId === item.id) + })) const itemItemTags: Array = useMemo(() => { const tagsForThisItem = itemTags.filter((it) => it.itemId === item.id) diff --git a/src/state/global.ts b/src/state/global.ts index 0c9eb09..64c9b9a 100644 --- a/src/state/global.ts +++ b/src/state/global.ts @@ -10,18 +10,19 @@ import { } from "../types" import { addBoard as addBoardDb } from "../idb" -export const GlobalCtx = createContext(null) -export const GlobalDispatchCtx = - createContext<(action: GlobalDispatchAction) => void>(null) +export const GlobalCtx = createContext(null) +export const GlobalDispatchCtx = createContext< + ((action: GlobalDispatchAction) => void) | null +>(null) export function useGlobal() { const dispatch = useContext(GlobalDispatchCtx) const setItemDragTarget = (payload: ItemDragTarget | null) => - dispatch({ type: "SET_ITEM_DRAG_TARGET", payload }) + dispatch?.({ type: "SET_ITEM_DRAG_TARGET", payload }) const setListDragTarget = (payload: ListDragTarget | null) => - dispatch({ type: "SET_LIST_DRAG_TARGET", payload }) + dispatch?.({ type: "SET_LIST_DRAG_TARGET", payload }) function handleListDrag(e: MouseEvent, clickedList: ClickedList) { if (!clickedList.mouseOffset) throw new Error("no mouseoffset") @@ -48,7 +49,7 @@ export function useGlobal() { const addBoard = async () => { const newBoard = await addBoardDb() - dispatch({ type: "ADD_BOARD", payload: newBoard }) + dispatch?.({ type: "ADD_BOARD", payload: newBoard }) } function handleItemDrag( @@ -86,27 +87,27 @@ export function useGlobal() { } function setBoardEditorOpen(value: boolean) { - dispatch({ type: "SET_BOARD_EDITOR_OPEN", payload: value }) + dispatch?.({ type: "SET_BOARD_EDITOR_OPEN", payload: value }) } return { ...useContext(GlobalCtx), addBoard, setRootElement: (payload: HTMLDivElement) => - dispatch({ type: "SET_ROOT_EL", payload }), + dispatch?.({ type: "SET_ROOT_EL", payload }), setBoardEditorOpen, setDragging: (dragging: boolean) => - dispatch({ type: "SET_DRAGGING", payload: { dragging } }), + dispatch?.({ type: "SET_DRAGGING", payload: { dragging } }), setClickedItem: (payload: ClickedItem | null) => - dispatch({ type: "SET_CLICKED_ITEM", payload }), + dispatch?.({ type: "SET_CLICKED_ITEM", payload }), setItemDragTarget, handleItemDrag, setClickedList: (payload: ClickedList | null) => - dispatch({ type: "SET_CLICKED_LIST", payload }), + dispatch?.({ type: "SET_CLICKED_LIST", payload }), setListDragTarget, handleListDrag, updateBoards: (payload: Board[]) => - dispatch({ type: "SET_BOARDS", payload }), + dispatch?.({ type: "SET_BOARDS", payload }), } } diff --git a/src/state/mouse.ts b/src/state/mouse.ts index 9a5692a..d82814a 100644 --- a/src/state/mouse.ts +++ b/src/state/mouse.ts @@ -6,5 +6,5 @@ type MouseContext = { setValue: (payload: Vector2) => void } -export const MouseCtx = createContext(null) +export const MouseCtx = createContext(null) export const useMouse = () => useContext(MouseCtx)