diff --git a/packages/web/src/app/bookmarks.tsx b/packages/web/src/app/bookmarks.tsx index 9b76d6f..98ff7ad 100644 --- a/packages/web/src/app/bookmarks.tsx +++ b/packages/web/src/app/bookmarks.tsx @@ -34,7 +34,9 @@ interface BookmarkPageState { bookmarkToBeDeleted: LinkBookmark | null layoutMode: LayoutMode activeDialog: ActiveDialog + actionBarHeight: number + setActionBarHeight: (height: number) => void setActiveDialog: (dialog: ActiveDialog) => void setBookmarkItemExpanded: (isExpanded: boolean) => void setBookmarkPreviewOpened: (isOpened: boolean) => void @@ -54,6 +56,11 @@ const useBookmarkPageStore = create()((set, get) => ({ bookmarkToBeDeleted: null, layoutMode: LAYOUT_MODE.popup, activeDialog: ActiveDialog.None, + actionBarHeight: 0, + + setActionBarHeight(height: number) { + set({ actionBarHeight: height }) + }, setActiveDialog(dialog: ActiveDialog) { set({ activeDialog: dialog }) @@ -77,16 +84,21 @@ const useBookmarkPageStore = create()((set, get) => ({ reconcileSelection(bookmarks: LinkBookmark[]) { const { selectedBookmarkId, selectedBookmarkIndex } = get() - const newIndex = bookmarks.findIndex((bookmark) => bookmark.id === selectedBookmarkId) - if (newIndex !== selectedBookmarkIndex) { - if (newIndex >= 0) { - set({ selectedBookmarkIndex: newIndex }) - } else if (selectedBookmarkIndex >= bookmarks.length - 1) { - set({ selectedBookmarkIndex: bookmarks.length - 1 }) - } else if (selectedBookmarkIndex === 0) { - set({ selectedBookmarkIndex: 0 }) - } else { - set({ selectedBookmarkIndex: selectedBookmarkIndex + 1 }) + + if (!selectedBookmarkId) { + set({ selectedBookmarkId: bookmarks[selectedBookmarkIndex].id }) + } else { + const newIndex = bookmarks.findIndex((bookmark) => bookmark.id === selectedBookmarkId) + if (newIndex !== selectedBookmarkIndex) { + if (newIndex >= 0) { + set({ selectedBookmarkIndex: newIndex }) + } else if (selectedBookmarkIndex >= bookmarks.length - 1) { + set({ selectedBookmarkIndex: bookmarks.length - 1 }) + } else if (selectedBookmarkIndex === 0) { + set({ selectedBookmarkIndex: 0 }) + } else { + set({ selectedBookmarkIndex: selectedBookmarkIndex + 1 }) + } } } }, @@ -121,15 +133,7 @@ function Page() { return (
-
-
-

- - YOUR BOOKMARKS -

-
- -
+
@@ -145,8 +149,8 @@ function Main({ children }: React.PropsWithChildren) { return (
{children} @@ -154,6 +158,28 @@ function Main({ children }: React.PropsWithChildren) { ) } +function BookmarkListPane() { + const isBookmarkPreviewOpened = useBookmarkPageStore((state) => state.isBookmarkPreviewOpened) + + return ( +
+
+

+ +  >  + + YOUR BOOKMARKS +

+
+ +
+ ) +} + function PageDialog() { const dialog = useBookmarkPageStore((state) => state.activeDialog) switch (dialog) { @@ -385,42 +411,57 @@ function BookmarkList({ bookmarks }: { bookmarks: LinkBookmark[] }) { reconcileSelection(bookmarks) }, [bookmarks, reconcileSelection]) - useEffect(() => { - function onKeyDown(event: KeyboardEvent) { - const state = useBookmarkPageStore.getState() + useMnemonics( + { + j: selectNextItem, + ArrowDown: selectNextItem, - switch (event.key) { - case "ArrowDown": { - const nextIndex = state.selectedBookmarkIndex + 1 - if (nextIndex < bookmarks.length) { - state.selectBookmark(bookmarks[nextIndex], nextIndex) - } - break - } - case "ArrowUp": { - const prevIndex = state.selectedBookmarkIndex - 1 - if (prevIndex >= 0) { - state.selectBookmark(bookmarks[prevIndex], prevIndex) - } - break - } - case "ArrowLeft": - state.setBookmarkItemExpanded(false) - break - case "ArrowRight": - state.setBookmarkItemExpanded(true) - break - default: - break - } + k: selectPrevItem, + ArrowUp: selectPrevItem, + + h: collapseItem, + ArrowLeft: collapseItem, + + l: expandItem, + ArrowRight: expandItem, + + Enter: () => { + const state = useBookmarkPageStore.getState() + expandItem() + state.setBookmarkPreviewOpened(true) + }, + }, + { + ignore: useCallback(() => useBookmarkPageStore.getState().activeDialog !== ActiveDialog.None, []), + }, + ) + + function selectPrevItem() { + const state = useBookmarkPageStore.getState() + const prevIndex = state.selectedBookmarkIndex - 1 + if (prevIndex >= 0) { + state.selectBookmark(bookmarks[prevIndex], prevIndex) } + } - window.addEventListener("keydown", onKeyDown) - - return () => { - window.removeEventListener("keydown", onKeyDown) + function selectNextItem() { + const state = useBookmarkPageStore.getState() + const nextIndex = state.selectedBookmarkIndex + 1 + if (nextIndex < bookmarks.length) { + state.selectBookmark(bookmarks[nextIndex], nextIndex) } - }, [bookmarks]) + } + + function expandItem() { + const state = useBookmarkPageStore.getState() + state.setBookmarkItemExpanded(true) + } + + function collapseItem() { + const state = useBookmarkPageStore.getState() + state.setBookmarkItemExpanded(false) + state.setBookmarkPreviewOpened(false) + } return (
@@ -444,7 +485,7 @@ function BookmarkPreview() { return (
state.selectedBookmarkId) + const actionBarHeight = useBookmarkPageStore((state) => state.actionBarHeight) const { data, status } = useAuthenticatedQuery(["bookmarks", selectedBookmarkId], () => fetchApi(`/bookmark/${selectedBookmarkId}`, { headers: { @@ -473,7 +515,13 @@ function BookmarkPreviewFrame() {

) case "success": - return