From e0999f701cd1834c3d940113cd8dd5247c5fe95f Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Fri, 19 Apr 2024 00:09:27 +0100 Subject: feature: Nested lists (#110). Fixes #62 * feature: Add support for nested lists * prevent moving the parent to a subtree --- apps/web/components/dashboard/sidebar/AllLists.tsx | 79 +++++++--- .../components/dashboard/sidebar/NewListModal.tsx | 174 --------------------- .../components/dashboard/sidebar/SidebarItem.tsx | 22 ++- 3 files changed, 74 insertions(+), 201 deletions(-) delete mode 100644 apps/web/components/dashboard/sidebar/NewListModal.tsx (limited to 'apps/web/components/dashboard/sidebar') 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 ( ); } 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>({ - 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 ( - { - form.reset(); - setOpen(s); - }} - > - -
- { - createList(value); - })} - > - - New List - -
- { - return ( - - - - - {field.value} - - - - field.onChange(e.native) - } - /> - - - - - - ); - }} - /> - - { - return ( - - - - - - - ); - }} - /> -
- - - - - - Create - - -
- -
-
- ); -} 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 (
  • - - {logo} - {name} - + {collapseButton} +
    + + {logo} + {name} + + {right} +
  • ); } -- cgit v1.2.3-70-g09d2