import * as React from "react" import { useAtom } from "jotai" import { atomWithStorage } from "jotai/utils" import { Link, useNavigate } from "@tanstack/react-router" import { useAccount } from "@/lib/providers/jazz-provider" import { cn } from "@/lib/utils" import { PersonalPage, PersonalPageLists } from "@/lib/schema/personal-page" import { Button } from "@/components/ui/button" import { LaIcon } from "@/components/custom/la-icon" import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuPortal, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { usePageActions } from "~/hooks/actions/use-page-actions" import { icons } from "lucide-react" import { ArrowIcon } from "~/components/icons/arrow-icon" 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) const isExpandedAtom = atomWithStorage("isPageSectionExpanded", true) export const PageSection: React.FC = () => { const { me } = useAccount({ root: { personalPages: [] } }) const [sort] = useAtom(pageSortAtom) const [show] = useAtom(pageShowAtom) const [isExpanded, setIsExpanded] = useAtom(isExpandedAtom) if (!me) return null const pageCount = me.root.personalPages?.length || 0 return (
setIsExpanded(!isExpanded)} /> {isExpanded && ( )}
) } interface PageSectionHeaderProps { pageCount: number isExpanded: boolean onToggle: () => void } const PageSectionHeader: React.FC = ({ pageCount, isExpanded, onToggle, }) => (
) const NewPageButton: React.FC = () => { const { me } = useAccount() const navigate = useNavigate() const { newPage } = usePageActions() const handleClick = async (e: React.MouseEvent) => { e.preventDefault() e.stopPropagation() const page = newPage(me) if (page.id) { navigate({ to: "/pages/$pageId", params: { pageId: page.id }, replace: true, }) } } return ( ) } interface PageListProps { personalPages: PersonalPageLists sort: SortOption show: ShowOption } const PageList: React.FC = ({ personalPages, sort, show }) => { const sortedPages = React.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 } const PageListItem: React.FC = ({ page }) => { return ( {({ 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 ( ) }