import React from "react" import { useAtom } from "jotai" import { usePathname, useRouter } from "next/navigation" import { useAccount } from "@/lib/providers/jazz-provider" import { cn } from "@/lib/utils" import { atomWithStorage } from "jotai/utils" import { PersonalPage, PersonalPageLists } from "@/lib/schema/personal-page" import { Button } from "@/components/ui/button" import { LaIcon } from "@/components/custom/la-icon" import { toast } from "sonner" import Link from "next/link" import { useEffect } from "react" import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuPortal, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { icons } from "lucide-react" type SortOption = "title" | "recent" type ShowOption = 5 | 10 | 15 | 20 | 0 interface Option { label: string value: T } const SORTS: Option[] = [ { label: "Title", value: "title" }, { label: "Last edited", value: "recent" } ] const SHOWS: Option[] = [ { label: "5 items", value: 5 }, { label: "10 items", value: 10 }, { label: "15 items", value: 15 }, { label: "20 items", value: 20 }, { label: "All", value: 0 } ] const pageSortAtom = atomWithStorage("pageSort", "title") const pageShowAtom = atomWithStorage("pageShow", 5) export const PageSection: React.FC<{ pathname?: string }> = ({ pathname }) => { const { me } = useAccount({ root: { personalPages: [] } }) const [sort, setSort] = useAtom(pageSortAtom) const [show, setShow] = useAtom(pageShowAtom) const pageCount = me?.root.personalPages?.length || 0 const isActive = pathname === "/pages" if (!me) return null return (
) } interface PageSectionHeaderProps { pageCount: number isActive: boolean } const PageSectionHeader: React.FC = ({ pageCount, isActive }) => (

Pages {pageCount > 0 && {pageCount}}

) const NewPageButton: React.FC = () => { const { me } = useAccount() const router = useRouter() if (!me) return null const handleClick = () => { try { const newPersonalPage = PersonalPage.create( { public: false, createdAt: new Date(), updatedAt: new Date() }, { owner: me._owner } ) me.root?.personalPages?.push(newPersonalPage) router.push(`/pages/${newPersonalPage.id}`) } catch (error) { toast.error("Failed to create page") } } return ( ) } interface PageListProps { personalPages: PersonalPageLists sort: SortOption show: ShowOption } const PageList: React.FC = ({ personalPages }) => { const pathname = usePathname() const [sortCriteria] = useAtom(pageSortAtom) const [showCount] = useAtom(pageShowAtom) const sortedPages = [...personalPages] .sort((a, b) => { switch (sortCriteria) { case "title": return (a?.title ?? "").localeCompare(b?.title ?? "") case "recent": return (b?.updatedAt?.getTime() ?? 0) - (a?.updatedAt?.getTime() ?? 0) default: return 0 } }) .slice(0, showCount === 0 ? personalPages.length : showCount) return (
{sortedPages.map( page => page?.id && )}
) } interface PageListItemProps { page: PersonalPage isActive: boolean } const PageListItem: React.FC = ({ page, isActive }) => (

{page.title || "Untitled"}

) interface SubMenuProps { icon: keyof typeof icons label: string options: Option[] currentValue: T onSelect: (value: T) => void } const SubMenu = ({ icon, label, options, currentValue, onSelect }: SubMenuProps) => ( {label} {options.find(option => option.value === currentValue)?.label} {options.map(option => ( onSelect(option.value)}> {option.label} {currentValue === option.value && } ))} ) const ShowAllForm: React.FC = () => { const [pagesSorted, setPagesSorted] = useAtom(pageSortAtom) const [pagesShow, setPagesShow] = useAtom(pageShowAtom) return ( icon="ArrowUpDown" label="Sort" options={SORTS} currentValue={pagesSorted} onSelect={setPagesSorted} /> icon="Hash" label="Show" options={SHOWS} currentValue={pagesShow} onSelect={setPagesShow} /> ) }