From 1def5eca2065347c470e8a18d3abe1be0a144023 Mon Sep 17 00:00:00 2001 From: Aslam H Date: Mon, 9 Sep 2024 19:04:32 +0700 Subject: [PATCH] feat(link): command+backspace to handle delete link --- web/components/routes/link/list.tsx | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/web/components/routes/link/list.tsx b/web/components/routes/link/list.tsx index 1a5fb372..63e6a38f 100644 --- a/web/components/routes/link/list.tsx +++ b/web/components/routes/link/list.tsx @@ -21,6 +21,8 @@ import { LinkItem } from "./partials/link-item" import { useQueryState } from "nuqs" import { learningStateAtom } from "./header" import { commandPaletteOpenAtom } from "@/components/custom/command-palette/command-palette" +import { useConfirm } from "@omit/react-confirm-dialog" +import { toast } from "sonner" interface LinkListProps { activeItemIndex: number | null @@ -33,6 +35,7 @@ const LinkList: React.FC = ({ activeItemIndex, setActiveItemIndex const [editId, setEditId] = useQueryState("editId") const [activeLearningState] = useAtom(learningStateAtom) const [draggingId, setDraggingId] = React.useState(null) + const confirm = useConfirm() const { me } = useAccount({ root: { personalLinks: [] } @@ -76,6 +79,55 @@ const LinkList: React.FC = ({ activeItemIndex, setActiveItemIndex } }) + useKey( + event => (event.metaKey || event.ctrlKey) && event.key === "Backspace", + async () => { + if (activeItemIndex !== null) { + const activeLink = sortedLinks[activeItemIndex] + if (activeLink) { + console.log("Delete link", activeLink.toJSON()) + + const result = await confirm({ + title: `Delete "${activeLink.title}"?`, + description: "This action cannot be undone.", + alertDialogTitle: { + className: "text-base" + }, + cancelButton: { + variant: "outline" + }, + confirmButton: { + variant: "destructive" + } + }) + + if (result) { + if (!me?.root.personalLinks) return + + const index = me.root.personalLinks.findIndex(item => item?.id === activeLink.id) + if (index === -1) { + console.error("Delete operation fail", { index, activeLink }) + return + } + + toast.success("Link deleted.", { + position: "bottom-right", + description: ( + + {activeLink.title} has been deleted. + + ) + }) + + me.root.personalLinks.splice(index, 1) + setEditId(null) + } + } + } + }, + { event: "keydown" } + ) + // on mounted, if editId is set, set activeItemIndex to the index of the item with the editId useEffect(() => { if (editId) {