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 'apps/web/components/dashboard/sidebar')
-rw-r--r--apps/web/components/dashboard/sidebar/AllLists.tsx79
-rw-r--r--apps/web/components/dashboard/sidebar/NewListModal.tsx174
-rw-r--r--apps/web/components/dashboard/sidebar/SidebarItem.tsx22
3 files changed, 74 insertions, 201 deletions
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/NewListModal.tsx b/apps/web/components/dashboard/sidebar/NewListModal.tsx
deleted file mode 100644
index 5169fbb5..00000000
--- a/apps/web/components/dashboard/sidebar/NewListModal.tsx
+++ /dev/null
@@ -1,174 +0,0 @@
-"use client";
-
-import { ActionButton } from "@/components/ui/action-button";
-import { Button } from "@/components/ui/button";
-import {
- Dialog,
- DialogClose,
- DialogContent,
- DialogFooter,
- DialogHeader,
- DialogTitle,
-} from "@/components/ui/dialog";
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormMessage,
-} from "@/components/ui/form";
-import { Input } from "@/components/ui/input";
-import {
- Popover,
- PopoverContent,
- 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 { 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 })),
-}));
-
-export default function NewListModal() {
- const { open, setOpen } = useNewListModal();
-
- const formSchema = z.object({
- name: z.string(),
- icon: z.string(),
- });
- const form = useForm<z.infer<typeof formSchema>>({
- resolver: zodResolver(formSchema),
- defaultValues: {
- name: "",
- icon: "🚀",
- },
- });
-
- const listsInvalidationFunction = api.useUtils().lists.list.invalidate;
-
- const { mutate: createList, isPending } = api.lists.create.useMutation({
- onSuccess: () => {
- toast({
- description: "List has been created!",
- });
- listsInvalidationFunction();
- 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",
- });
- }
- },
- });
-
- return (
- <Dialog
- open={open}
- onOpenChange={(s) => {
- form.reset();
- setOpen(s);
- }}
- >
- <DialogContent>
- <Form {...form}>
- <form
- onSubmit={form.handleSubmit((value) => {
- createList(value);
- })}
- >
- <DialogHeader>
- <DialogTitle>New List</DialogTitle>
- </DialogHeader>
- <div className="flex w-full gap-2 py-4">
- <FormField
- control={form.control}
- name="icon"
- render={({ field }) => {
- return (
- <FormItem>
- <FormControl>
- <Popover>
- <PopoverTrigger className="h-full rounded border border-input px-2 text-2xl">
- {field.value}
- </PopoverTrigger>
- <PopoverContent>
- <Picker
- data={data}
- onEmojiSelect={(e: { native: string }) =>
- field.onChange(e.native)
- }
- />
- </PopoverContent>
- </Popover>
- </FormControl>
- <FormMessage />
- </FormItem>
- );
- }}
- />
-
- <FormField
- control={form.control}
- name="name"
- render={({ field }) => {
- return (
- <FormItem className="grow">
- <FormControl>
- <Input
- type="text"
- className="w-full"
- placeholder="List Name"
- {...field}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- );
- }}
- />
- </div>
- <DialogFooter className="sm:justify-end">
- <DialogClose asChild>
- <Button type="button" variant="secondary">
- Close
- </Button>
- </DialogClose>
- <ActionButton type="submit" loading={isPending}>
- Create
- </ActionButton>
- </DialogFooter>
- </form>
- </Form>
- </DialogContent>
- </Dialog>
- );
-}
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>
);
}