aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/sidebar
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2024-04-19 00:09:27 +0100
committerGitHub <noreply@github.com>2024-04-19 00:09:27 +0100
commite0999f701cd1834c3d940113cd8dd5247c5fe95f (patch)
treec4169a564ecd3f933e711bcc8ef7db20532174ea /apps/web/components/dashboard/sidebar
parentdeba31ee010f785a9739fd4df8a64a3056c9593d (diff)
downloadkarakeep-e0999f701cd1834c3d940113cd8dd5247c5fe95f.tar.zst
feature: Nested lists (#110). Fixes #62
* feature: Add support for nested lists * prevent moving the parent to a subtree
Diffstat (limited to '')
-rw-r--r--apps/web/components/dashboard/lists/EditListModal.tsx (renamed from apps/web/components/dashboard/sidebar/NewListModal.tsx)146
-rw-r--r--apps/web/components/dashboard/sidebar/AllLists.tsx79
-rw-r--r--apps/web/components/dashboard/sidebar/SidebarItem.tsx22
3 files changed, 198 insertions, 49 deletions
diff --git a/apps/web/components/dashboard/sidebar/NewListModal.tsx b/apps/web/components/dashboard/lists/EditListModal.tsx
index 5169fbb5..993c975b 100644
--- a/apps/web/components/dashboard/sidebar/NewListModal.tsx
+++ b/apps/web/components/dashboard/lists/EditListModal.tsx
@@ -1,5 +1,5 @@
-"use client";
-
+import { useEffect, useState } from "react";
+import { useRouter } from "next/navigation";
import { ActionButton } from "@/components/ui/action-button";
import { Button } from "@/components/ui/button";
import {
@@ -9,12 +9,14 @@ import {
DialogFooter,
DialogHeader,
DialogTitle,
+ DialogTrigger,
} from "@/components/ui/dialog";
import {
Form,
FormControl,
FormField,
FormItem,
+ FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
@@ -24,46 +26,75 @@ import {
PopoverTrigger,
} from "@/components/ui/popover";
import { toast } from "@/components/ui/use-toast";
-import { api } from "@/lib/trpc";
import data from "@emoji-mart/data";
import Picker from "@emoji-mart/react";
import { zodResolver } from "@hookform/resolvers/zod";
+import { X } from "lucide-react";
import { useForm } from "react-hook-form";
import { z } from "zod";
-import { create } from "zustand";
-export const useNewListModal = create<{
- open: boolean;
- setOpen: (v: boolean) => void;
-}>((set) => ({
- open: false,
- setOpen: (open: boolean) => set(() => ({ open })),
-}));
+import {
+ useCreateBookmarkList,
+ useEditBookmarkList,
+} from "@hoarder/shared-react/hooks/lists";
+import { ZBookmarkList } from "@hoarder/shared/types/lists";
-export default function NewListModal() {
- const { open, setOpen } = useNewListModal();
+import { BookmarkListSelector } from "./BookmarkListSelector";
+export function EditListModal({
+ open: userOpen,
+ setOpen: userSetOpen,
+ list,
+ parent,
+ children,
+}: {
+ open?: boolean;
+ setOpen?: (v: boolean) => void;
+ list?: ZBookmarkList;
+ parent?: ZBookmarkList;
+ children?: React.ReactNode;
+}) {
+ const router = useRouter();
+ if (
+ (userOpen !== undefined && !userSetOpen) ||
+ (userOpen === undefined && userSetOpen)
+ ) {
+ throw new Error("You must provide both open and setOpen or neither");
+ }
+ const [customOpen, customSetOpen] = useState(false);
const formSchema = z.object({
name: z.string(),
icon: z.string(),
+ parentId: z.string().nullish(),
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
- name: "",
- icon: "🚀",
+ name: list?.name ?? "",
+ icon: list?.icon ?? "🚀",
+ parentId: list?.parentId ?? parent?.id,
},
});
+ const [open, setOpen] = [
+ userOpen ?? customOpen,
+ userSetOpen ?? customSetOpen,
+ ];
- const listsInvalidationFunction = api.useUtils().lists.list.invalidate;
+ useEffect(() => {
+ form.reset({
+ name: list?.name ?? "",
+ icon: list?.icon ?? "🚀",
+ parentId: list?.parentId ?? parent?.id,
+ });
+ }, [open]);
- const { mutate: createList, isPending } = api.lists.create.useMutation({
- onSuccess: () => {
+ const { mutate: createList, isPending: isCreating } = useCreateBookmarkList({
+ onSuccess: (resp) => {
toast({
description: "List has been created!",
});
- listsInvalidationFunction();
setOpen(false);
+ router.push(`/dashboard/lists/${resp.id}`);
form.reset();
},
onError: (e) => {
@@ -90,6 +121,41 @@ export default function NewListModal() {
},
});
+ const { mutate: editList, isPending: isEditing } = useEditBookmarkList({
+ onSuccess: () => {
+ toast({
+ description: "List has been updated!",
+ });
+ setOpen(false);
+ form.reset();
+ },
+ onError: (e) => {
+ if (e.data?.code == "BAD_REQUEST") {
+ if (e.data.zodError) {
+ toast({
+ variant: "destructive",
+ description: Object.values(e.data.zodError.fieldErrors)
+ .flat()
+ .join("\n"),
+ });
+ } else {
+ toast({
+ variant: "destructive",
+ description: e.message,
+ });
+ }
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Something went wrong",
+ });
+ }
+ },
+ });
+
+ const isEdit = !!list;
+ const isPending = isCreating || isEditing;
+
return (
<Dialog
open={open}
@@ -98,15 +164,19 @@ export default function NewListModal() {
setOpen(s);
}}
>
+ {children && <DialogTrigger asChild>{children}</DialogTrigger>}
<DialogContent>
<Form {...form}>
<form
onSubmit={form.handleSubmit((value) => {
- createList(value);
+ value.parentId = value.parentId === "" ? null : value.parentId;
+ isEdit
+ ? editList({ ...value, listId: list.id })
+ : createList(value);
})}
>
<DialogHeader>
- <DialogTitle>New List</DialogTitle>
+ <DialogTitle>{isEdit ? "Edit" : "New"} List</DialogTitle>
</DialogHeader>
<div className="flex w-full gap-2 py-4">
<FormField
@@ -156,6 +226,38 @@ export default function NewListModal() {
}}
/>
</div>
+ <FormField
+ control={form.control}
+ name="parentId"
+ render={({ field }) => {
+ return (
+ <FormItem className="grow pb-4">
+ <FormLabel>Parent</FormLabel>
+ <div className="flex items-center gap-1">
+ <FormControl>
+ <BookmarkListSelector
+ // Hide the current list from the list of parents
+ hideSubtreeOf={list ? list.id : undefined}
+ value={field.value}
+ onChange={field.onChange}
+ placeholder={"No Parent"}
+ />
+ </FormControl>
+ <Button
+ type="button"
+ variant="ghost"
+ onClick={() => {
+ form.reset({ parentId: null });
+ }}
+ >
+ <X />
+ </Button>
+ </div>
+ <FormMessage />
+ </FormItem>
+ );
+ }}
+ />
<DialogFooter className="sm:justify-end">
<DialogClose asChild>
<Button type="button" variant="secondary">
@@ -163,7 +265,7 @@ export default function NewListModal() {
</Button>
</DialogClose>
<ActionButton type="submit" loading={isPending}>
- Create
+ {list ? "Save" : "Create"}
</ActionButton>
</DialogFooter>
</form>
diff --git a/apps/web/components/dashboard/sidebar/AllLists.tsx b/apps/web/components/dashboard/sidebar/AllLists.tsx
index 6ab42851..b1c6ddb2 100644
--- a/apps/web/components/dashboard/sidebar/AllLists.tsx
+++ b/apps/web/components/dashboard/sidebar/AllLists.tsx
@@ -1,12 +1,18 @@
"use client";
+import { useCallback } from "react";
import Link from "next/link";
-import { api } from "@/lib/trpc";
-import { Plus } from "lucide-react";
+import { usePathname } from "next/navigation";
+import { Button } from "@/components/ui/button";
+import { CollapsibleTriggerTriangle } from "@/components/ui/collapsible";
+import { MoreHorizontal, Plus } from "lucide-react";
import type { ZBookmarkList } from "@hoarder/shared/types/lists";
+import { ZBookmarkListTreeNode } from "@hoarder/shared/utils/listUtils";
-import NewListModal, { useNewListModal } from "./NewListModal";
+import { CollapsibleBookmarkLists } from "../lists/CollapsibleBookmarkLists";
+import { EditListModal } from "../lists/EditListModal";
+import { ListOptions } from "../lists/ListOptions";
import SidebarItem from "./SidebarItem";
export default function AllLists({
@@ -14,21 +20,20 @@ export default function AllLists({
}: {
initialData: { lists: ZBookmarkList[] };
}) {
- let { data: lists } = api.lists.list.useQuery(undefined, {
- initialData,
- });
- // TODO: This seems to be a bug in react query
- lists ||= initialData;
- const { setOpen } = useNewListModal();
-
+ const pathName = usePathname();
+ const isNodeOpen = useCallback(
+ (node: ZBookmarkListTreeNode) => pathName.includes(node.item.id),
+ [pathName],
+ );
return (
<ul className="max-h-full gap-y-2 overflow-auto text-sm font-medium">
- <NewListModal />
<li className="flex justify-between pb-2 font-bold">
<p>Lists</p>
- <Link href="#" onClick={() => setOpen(true)}>
- <Plus />
- </Link>
+ <EditListModal>
+ <Link href="#">
+ <Plus />
+ </Link>
+ </EditListModal>
</li>
<SidebarItem
logo={<span className="text-lg">📋</span>}
@@ -48,15 +53,45 @@ export default function AllLists({
path={`/dashboard/archive`}
className="py-0.5"
/>
- {lists.lists.map((l) => (
- <SidebarItem
- key={l.id}
- logo={<span className="text-lg"> {l.icon}</span>}
- name={l.name}
- path={`/dashboard/lists/${l.id}`}
- className="py-0.5"
+
+ {
+ <CollapsibleBookmarkLists
+ initialData={initialData.lists}
+ isOpenFunc={isNodeOpen}
+ render={({ item: node, level, open }) => (
+ <SidebarItem
+ collapseButton={
+ node.children.length > 0 && (
+ <CollapsibleTriggerTriangle
+ className="absolute left-0 top-1/2 size-2 -translate-y-1/2"
+ open={open}
+ />
+ )
+ }
+ logo={
+ <span className="flex">
+ <span className="text-lg"> {node.item.icon}</span>
+ </span>
+ }
+ name={node.item.name}
+ path={`/dashboard/lists/${node.item.id}`}
+ right={
+ <ListOptions list={node.item}>
+ <Button
+ size="none"
+ variant="ghost"
+ className="invisible group-hover:visible"
+ >
+ <MoreHorizontal className="size-4" />
+ </Button>
+ </ListOptions>
+ }
+ className="group py-0.5"
+ style={{ marginLeft: `${level * 1}rem` }}
+ />
+ )}
/>
- ))}
+ }
</ul>
);
}
diff --git a/apps/web/components/dashboard/sidebar/SidebarItem.tsx b/apps/web/components/dashboard/sidebar/SidebarItem.tsx
index 7e5eb3bd..262fd9ae 100644
--- a/apps/web/components/dashboard/sidebar/SidebarItem.tsx
+++ b/apps/web/components/dashboard/sidebar/SidebarItem.tsx
@@ -1,5 +1,6 @@
"use client";
+import React from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { cn } from "@/lib/utils";
@@ -9,25 +10,36 @@ export default function SidebarItem({
logo,
path,
className,
+ style,
+ collapseButton,
+ right = null,
}: {
name: string;
logo: React.ReactNode;
path: string;
+ style?: React.CSSProperties;
className?: string;
+ right?: React.ReactNode;
+ collapseButton?: React.ReactNode;
}) {
const currentPath = usePathname();
return (
<li
className={cn(
- "rounded-lg px-3 py-2 hover:bg-accent",
+ "relative rounded-lg px-3 py-2 hover:bg-accent",
path == currentPath ? "bg-accent/50" : "",
className,
)}
+ style={style}
>
- <Link href={path} className="flex w-full gap-x-2">
- {logo}
- <span className="my-auto"> {name} </span>
- </Link>
+ {collapseButton}
+ <div className="flex justify-between">
+ <Link href={path} className="flex w-full gap-x-2">
+ {logo}
+ <span className="my-auto"> {name} </span>
+ </Link>
+ {right}
+ </div>
</li>
);
}