import { createContext, useContext } from 'react'; import { Link } from '@tanstack/react-router'; import { FolderOpen } from 'lucide-react'; import { useCurrentServerIdentifier } from '@/api/server/queries'; import { fileObjectKey, type FileObject } from '@/api/server/files/queries'; import { NewButton } from '@/components/elements/NewButton'; import ErrorBoundary from '@/components/elements/ErrorBoundary'; import Spinner from '@/components/elements/Spinner'; import Checkbox from '@/components/ui/Checkbox'; import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from '@/components/ui/empty'; import FileManagerBreadcrumbs from '@/components/server/files/FileManagerBreadcrumbs'; import FileManagerStatus from '@/components/server/files/FileManagerStatus'; import FileObjectRow from '@/components/server/files/FileObjectRow'; import MassActionsBar from '@/components/server/files/MassActionsBar'; import NewDirectoryButton from '@/components/server/files/NewDirectoryButton'; import UploadButton from '@/components/server/files/UploadButton'; import { FileRowActionsProvider } from '@/components/server/files/useFileActions'; import Slot from '@/extensions/Slot'; import { encodePathSegments } from '@/helpers'; import { cn } from '@/lib/cn'; import type { ComponentPartProps, ComponentParts, DefaultComponentProps, FileManagerModel, } from '@/extensions/componentTypes'; import useFileManagerExtensionData from './useFileManagerExtensionData'; export interface FileManagerSession { model: FileManagerModel; files: readonly FileObject[]; } export const FileManagerContext = createContext(null); const managerActionsClass = [ 'grid grid-cols-2 sm:grid-cols-3 w-full gap-4 mb-4', 'md:flex md:flex-1 md:justify-end md:mb-0', '[&_button]:w-full [&_button:first-child]:col-span-2 sm:[&_button:first-child]:col-span-1 md:[&_button]:w-auto', ].join(' '); type PartProps = ComponentPartProps<'server.files.manager'>; function Toolbar({ model }: PartProps) { const id = useCurrentServerIdentifier()!; const extensionData = useFileManagerExtensionData(); return (
0 && model.selection.length === model.entries.length} indeterminate={model.selection.length > 0 && model.selection.length < model.entries.length} onChange={(checked) => model.actions.select(checked ? model.entries.map((entry) => entry.name) : []) } /> {model.permissions.create && (
{model.permissions.update && } New file
)}
{extensionData && }
); } function List({ model }: PartProps) { const { files } = useContext(FileManagerContext)!; if (model.loading) { return ; } if (!files.length) { return ( This folder is empty {model.permissions.create ? 'Upload files or create a new file or folder to get started.' : 'There are no files in this folder.'} ); } return (
{model.truncated && (

This directory is too large to display in the browser, limiting output to first 250 files.

)} {files.map((file) => ( ))}
); } function Selection({ model }: PartProps) { return model.entries.length > 0 ? : null; } export const fileManagerParts: ComponentParts<'server.files.manager'> = { toolbar: Toolbar, list: List, selection: Selection, }; export function DefaultFileManager({ className, parts }: DefaultComponentProps<'server.files.manager'>) { const { model } = useContext(FileManagerContext)!; const ToolbarPart = parts?.toolbar ?? Toolbar; const ListPart = parts?.list ?? List; const SelectionPart = parts?.selection ?? Selection; return (
); }