import React, { useMemo } 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 Link from "next/link" import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuPortal, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { icons } from "lucide-react" import { usePageActions } from "@/components/routes/page/hooks/use-page-actions" 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] = useAtom(pageSortAtom) const [show] = useAtom(pageShowAtom) if (!me) return null const pageCount = me.root.personalPages?.length || 0 const isActive = pathname === "/pages" 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() const { newPage } = usePageActions() if (!me) return null const handleClick = () => { const page = newPage(me) router.push(`/pages/${page.id}`) } return ( ) } interface PageListProps { personalPages: PersonalPageLists sort: SortOption show: ShowOption } const PageList: React.FC = ({ personalPages, sort, show }) => { const pathname = usePathname() const sortedPages = useMemo(() => { return [...personalPages] .sort((a, b) => { if (sort === "title") { return (a?.title ?? "").localeCompare(b?.title ?? "") } return (b?.updatedAt?.getTime() ?? 0) - (a?.updatedAt?.getTime() ?? 0) }) .slice(0, show === 0 ? personalPages.length : show) }, [personalPages, sort, show]) 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} /> ) }