2025-12-21 01:48:25 +00:00
|
|
|
// TODO: make table sorting work (right now not working probably because different params share same query key)
|
|
|
|
|
|
|
|
|
|
import { useInfiniteQuery, useMutation } from "@tanstack/react-query"
|
2025-12-18 01:24:35 +00:00
|
|
|
import { Link, useNavigate } from "@tanstack/react-router"
|
2025-09-19 23:01:44 +00:00
|
|
|
import {
|
|
|
|
|
type ColumnDef,
|
|
|
|
|
flexRender,
|
|
|
|
|
getCoreRowModel,
|
2025-10-18 22:58:23 +00:00
|
|
|
getFilteredRowModel,
|
2025-09-19 23:01:44 +00:00
|
|
|
type Row,
|
2025-09-21 17:03:50 +00:00
|
|
|
type Table as TableType,
|
2025-09-19 23:01:44 +00:00
|
|
|
useReactTable,
|
|
|
|
|
} from "@tanstack/react-table"
|
2025-12-18 00:47:59 +00:00
|
|
|
import { useVirtualizer, type VirtualItem } from "@tanstack/react-virtual"
|
2025-12-21 01:48:25 +00:00
|
|
|
import { type PrimitiveAtom, useAtomValue, useSetAtom, useStore } from "jotai"
|
|
|
|
|
import { ArrowDownIcon, ArrowUpIcon } from "lucide-react"
|
2025-12-18 00:47:59 +00:00
|
|
|
import type React from "react"
|
2025-12-21 01:48:25 +00:00
|
|
|
import { createContext, useContext, useEffect, useMemo, useRef } from "react"
|
|
|
|
|
import { toast } from "sonner"
|
2025-09-19 23:01:44 +00:00
|
|
|
import { DirectoryIcon } from "@/components/icons/directory-icon"
|
2025-12-15 00:13:10 +00:00
|
|
|
import { TextFileIcon } from "@/components/icons/text-file-icon"
|
2025-09-19 23:01:44 +00:00
|
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
|
|
|
|
import {
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
2025-12-18 00:47:59 +00:00
|
|
|
TableContainer,
|
2025-09-19 23:01:44 +00:00
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableRow,
|
|
|
|
|
} from "@/components/ui/table"
|
2025-12-15 00:13:10 +00:00
|
|
|
import { type FileDragInfo, useFileDrop } from "@/files/use-file-drop"
|
2025-09-21 15:12:05 +00:00
|
|
|
import {
|
|
|
|
|
isControlOrCommandKeyActive,
|
|
|
|
|
keyboardModifierAtom,
|
|
|
|
|
} from "@/lib/keyboard"
|
2025-12-15 00:13:10 +00:00
|
|
|
import { cn } from "@/lib/utils"
|
2025-12-21 01:48:25 +00:00
|
|
|
import {
|
|
|
|
|
DIRECTORY_CONTENT_ORDER_BY,
|
|
|
|
|
DIRECTORY_CONTENT_ORDER_DIRECTION,
|
|
|
|
|
type DirectoryContentOrderBy,
|
|
|
|
|
type DirectoryContentOrderDirection,
|
|
|
|
|
type DirectoryContentQuery,
|
|
|
|
|
type MoveDirectoryItemsResult,
|
|
|
|
|
moveDirectoryItemsMutationAtom,
|
|
|
|
|
} from "@/vfs/api"
|
2025-12-15 00:13:10 +00:00
|
|
|
import type { DirectoryInfo, DirectoryItem, FileInfo } from "@/vfs/vfs"
|
2025-12-21 01:48:25 +00:00
|
|
|
import {
|
|
|
|
|
DEFAULT_DIRECTORY_CONTENT_ORDER_BY,
|
|
|
|
|
DEFAULT_DIRECTORY_CONTENT_ORDER_DIRECTION,
|
|
|
|
|
} from "./defaults"
|
2025-12-17 22:59:18 +00:00
|
|
|
import { DirectoryContentTableSkeleton } from "./directory-content-table-skeleton"
|
2025-10-04 14:09:25 +00:00
|
|
|
|
2025-12-15 00:13:10 +00:00
|
|
|
type DirectoryContentTableItemIdFilter = Set<string>
|
2025-10-18 22:58:23 +00:00
|
|
|
|
2025-12-21 01:48:25 +00:00
|
|
|
export type DirectoryContentTableSortChangeCallback = (
|
|
|
|
|
orderBy: DirectoryContentOrderBy,
|
|
|
|
|
direction: DirectoryContentOrderDirection,
|
|
|
|
|
) => void
|
|
|
|
|
|
2025-12-18 01:24:35 +00:00
|
|
|
export type DirectoryContentTableProps = {
|
2025-12-21 01:48:25 +00:00
|
|
|
query: DirectoryContentQuery
|
2025-12-15 00:13:10 +00:00
|
|
|
directoryUrlFn: (directory: DirectoryInfo) => string
|
2025-10-04 14:09:25 +00:00
|
|
|
fileDragInfoAtom: PrimitiveAtom<FileDragInfo | null>
|
2025-12-21 01:48:25 +00:00
|
|
|
loadingComponent?: React.ReactNode
|
|
|
|
|
debugBanner?: React.ReactNode
|
|
|
|
|
|
2025-10-04 14:56:53 +00:00
|
|
|
onContextMenu: (
|
2025-12-15 00:13:10 +00:00
|
|
|
row: Row<DirectoryItem>,
|
|
|
|
|
table: TableType<DirectoryItem>,
|
2025-10-04 14:56:53 +00:00
|
|
|
) => void
|
2025-12-15 00:13:10 +00:00
|
|
|
onOpenFile: (file: FileInfo) => void
|
2025-12-21 01:48:25 +00:00
|
|
|
onSortChange: DirectoryContentTableSortChangeCallback
|
2025-10-04 14:09:25 +00:00
|
|
|
}
|
2025-09-19 23:01:44 +00:00
|
|
|
|
2025-12-21 01:48:25 +00:00
|
|
|
export type DirectoryContentTableContext = {
|
|
|
|
|
orderBy: DirectoryContentOrderBy
|
|
|
|
|
direction: DirectoryContentOrderDirection
|
|
|
|
|
onSortChange: DirectoryContentTableSortChangeCallback
|
|
|
|
|
}
|
|
|
|
|
const DirectoryContentTableContext =
|
|
|
|
|
createContext<DirectoryContentTableContext>({
|
|
|
|
|
orderBy: DEFAULT_DIRECTORY_CONTENT_ORDER_BY,
|
|
|
|
|
direction: DEFAULT_DIRECTORY_CONTENT_ORDER_DIRECTION,
|
|
|
|
|
onSortChange: () => {},
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-19 23:01:44 +00:00
|
|
|
function formatFileSize(bytes: number): string {
|
|
|
|
|
if (bytes === 0) return "0 B"
|
|
|
|
|
|
|
|
|
|
const k = 1024
|
|
|
|
|
const sizes = ["B", "KB", "MB", "GB", "TB", "PB"]
|
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
|
|
|
|
|
|
|
|
return `${parseFloat((bytes / k ** i).toFixed(2))} ${sizes[i]}`
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 14:09:25 +00:00
|
|
|
function useTableColumns(
|
2025-12-15 00:13:10 +00:00
|
|
|
onOpenFile: (file: FileInfo) => void,
|
|
|
|
|
directoryUrlFn: (directory: DirectoryInfo) => string,
|
|
|
|
|
): ColumnDef<DirectoryItem>[] {
|
2025-10-04 14:09:25 +00:00
|
|
|
return useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
{
|
|
|
|
|
id: "select",
|
|
|
|
|
header: ({ table }) => (
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={table.getIsAllPageRowsSelected()}
|
|
|
|
|
onCheckedChange={(value) => {
|
|
|
|
|
table.toggleAllPageRowsSelected(!!value)
|
|
|
|
|
}}
|
|
|
|
|
aria-label="Select all"
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
cell: ({ row }) => (
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={row.getIsSelected()}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}}
|
|
|
|
|
onCheckedChange={row.getToggleSelectedHandler()}
|
|
|
|
|
aria-label="Select row"
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
enableSorting: false,
|
|
|
|
|
enableHiding: false,
|
|
|
|
|
size: 24,
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-12-21 01:48:25 +00:00
|
|
|
header: () => <NameHeaderCell />,
|
2025-10-04 14:09:25 +00:00
|
|
|
accessorKey: "doc.name",
|
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
switch (row.original.kind) {
|
2025-12-15 00:13:10 +00:00
|
|
|
case "file":
|
2025-10-04 14:09:25 +00:00
|
|
|
return (
|
|
|
|
|
<FileNameCell
|
2025-12-15 00:13:10 +00:00
|
|
|
file={row.original}
|
2025-10-04 14:09:25 +00:00
|
|
|
onOpenFile={onOpenFile}
|
|
|
|
|
/>
|
|
|
|
|
)
|
2025-12-15 00:13:10 +00:00
|
|
|
case "directory":
|
2025-10-04 14:09:25 +00:00
|
|
|
return (
|
|
|
|
|
<DirectoryNameCell
|
2025-12-15 00:13:10 +00:00
|
|
|
directory={row.original}
|
2025-10-05 00:41:59 +00:00
|
|
|
directoryUrlFn={directoryUrlFn}
|
2025-10-04 14:09:25 +00:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
size: 1000,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: "Size",
|
|
|
|
|
accessorKey: "size",
|
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
switch (row.original.kind) {
|
2025-12-15 00:13:10 +00:00
|
|
|
case "file":
|
2025-10-04 14:09:25 +00:00
|
|
|
return (
|
2025-12-15 00:13:10 +00:00
|
|
|
<div>{formatFileSize(row.original.size)}</div>
|
2025-10-04 14:09:25 +00:00
|
|
|
)
|
2025-12-15 00:13:10 +00:00
|
|
|
case "directory":
|
2025-10-04 14:09:25 +00:00
|
|
|
return <div className="font-mono">-</div>
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: "Created At",
|
|
|
|
|
accessorKey: "createdAt",
|
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2025-12-15 00:13:10 +00:00
|
|
|
{new Date(row.original.createdAt).toLocaleString()}
|
2025-10-04 14:09:25 +00:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
2025-10-05 00:41:59 +00:00
|
|
|
[onOpenFile, directoryUrlFn],
|
2025-10-04 14:09:25 +00:00
|
|
|
)
|
|
|
|
|
}
|
2025-09-19 23:01:44 +00:00
|
|
|
|
2025-10-04 14:56:53 +00:00
|
|
|
export function DirectoryContentTable({
|
2025-10-05 00:41:59 +00:00
|
|
|
directoryUrlFn,
|
2025-10-04 14:09:25 +00:00
|
|
|
fileDragInfoAtom,
|
2025-12-18 01:24:35 +00:00
|
|
|
query,
|
|
|
|
|
loadingComponent,
|
|
|
|
|
debugBanner,
|
2025-12-21 01:48:25 +00:00
|
|
|
onOpenFile,
|
|
|
|
|
onContextMenu,
|
|
|
|
|
onSortChange,
|
2025-10-04 14:09:25 +00:00
|
|
|
}: DirectoryContentTableProps) {
|
2025-12-18 00:47:59 +00:00
|
|
|
const {
|
|
|
|
|
data: directoryContent,
|
|
|
|
|
isLoading: isLoadingDirectoryContent,
|
|
|
|
|
isFetchingNextPage: isFetchingMoreDirectoryItems,
|
|
|
|
|
fetchNextPage: fetchMoreDirectoryItems,
|
|
|
|
|
hasNextPage: hasMoreDirectoryItems,
|
2025-12-18 01:24:35 +00:00
|
|
|
} = useInfiniteQuery(query)
|
2025-12-17 22:59:18 +00:00
|
|
|
|
2025-09-19 23:01:44 +00:00
|
|
|
const store = useStore()
|
2025-09-21 15:18:32 +00:00
|
|
|
const navigate = useNavigate()
|
2025-12-21 01:48:25 +00:00
|
|
|
const moveDirectoryItemsMutation = useAtomValue(
|
|
|
|
|
moveDirectoryItemsMutationAtom,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const { mutate: moveDroppedItems } = useMutation({
|
|
|
|
|
...moveDirectoryItemsMutation,
|
|
|
|
|
onSuccess: (data: MoveDirectoryItemsResult) => {
|
|
|
|
|
const conflictCount = data.errors.length
|
|
|
|
|
if (conflictCount > 0) {
|
|
|
|
|
toast.warning(
|
|
|
|
|
`${data.moved.length} items moved${conflictCount > 0 ? `, ${conflictCount} conflicts` : ""}`,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
toast.success(`${data.moved.length} items moved!`)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const handleFileDrop = (
|
|
|
|
|
items: import("@/vfs/vfs").DirectoryItem[],
|
|
|
|
|
targetDirectory: import("@/vfs/vfs").DirectoryInfo | string,
|
|
|
|
|
) => {
|
|
|
|
|
moveDroppedItems({
|
|
|
|
|
targetDirectory,
|
|
|
|
|
items,
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-09-19 23:01:44 +00:00
|
|
|
|
2025-09-26 22:28:51 +00:00
|
|
|
const table = useReactTable({
|
2025-12-17 22:59:18 +00:00
|
|
|
data: useMemo(
|
2025-12-18 01:24:35 +00:00
|
|
|
() =>
|
|
|
|
|
directoryContent?.pages.flatMap(
|
|
|
|
|
(page) => (page as { items: DirectoryItem[] }).items,
|
|
|
|
|
) || [],
|
2025-12-17 22:59:18 +00:00
|
|
|
[directoryContent],
|
|
|
|
|
),
|
2025-10-05 00:41:59 +00:00
|
|
|
columns: useTableColumns(onOpenFile, directoryUrlFn),
|
2025-09-26 22:28:51 +00:00
|
|
|
getCoreRowModel: getCoreRowModel(),
|
2025-10-18 22:58:23 +00:00
|
|
|
getFilteredRowModel: getFilteredRowModel(),
|
2025-09-26 22:28:51 +00:00
|
|
|
enableRowSelection: true,
|
|
|
|
|
enableGlobalFilter: true,
|
2025-10-18 22:58:23 +00:00
|
|
|
globalFilterFn: (
|
|
|
|
|
row,
|
|
|
|
|
_columnId,
|
|
|
|
|
filterValue: DirectoryContentTableItemIdFilter,
|
|
|
|
|
_addMeta,
|
2025-12-15 00:13:10 +00:00
|
|
|
) => !filterValue.has(row.original.id),
|
|
|
|
|
getRowId: (row) => row.id,
|
2025-09-26 22:28:51 +00:00
|
|
|
})
|
2025-12-18 00:47:59 +00:00
|
|
|
const { rows } = table.getRowModel()
|
|
|
|
|
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
const virtualizer = useVirtualizer({
|
|
|
|
|
count: rows.length,
|
|
|
|
|
getScrollElement: () => containerRef.current,
|
|
|
|
|
estimateSize: () => 36,
|
|
|
|
|
overscan: 20,
|
|
|
|
|
})
|
|
|
|
|
const virtualItems = virtualizer.getVirtualItems()
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const lastVirtualItem = virtualItems.at(-1)
|
|
|
|
|
if (
|
|
|
|
|
lastVirtualItem &&
|
|
|
|
|
lastVirtualItem.index >= rows.length - 1 &&
|
|
|
|
|
hasMoreDirectoryItems &&
|
|
|
|
|
!isFetchingMoreDirectoryItems
|
|
|
|
|
) {
|
|
|
|
|
fetchMoreDirectoryItems()
|
|
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
virtualItems,
|
|
|
|
|
rows.length,
|
|
|
|
|
hasMoreDirectoryItems,
|
|
|
|
|
isFetchingMoreDirectoryItems,
|
|
|
|
|
fetchMoreDirectoryItems,
|
|
|
|
|
])
|
2025-09-26 22:28:51 +00:00
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
|
function escapeToClearSelections() {
|
|
|
|
|
const handleEscape = (e: KeyboardEvent) => {
|
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
|
table.setRowSelection({})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
window.addEventListener("keydown", handleEscape)
|
|
|
|
|
return () => window.removeEventListener("keydown", handleEscape)
|
|
|
|
|
},
|
|
|
|
|
[table.setRowSelection],
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-17 22:59:18 +00:00
|
|
|
if (isLoadingDirectoryContent) {
|
2025-12-18 01:24:35 +00:00
|
|
|
return <>{loadingComponent || <DirectoryContentTableSkeleton />}</>
|
2025-12-17 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 23:01:44 +00:00
|
|
|
const handleRowContextMenu = (
|
2025-12-15 00:13:10 +00:00
|
|
|
row: Row<DirectoryItem>,
|
2025-09-19 23:01:44 +00:00
|
|
|
_event: React.MouseEvent,
|
|
|
|
|
) => {
|
2025-10-04 14:56:53 +00:00
|
|
|
if (!row.getIsSelected()) {
|
2025-09-19 23:01:44 +00:00
|
|
|
selectRow(row)
|
|
|
|
|
}
|
2025-10-04 14:56:53 +00:00
|
|
|
onContextMenu(row, table)
|
2025-09-19 23:01:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-15 00:13:10 +00:00
|
|
|
const selectRow = (row: Row<DirectoryItem>) => {
|
2025-09-21 15:12:05 +00:00
|
|
|
const keyboardModifiers = store.get(keyboardModifierAtom)
|
|
|
|
|
const isMultiSelectMode = isControlOrCommandKeyActive(keyboardModifiers)
|
|
|
|
|
const isRowSelected = row.getIsSelected()
|
|
|
|
|
if (isRowSelected && isMultiSelectMode) {
|
|
|
|
|
row.toggleSelected(false)
|
|
|
|
|
} else if (isRowSelected && !isMultiSelectMode) {
|
|
|
|
|
table.setRowSelection({
|
|
|
|
|
[row.id]: true,
|
|
|
|
|
})
|
2025-09-19 23:01:44 +00:00
|
|
|
row.toggleSelected(true)
|
2025-09-21 15:12:05 +00:00
|
|
|
} else if (!isRowSelected) {
|
|
|
|
|
if (isMultiSelectMode) {
|
|
|
|
|
row.toggleSelected(true)
|
|
|
|
|
} else {
|
|
|
|
|
table.setRowSelection({
|
|
|
|
|
[row.id]: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-09-19 23:01:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 00:13:10 +00:00
|
|
|
const handleRowDoubleClick = (row: Row<DirectoryItem>) => {
|
|
|
|
|
if (row.original.kind === "directory") {
|
2025-09-21 15:18:32 +00:00
|
|
|
navigate({
|
2025-12-15 00:13:10 +00:00
|
|
|
to: `/directories/${row.original.id}`,
|
2025-09-21 15:18:32 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 00:47:59 +00:00
|
|
|
const renderRow = (virtualRow: VirtualItem, i: number) => {
|
2025-12-18 01:24:35 +00:00
|
|
|
const row = rows[virtualRow.index]
|
|
|
|
|
if (!row) return null
|
2025-12-18 00:47:59 +00:00
|
|
|
return (
|
|
|
|
|
<FileItemRow
|
|
|
|
|
style={{
|
|
|
|
|
height: virtualRow.size,
|
|
|
|
|
transform: `translateY(${
|
|
|
|
|
virtualRow.start - i * virtualRow.size
|
|
|
|
|
}px)`,
|
|
|
|
|
}}
|
|
|
|
|
key={row.id}
|
|
|
|
|
table={table}
|
|
|
|
|
row={row}
|
|
|
|
|
onClick={() => selectRow(row)}
|
|
|
|
|
fileDragInfoAtom={fileDragInfoAtom}
|
|
|
|
|
onContextMenu={(e) => handleRowContextMenu(row, e)}
|
|
|
|
|
onDoubleClick={() => {
|
|
|
|
|
handleRowDoubleClick(row)
|
|
|
|
|
}}
|
2025-12-21 01:48:25 +00:00
|
|
|
onFileDrop={handleFileDrop}
|
2025-12-18 00:47:59 +00:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 23:01:44 +00:00
|
|
|
return (
|
2025-12-21 01:48:25 +00:00
|
|
|
<DirectoryContentTableContext
|
|
|
|
|
value={{
|
|
|
|
|
orderBy: query.initialPageParam.orderBy,
|
|
|
|
|
direction: query.initialPageParam.direction,
|
|
|
|
|
onSortChange,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div className="h-full flex flex-col">
|
|
|
|
|
{debugBanner}
|
|
|
|
|
<TableContainer
|
|
|
|
|
className={debugBanner ? "flex-1" : "h-full"}
|
|
|
|
|
ref={containerRef}
|
|
|
|
|
>
|
|
|
|
|
<Table className="h-full min-h-0">
|
|
|
|
|
<TableHeader>
|
|
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
|
<TableRow
|
|
|
|
|
className="px-4 border-b-0!"
|
|
|
|
|
key={headerGroup.id}
|
|
|
|
|
>
|
|
|
|
|
{headerGroup.headers.map((header) => (
|
|
|
|
|
<TableHead
|
|
|
|
|
className="first:pl-4 last:pr-4 sticky top-0 bg-background z-1 inset-shadow-[0_-1px_0_0_var(--border)]"
|
|
|
|
|
key={header.id}
|
|
|
|
|
style={{ width: header.getSize() }}
|
|
|
|
|
>
|
|
|
|
|
{header.isPlaceholder
|
|
|
|
|
? null
|
|
|
|
|
: flexRender(
|
|
|
|
|
header.column.columnDef
|
|
|
|
|
.header,
|
|
|
|
|
header.getContext(),
|
|
|
|
|
)}
|
|
|
|
|
</TableHead>
|
|
|
|
|
))}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody className="overflow-auto">
|
|
|
|
|
{rows.length > 0 ? (
|
|
|
|
|
virtualItems.map(renderRow)
|
|
|
|
|
) : (
|
|
|
|
|
<NoResultsRow />
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</TableContainer>
|
|
|
|
|
</div>
|
|
|
|
|
</DirectoryContentTableContext>
|
2025-09-19 23:01:44 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function NoResultsRow() {
|
|
|
|
|
return (
|
2025-10-05 00:46:29 +00:00
|
|
|
<TableRow className="hover:bg-transparent">
|
2025-10-04 14:09:25 +00:00
|
|
|
<TableCell colSpan={4} className="text-center">
|
2025-09-19 23:01:44 +00:00
|
|
|
No results.
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 22:25:01 +00:00
|
|
|
function FileItemRow({
|
2025-09-21 17:03:50 +00:00
|
|
|
table,
|
2025-09-20 22:25:01 +00:00
|
|
|
row,
|
|
|
|
|
onClick,
|
|
|
|
|
onContextMenu,
|
2025-09-21 15:18:32 +00:00
|
|
|
onDoubleClick,
|
2025-10-04 14:09:25 +00:00
|
|
|
fileDragInfoAtom,
|
2025-12-21 01:48:25 +00:00
|
|
|
onFileDrop,
|
2025-12-18 00:47:59 +00:00
|
|
|
...rowProps
|
|
|
|
|
}: React.ComponentProps<typeof TableRow> & {
|
2025-12-15 00:13:10 +00:00
|
|
|
table: TableType<DirectoryItem>
|
|
|
|
|
row: Row<DirectoryItem>
|
2025-09-20 22:25:01 +00:00
|
|
|
onClick: () => void
|
|
|
|
|
onContextMenu: (e: React.MouseEvent) => void
|
2025-09-21 15:18:32 +00:00
|
|
|
onDoubleClick: () => void
|
2025-10-04 14:09:25 +00:00
|
|
|
fileDragInfoAtom: PrimitiveAtom<FileDragInfo | null>
|
2025-12-21 01:48:25 +00:00
|
|
|
onFileDrop: (
|
|
|
|
|
items: import("@/vfs/vfs").DirectoryItem[],
|
|
|
|
|
targetDirectory: import("@/vfs/vfs").DirectoryInfo | string,
|
|
|
|
|
) => void
|
2025-09-20 22:25:01 +00:00
|
|
|
}) {
|
|
|
|
|
const ref = useRef<HTMLTableRowElement>(null)
|
2025-10-04 14:09:25 +00:00
|
|
|
const setFileDragInfo = useSetAtom(fileDragInfoAtom)
|
2025-09-20 22:25:01 +00:00
|
|
|
|
2025-09-20 22:43:31 +00:00
|
|
|
const { isDraggedOver, dropHandlers } = useFileDrop({
|
2025-12-15 22:45:32 +00:00
|
|
|
enabled: row.original.kind === "directory",
|
|
|
|
|
destDir: row.original.kind === "directory" ? row.original : undefined,
|
2025-10-04 14:09:25 +00:00
|
|
|
dragInfoAtom: fileDragInfoAtom,
|
2025-12-21 01:48:25 +00:00
|
|
|
onDrop: onFileDrop,
|
2025-09-20 22:25:01 +00:00
|
|
|
})
|
|
|
|
|
|
2025-09-28 15:45:49 +00:00
|
|
|
const handleDragStart = (_e: React.DragEvent) => {
|
2025-12-15 00:13:10 +00:00
|
|
|
let draggedItems: DirectoryItem[]
|
2025-09-26 22:20:30 +00:00
|
|
|
// drag all selections, but only if the currently dragged row is also selected
|
|
|
|
|
if (row.getIsSelected()) {
|
2025-12-15 22:45:32 +00:00
|
|
|
draggedItems = []
|
|
|
|
|
let currentRowFound = false
|
|
|
|
|
for (const { original: item } of table.getSelectedRowModel().rows) {
|
|
|
|
|
draggedItems.push(item)
|
|
|
|
|
if (item.id === row.original.id) {
|
|
|
|
|
currentRowFound = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!currentRowFound) {
|
2025-12-15 00:13:10 +00:00
|
|
|
draggedItems.push(row.original)
|
2025-09-21 17:03:50 +00:00
|
|
|
}
|
2025-09-26 22:20:30 +00:00
|
|
|
} else {
|
2025-12-15 00:13:10 +00:00
|
|
|
draggedItems = [row.original]
|
2025-09-26 22:20:30 +00:00
|
|
|
}
|
2025-09-21 17:03:50 +00:00
|
|
|
|
2025-10-04 14:09:25 +00:00
|
|
|
setFileDragInfo({
|
2025-12-15 00:13:10 +00:00
|
|
|
source: row.original,
|
2025-09-21 17:03:50 +00:00
|
|
|
items: draggedItems,
|
|
|
|
|
})
|
2025-09-20 22:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 22:43:31 +00:00
|
|
|
const handleDragEnd = () => {
|
2025-10-04 14:09:25 +00:00
|
|
|
setFileDragInfo(null)
|
2025-09-20 22:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TableRow
|
|
|
|
|
draggable
|
|
|
|
|
ref={ref}
|
|
|
|
|
key={row.id}
|
|
|
|
|
data-state={row.getIsSelected() && "selected"}
|
|
|
|
|
onClick={onClick}
|
2025-09-21 15:18:32 +00:00
|
|
|
onDoubleClick={onDoubleClick}
|
2025-09-20 22:25:01 +00:00
|
|
|
onContextMenu={onContextMenu}
|
2025-09-20 22:43:31 +00:00
|
|
|
onDragStart={handleDragStart}
|
|
|
|
|
onDragEnd={handleDragEnd}
|
|
|
|
|
{...dropHandlers}
|
2025-09-20 22:25:01 +00:00
|
|
|
className={cn({ "bg-muted": isDraggedOver })}
|
2025-12-18 00:47:59 +00:00
|
|
|
{...rowProps}
|
2025-09-20 22:25:01 +00:00
|
|
|
>
|
|
|
|
|
{row.getVisibleCells().map((cell) => (
|
|
|
|
|
<TableCell
|
|
|
|
|
className="first:pl-4 last:pr-4"
|
|
|
|
|
key={cell.id}
|
|
|
|
|
style={{ width: cell.column.getSize() }}
|
|
|
|
|
>
|
|
|
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
|
|
|
</TableCell>
|
|
|
|
|
))}
|
|
|
|
|
</TableRow>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 01:48:25 +00:00
|
|
|
function NameHeaderCell() {
|
|
|
|
|
const { orderBy, direction, onSortChange } = useContext(
|
|
|
|
|
DirectoryContentTableContext,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
let arrow: React.ReactNode = null
|
|
|
|
|
if (orderBy === DIRECTORY_CONTENT_ORDER_BY.name) {
|
|
|
|
|
switch (direction) {
|
|
|
|
|
case DIRECTORY_CONTENT_ORDER_DIRECTION.asc:
|
|
|
|
|
arrow = <ArrowUpIcon className="size-4" />
|
|
|
|
|
break
|
|
|
|
|
case DIRECTORY_CONTENT_ORDER_DIRECTION.desc:
|
|
|
|
|
arrow = <ArrowDownIcon className="size-4" />
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let directionLabel: string
|
|
|
|
|
switch (direction) {
|
|
|
|
|
case DIRECTORY_CONTENT_ORDER_DIRECTION.asc:
|
|
|
|
|
directionLabel = "ascending"
|
|
|
|
|
break
|
|
|
|
|
case DIRECTORY_CONTENT_ORDER_DIRECTION.desc:
|
|
|
|
|
directionLabel = "descending"
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
className="hover:underline cursor-pointer flex items-center gap-2 w-full"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
onSortChange(
|
|
|
|
|
DIRECTORY_CONTENT_ORDER_BY.name,
|
|
|
|
|
direction === DIRECTORY_CONTENT_ORDER_DIRECTION.asc
|
|
|
|
|
? DIRECTORY_CONTENT_ORDER_DIRECTION.desc
|
|
|
|
|
: DIRECTORY_CONTENT_ORDER_DIRECTION.asc,
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<span className="sr-only">Sort by</span>
|
|
|
|
|
<span>Name</span>
|
|
|
|
|
<span className="sr-only">in {directionLabel} order</span>
|
|
|
|
|
{arrow}
|
|
|
|
|
</button>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 00:41:59 +00:00
|
|
|
function DirectoryNameCell({
|
|
|
|
|
directory,
|
|
|
|
|
directoryUrlFn,
|
|
|
|
|
}: {
|
2025-12-15 00:13:10 +00:00
|
|
|
directory: DirectoryInfo
|
|
|
|
|
directoryUrlFn: (directory: DirectoryInfo) => string
|
2025-10-05 00:41:59 +00:00
|
|
|
}) {
|
2025-09-19 23:01:44 +00:00
|
|
|
return (
|
|
|
|
|
<div className="flex w-full items-center gap-2">
|
|
|
|
|
<DirectoryIcon className="size-4" />
|
2025-10-05 00:41:59 +00:00
|
|
|
<Link className="hover:underline" to={directoryUrlFn(directory)}>
|
2025-09-19 23:01:44 +00:00
|
|
|
{directory.name}
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 14:09:25 +00:00
|
|
|
function FileNameCell({
|
|
|
|
|
file,
|
|
|
|
|
onOpenFile,
|
|
|
|
|
}: {
|
2025-12-15 00:13:10 +00:00
|
|
|
file: FileInfo
|
|
|
|
|
onOpenFile: (file: FileInfo) => void
|
2025-10-04 14:09:25 +00:00
|
|
|
}) {
|
2025-09-19 23:01:44 +00:00
|
|
|
return (
|
|
|
|
|
<div className="flex w-full items-center gap-2">
|
|
|
|
|
<TextFileIcon className="size-4" />
|
2025-09-20 19:55:20 +00:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
className="hover:underline cursor-pointer"
|
|
|
|
|
onClick={() => {
|
2025-10-04 14:09:25 +00:00
|
|
|
onOpenFile(file)
|
2025-09-20 19:55:20 +00:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{file.name}
|
|
|
|
|
</button>
|
2025-09-19 23:01:44 +00:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|