2025-09-20 23:54:27 +00:00
|
|
|
import type { Doc } from "@fileone/convex/_generated/dataModel"
|
|
|
|
|
import {
|
2025-09-28 15:45:49 +00:00
|
|
|
type DirectoryHandle,
|
|
|
|
|
type FileHandle,
|
2025-09-21 17:03:50 +00:00
|
|
|
type FileSystemHandle,
|
2025-09-28 15:45:49 +00:00
|
|
|
type FileSystemItem,
|
|
|
|
|
FileType,
|
2025-09-26 22:20:30 +00:00
|
|
|
isSameHandle,
|
2025-09-20 23:54:27 +00:00
|
|
|
newDirectoryHandle,
|
|
|
|
|
newFileHandle,
|
2025-09-28 15:45:49 +00:00
|
|
|
newFileSystemHandle,
|
2025-10-18 14:02:20 +00:00
|
|
|
} from "@fileone/convex/filesystem"
|
2025-09-21 15:18:32 +00:00
|
|
|
import { Link, useNavigate } from "@tanstack/react-router"
|
2025-09-19 23:01:44 +00:00
|
|
|
import {
|
|
|
|
|
type ColumnDef,
|
|
|
|
|
flexRender,
|
|
|
|
|
getCoreRowModel,
|
|
|
|
|
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-10-04 14:09:25 +00:00
|
|
|
import { type PrimitiveAtom, useSetAtom, useStore } from "jotai"
|
|
|
|
|
import { useContext, useEffect, useMemo, useRef } from "react"
|
2025-09-19 23:01:44 +00:00
|
|
|
import { DirectoryIcon } from "@/components/icons/directory-icon"
|
|
|
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
|
|
|
|
import {
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableRow,
|
|
|
|
|
} from "@/components/ui/table"
|
2025-09-21 15:12:05 +00:00
|
|
|
import {
|
|
|
|
|
isControlOrCommandKeyActive,
|
|
|
|
|
keyboardModifierAtom,
|
|
|
|
|
} from "@/lib/keyboard"
|
2025-09-19 23:01:44 +00:00
|
|
|
import { TextFileIcon } from "../../components/icons/text-file-icon"
|
2025-10-04 14:09:25 +00:00
|
|
|
import { type FileDragInfo, useFileDrop } from "../../files/use-file-drop"
|
2025-09-19 23:01:44 +00:00
|
|
|
import { cn } from "../../lib/utils"
|
|
|
|
|
import { DirectoryPageContext } from "./context"
|
2025-10-04 14:09:25 +00:00
|
|
|
|
|
|
|
|
type DirectoryContentTableProps = {
|
|
|
|
|
filterFn: (item: FileSystemItem) => boolean
|
2025-10-05 00:41:59 +00:00
|
|
|
directoryUrlFn: (directory: Doc<"directories">) => string
|
2025-10-04 14:09:25 +00:00
|
|
|
fileDragInfoAtom: PrimitiveAtom<FileDragInfo | null>
|
2025-10-04 14:56:53 +00:00
|
|
|
onContextMenu: (
|
|
|
|
|
row: Row<FileSystemItem>,
|
|
|
|
|
table: TableType<FileSystemItem>,
|
|
|
|
|
) => void
|
2025-10-04 14:09:25 +00:00
|
|
|
onOpenFile: (file: Doc<"files">) => void
|
|
|
|
|
}
|
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(
|
|
|
|
|
onOpenFile: (file: Doc<"files">) => void,
|
2025-10-05 00:41:59 +00:00
|
|
|
directoryUrlFn: (directory: Doc<"directories">) => string,
|
2025-10-04 14:09:25 +00:00
|
|
|
): ColumnDef<FileSystemItem>[] {
|
|
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: "Name",
|
|
|
|
|
accessorKey: "doc.name",
|
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
switch (row.original.kind) {
|
|
|
|
|
case FileType.File:
|
|
|
|
|
return (
|
|
|
|
|
<FileNameCell
|
|
|
|
|
file={row.original.doc}
|
|
|
|
|
onOpenFile={onOpenFile}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
case FileType.Directory:
|
|
|
|
|
return (
|
|
|
|
|
<DirectoryNameCell
|
|
|
|
|
directory={row.original.doc}
|
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) {
|
|
|
|
|
case FileType.File:
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{formatFileSize(row.original.doc.size)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
case FileType.Directory:
|
|
|
|
|
return <div className="font-mono">-</div>
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: "Created At",
|
|
|
|
|
accessorKey: "createdAt",
|
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{new Date(
|
|
|
|
|
row.original.doc.createdAt,
|
|
|
|
|
).toLocaleString()}
|
|
|
|
|
</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-04 14:09:25 +00:00
|
|
|
filterFn,
|
2025-10-05 00:41:59 +00:00
|
|
|
directoryUrlFn,
|
2025-10-04 14:09:25 +00:00
|
|
|
onContextMenu,
|
|
|
|
|
fileDragInfoAtom,
|
|
|
|
|
onOpenFile,
|
|
|
|
|
}: DirectoryContentTableProps) {
|
2025-09-19 23:01:44 +00:00
|
|
|
const { directoryContent } = useContext(DirectoryPageContext)
|
|
|
|
|
const store = useStore()
|
2025-09-21 15:18:32 +00:00
|
|
|
const navigate = useNavigate()
|
2025-09-19 23:01:44 +00:00
|
|
|
|
2025-09-26 22:28:51 +00:00
|
|
|
const table = useReactTable({
|
|
|
|
|
data: directoryContent || [],
|
2025-10-05 00:41:59 +00:00
|
|
|
columns: useTableColumns(onOpenFile, directoryUrlFn),
|
2025-09-26 22:28:51 +00:00
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
|
enableRowSelection: true,
|
|
|
|
|
enableGlobalFilter: true,
|
2025-10-04 14:09:25 +00:00
|
|
|
globalFilterFn: (row, _columnId, _filterValue, _addMeta) =>
|
|
|
|
|
filterFn(row.original),
|
2025-09-26 22:28:51 +00:00
|
|
|
getRowId: (row) => row.doc._id,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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-09-19 23:01:44 +00:00
|
|
|
const handleRowContextMenu = (
|
2025-09-28 15:45:49 +00:00
|
|
|
row: Row<FileSystemItem>,
|
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-09-28 15:45:49 +00:00
|
|
|
const selectRow = (row: Row<FileSystemItem>) => {
|
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-09-28 15:45:49 +00:00
|
|
|
const handleRowDoubleClick = (row: Row<FileSystemItem>) => {
|
|
|
|
|
if (row.original.kind === FileType.Directory) {
|
2025-09-21 15:18:32 +00:00
|
|
|
navigate({
|
|
|
|
|
to: `/directories/${row.original.doc._id}`,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 23:01:44 +00:00
|
|
|
return (
|
|
|
|
|
<div className="overflow-hidden">
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
|
<TableRow className="px-4" key={headerGroup.id}>
|
|
|
|
|
{headerGroup.headers.map((header) => (
|
|
|
|
|
<TableHead
|
|
|
|
|
className="first:pl-4 last:pr-4"
|
|
|
|
|
key={header.id}
|
|
|
|
|
style={{ width: header.getSize() }}
|
|
|
|
|
>
|
|
|
|
|
{header.isPlaceholder
|
|
|
|
|
? null
|
|
|
|
|
: flexRender(
|
|
|
|
|
header.column.columnDef.header,
|
|
|
|
|
header.getContext(),
|
|
|
|
|
)}
|
|
|
|
|
</TableHead>
|
|
|
|
|
))}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{table.getRowModel().rows?.length ? (
|
2025-09-21 15:18:32 +00:00
|
|
|
table.getRowModel().rows.map((row) => (
|
|
|
|
|
<FileItemRow
|
|
|
|
|
key={row.id}
|
2025-09-21 17:03:50 +00:00
|
|
|
table={table}
|
2025-09-21 15:18:32 +00:00
|
|
|
row={row}
|
|
|
|
|
onClick={() => selectRow(row)}
|
2025-10-04 14:09:25 +00:00
|
|
|
fileDragInfoAtom={fileDragInfoAtom}
|
2025-09-21 15:18:32 +00:00
|
|
|
onContextMenu={(e) =>
|
|
|
|
|
handleRowContextMenu(row, e)
|
|
|
|
|
}
|
|
|
|
|
onDoubleClick={() => {
|
|
|
|
|
handleRowDoubleClick(row)
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))
|
2025-09-19 23:01:44 +00:00
|
|
|
) : (
|
|
|
|
|
<NoResultsRow />
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-09-20 22:25:01 +00:00
|
|
|
}: {
|
2025-09-28 15:45:49 +00:00
|
|
|
table: TableType<FileSystemItem>
|
|
|
|
|
row: Row<FileSystemItem>
|
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-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-09-26 22:20:30 +00:00
|
|
|
destItem:
|
2025-09-28 15:45:49 +00:00
|
|
|
row.original.kind === FileType.Directory
|
2025-09-20 23:54:27 +00:00
|
|
|
? newDirectoryHandle(row.original.doc._id)
|
|
|
|
|
: null,
|
2025-10-04 14:09:25 +00:00
|
|
|
dragInfoAtom: fileDragInfoAtom,
|
2025-09-20 22:25:01 +00:00
|
|
|
})
|
|
|
|
|
|
2025-09-28 15:45:49 +00:00
|
|
|
const handleDragStart = (_e: React.DragEvent) => {
|
|
|
|
|
let source: DirectoryHandle | FileHandle
|
2025-09-21 17:03:50 +00:00
|
|
|
switch (row.original.kind) {
|
2025-09-28 15:45:49 +00:00
|
|
|
case FileType.File:
|
2025-09-21 17:03:50 +00:00
|
|
|
source = newFileHandle(row.original.doc._id)
|
|
|
|
|
break
|
2025-09-28 15:45:49 +00:00
|
|
|
case FileType.Directory:
|
2025-09-21 17:03:50 +00:00
|
|
|
source = newDirectoryHandle(row.original.doc._id)
|
|
|
|
|
break
|
2025-09-20 22:25:01 +00:00
|
|
|
}
|
2025-09-21 17:03:50 +00:00
|
|
|
|
2025-09-26 22:20:30 +00:00
|
|
|
let draggedItems: FileSystemHandle[]
|
|
|
|
|
// drag all selections, but only if the currently dragged row is also selected
|
|
|
|
|
if (row.getIsSelected()) {
|
2025-09-28 15:45:49 +00:00
|
|
|
draggedItems = table
|
|
|
|
|
.getSelectedRowModel()
|
|
|
|
|
.rows.map((row) => newFileSystemHandle(row.original))
|
2025-09-26 22:20:30 +00:00
|
|
|
if (!draggedItems.some((item) => isSameHandle(item, source))) {
|
|
|
|
|
draggedItems.push(source)
|
2025-09-21 17:03:50 +00:00
|
|
|
}
|
2025-09-26 22:20:30 +00:00
|
|
|
} else {
|
|
|
|
|
draggedItems = [source]
|
|
|
|
|
}
|
2025-09-21 17:03:50 +00:00
|
|
|
|
2025-10-04 14:09:25 +00:00
|
|
|
setFileDragInfo({
|
2025-09-21 17:03:50 +00:00
|
|
|
source,
|
|
|
|
|
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 })}
|
|
|
|
|
>
|
|
|
|
|
{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-10-05 00:41:59 +00:00
|
|
|
function DirectoryNameCell({
|
|
|
|
|
directory,
|
|
|
|
|
directoryUrlFn,
|
|
|
|
|
}: {
|
|
|
|
|
directory: Doc<"directories">
|
|
|
|
|
directoryUrlFn: (directory: Doc<"directories">) => string
|
|
|
|
|
}) {
|
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,
|
|
|
|
|
}: {
|
|
|
|
|
file: Doc<"files">
|
|
|
|
|
onOpenFile: (file: Doc<"files">) => void
|
|
|
|
|
}) {
|
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>
|
|
|
|
|
)
|
|
|
|
|
}
|