diff --git a/web/components/routes/link/LinkRoute.tsx b/web/components/routes/link/LinkRoute.tsx index 32e3605d..bd88d6ab 100644 --- a/web/components/routes/link/LinkRoute.tsx +++ b/web/components/routes/link/LinkRoute.tsx @@ -4,7 +4,7 @@ import { LinkHeader } from "@/components/routes/link/header" import { LinkList } from "@/components/routes/link/list" import { LinkManage } from "@/components/routes/link/manage" import { useQueryState } from "nuqs" -import { useEffect } from "react" +import { useEffect, useState } from "react" import { useAtom } from "jotai" import { linkEditIdAtom } from "@/store/link" import { LinkBottomBar } from "./bottom-bar" @@ -12,6 +12,7 @@ import { LinkBottomBar } from "./bottom-bar" export function LinkRoute() { const [, setEditId] = useAtom(linkEditIdAtom) const [nuqsEditId] = useQueryState("editId") + const [activeItemIndex, setActiveItemIndex] = useState(null) useEffect(() => { setEditId(nuqsEditId) @@ -21,8 +22,7 @@ export function LinkRoute() {
- {/* Refresh list everytime editId is changed */} - +
) diff --git a/web/components/routes/link/list.tsx b/web/components/routes/link/list.tsx index b9b58350..3de70727 100644 --- a/web/components/routes/link/list.tsx +++ b/web/components/routes/link/list.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useMemo, useState } from "react" +import React, { useCallback, useEffect, useMemo } from "react" import { DndContext, closestCenter, @@ -22,13 +22,16 @@ import { useQueryState } from "nuqs" import { learningStateAtom } from "./header" import { commandPaletteOpenAtom } from "@/components/custom/command-palette/command-palette" -interface LinkListProps {} +interface LinkListProps { + activeItemIndex: number | null + setActiveItemIndex: React.Dispatch> +} -const LinkList: React.FC = () => { +const LinkList: React.FC = ({ activeItemIndex, setActiveItemIndex }) => { const [isCommandPalettePpen] = useAtom(commandPaletteOpenAtom) const [editId, setEditId] = useQueryState("editId") const [activeLearningState] = useAtom(learningStateAtom) - const [activeItemIndex, setActiveItemIndex] = useState(null) + const [draggingId, setDraggingId] = React.useState(null) const { me } = useAccount({ root: { personalLinks: [] } @@ -36,7 +39,6 @@ const LinkList: React.FC = () => { const personalLinks = useMemo(() => me?.root?.personalLinks || [], [me?.root?.personalLinks]) const [sort] = useAtom(linkSortAtom) - const [draggingId, setDraggingId] = useState(null) const filteredLinks = useMemo( () => @@ -73,6 +75,16 @@ const LinkList: React.FC = () => { } }) + // on mounted, if editId is set, set activeItemIndex to the index of the item with the editId + useEffect(() => { + if (editId) { + const index = sortedLinks.findIndex(link => link?.id === editId) + if (index !== -1) { + setActiveItemIndex(index) + } + } + }, [editId, sortedLinks, setActiveItemIndex]) + const updateSequences = useCallback((links: PersonalLinkLists) => { links.forEach((link, index) => { if (link) { @@ -111,12 +123,28 @@ const LinkList: React.FC = () => { return newIndex }) + } else if (e.key === "Enter" && activeItemIndex !== null) { + e.preventDefault() + const activeLink = sortedLinks[activeItemIndex] + if (activeLink) { + setEditId(activeLink.id) + } } } window.addEventListener("keydown", handleKeyDown) return () => window.removeEventListener("keydown", handleKeyDown) - }, [me?.root?.personalLinks, sortedLinks, editId, sort, updateSequences, isCommandPalettePpen]) + }, [ + me?.root?.personalLinks, + sortedLinks, + editId, + sort, + updateSequences, + isCommandPalettePpen, + activeItemIndex, + setEditId, + setActiveItemIndex + ]) const handleDragStart = useCallback( (event: DragStartEvent) => { diff --git a/web/components/routes/link/partials/link-item.tsx b/web/components/routes/link/partials/link-item.tsx index 2c8c6901..ffab129e 100644 --- a/web/components/routes/link/partials/link-item.tsx +++ b/web/components/routes/link/partials/link-item.tsx @@ -90,82 +90,76 @@ export const LinkItem: React.FC = ({ onFocus={() => setActiveItemIndex(index)} onBlur={() => setActiveItemIndex(null)} onKeyDown={handleKeyDown} - className={cn("relative flex h-14 cursor-default items-center outline-none xl:h-11", { - "bg-muted-foreground/10": isActive, - "hover:bg-muted/50": !isActive - })} + className={cn( + "relative cursor-default outline-none", + "grid grid-cols-[auto_1fr_auto] items-center gap-x-2 px-2 py-2 sm:px-4 sm:py-2", + { + "bg-muted-foreground/10": isActive, + "hover:bg-muted/50": !isActive + } + )} onDoubleClick={handleRowDoubleClick} > -
-
- setOpenPopoverForId(open ? personalLink.id : null)} - > - - - - e.preventDefault()} - > - - - + setOpenPopoverForId(open ? personalLink.id : null)} + > + + + + e.preventDefault()} + > + + + - {personalLink.icon && ( - {personalLink.title} - )} -
-
-

- {personalLink.title} -

- {personalLink.url && ( -
-
- )} +
+ {personalLink.icon && ( + {personalLink.title} + )} +
+

{personalLink.title}

+ {personalLink.url && ( +
+
-
+ )}
+
-
- {personalLink.topic && {personalLink.topic.prettyName}} -
+
+ {personalLink.topic && {personalLink.topic.prettyName}}
)