import * as React from "react" import { useAtom } from "jotai" import { atomWithStorage } from "jotai/utils" import { Link } from "@tanstack/react-router" import { useAccount } from "@/lib/providers/jazz-provider" import { cn } from "@/lib/utils" 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) const pageCount = me?.root.personalPages.length || 0 const personalPages = me?.root.personalPages ?? [] const sortedPages = [...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) return (
setIsExpanded(!isExpanded)} /> {isExpanded && (
{sortedPages.map((page, index) => ( {({ isActive }) => (

{page.title || "Untitled"}

)} ))}
)}
) } interface PageSectionHeaderProps { pageCount: number isExpanded: boolean onToggle: () => void } const PageSectionHeader: React.FC = ({ pageCount, isExpanded, onToggle, }) => (
) const NewPageButton: React.FC = () => { const { createNewPage } = usePageActions() const handleClick = async (e: React.MouseEvent) => { e.preventDefault() e.stopPropagation() createNewPage() } return ( ) } 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 ( ) }