2024-05-21 01:08:45 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-06-25 22:53:55 +02:00
|
|
|
import * as services from "@/store/services";
|
2024-07-03 23:34:55 +02:00
|
|
|
import * as keyutil from "@/util/keyutil";
|
2024-06-22 09:41:49 +02:00
|
|
|
import * as util from "@/util/util";
|
2024-07-09 01:36:30 +02:00
|
|
|
import type { PreviewModel } from "@/view/preview";
|
2024-06-25 21:37:58 +02:00
|
|
|
import {
|
2024-07-03 23:34:55 +02:00
|
|
|
Row,
|
2024-06-25 21:37:58 +02:00
|
|
|
Table,
|
|
|
|
createColumnHelper,
|
|
|
|
flexRender,
|
|
|
|
getCoreRowModel,
|
|
|
|
getSortedRowModel,
|
|
|
|
useReactTable,
|
|
|
|
} from "@tanstack/react-table";
|
2024-06-04 03:22:26 +02:00
|
|
|
import clsx from "clsx";
|
2024-06-28 03:13:42 +02:00
|
|
|
import dayjs from "dayjs";
|
2024-05-29 09:00:36 +02:00
|
|
|
import * as jotai from "jotai";
|
2024-05-28 21:12:28 +02:00
|
|
|
import React from "react";
|
2024-06-25 21:37:58 +02:00
|
|
|
import { ContextMenuModel } from "../store/contextmenu";
|
2024-06-26 21:14:59 +02:00
|
|
|
import { atoms, createBlock, getApi } from "../store/global";
|
2024-05-21 01:08:45 +02:00
|
|
|
|
2024-05-29 09:00:36 +02:00
|
|
|
import "./directorypreview.less";
|
2024-05-21 01:08:45 +02:00
|
|
|
|
|
|
|
interface DirectoryTableProps {
|
|
|
|
data: FileInfo[];
|
2024-07-03 23:34:55 +02:00
|
|
|
search: string;
|
2024-06-27 01:59:45 +02:00
|
|
|
focusIndex: number;
|
|
|
|
setFocusIndex: (_: number) => void;
|
2024-05-29 09:00:36 +02:00
|
|
|
setFileName: (_: string) => void;
|
2024-06-27 21:30:08 +02:00
|
|
|
setSearch: (_: string) => void;
|
2024-06-28 03:13:42 +02:00
|
|
|
setSelectedPath: (_: string) => void;
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion: React.Dispatch<React.SetStateAction<number>>;
|
2024-05-21 01:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<FileInfo>();
|
|
|
|
|
2024-06-22 09:41:49 +02:00
|
|
|
const displaySuffixes = {
|
|
|
|
B: "b",
|
|
|
|
kB: "k",
|
|
|
|
MB: "m",
|
|
|
|
GB: "g",
|
|
|
|
TB: "t",
|
|
|
|
KiB: "k",
|
|
|
|
MiB: "m",
|
|
|
|
GiB: "g",
|
|
|
|
TiB: "t",
|
|
|
|
};
|
|
|
|
|
|
|
|
function getBestUnit(bytes: number, si: boolean = false, sigfig: number = 3): string {
|
|
|
|
if (bytes < 0) {
|
2024-06-25 21:37:58 +02:00
|
|
|
return "-";
|
2024-06-22 09:41:49 +02:00
|
|
|
}
|
|
|
|
const units = si ? ["kB", "MB", "GB", "TB"] : ["KiB", "MiB", "GiB", "TiB"];
|
|
|
|
const divisor = si ? 1000 : 1024;
|
|
|
|
|
|
|
|
let currentUnit = "B";
|
|
|
|
let currentValue = bytes;
|
|
|
|
let idx = 0;
|
|
|
|
while (currentValue > divisor && idx < units.length - 1) {
|
|
|
|
currentUnit = units[idx];
|
|
|
|
currentValue /= divisor;
|
2024-06-26 21:14:59 +02:00
|
|
|
idx += 1;
|
2024-06-22 09:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return `${parseFloat(currentValue.toPrecision(sigfig))}${displaySuffixes[currentUnit]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSpecificUnit(bytes: number, suffix: string): string {
|
|
|
|
if (bytes < 0) {
|
2024-06-25 21:37:58 +02:00
|
|
|
return "-";
|
2024-06-22 09:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const divisors = new Map([
|
|
|
|
["B", 1],
|
|
|
|
["kB", 1e3],
|
|
|
|
["MB", 1e6],
|
|
|
|
["GB", 1e9],
|
|
|
|
["TB", 1e12],
|
|
|
|
["KiB", 0x400],
|
|
|
|
["MiB", 0x400 ** 2],
|
|
|
|
["GiB", 0x400 ** 3],
|
|
|
|
["TiB", 0x400 ** 4],
|
|
|
|
]);
|
|
|
|
const divisor: number = divisors[suffix] ?? 1;
|
|
|
|
|
|
|
|
return `${bytes / divisor} ${displaySuffixes[suffix]}`;
|
|
|
|
}
|
|
|
|
|
2024-06-28 03:13:42 +02:00
|
|
|
function getLastModifiedTime(unixMillis: number): string {
|
|
|
|
let fileDatetime = dayjs(new Date(unixMillis));
|
|
|
|
let nowDatetime = dayjs(new Date());
|
|
|
|
|
|
|
|
if (nowDatetime.year() != fileDatetime.year()) {
|
|
|
|
return dayjs(fileDatetime).format("M/D/YY");
|
|
|
|
} else if (nowDatetime.month() != fileDatetime.month()) {
|
|
|
|
return dayjs(fileDatetime).format("MMM D");
|
|
|
|
} else {
|
2024-07-03 23:34:55 +02:00
|
|
|
return dayjs(fileDatetime).format("MMM D h:mm A");
|
2024-06-22 09:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const iconRegex = /^[a-z0-9- ]+$/;
|
|
|
|
|
|
|
|
function isIconValid(icon: string): boolean {
|
|
|
|
if (util.isBlank(icon)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return icon.match(iconRegex) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIconClass(icon: string): string {
|
|
|
|
if (!isIconValid(icon)) {
|
|
|
|
return "fa fa-solid fa-question fa-fw";
|
|
|
|
}
|
|
|
|
return `fa fa-solid fa-${icon} fa-fw`;
|
|
|
|
}
|
2024-05-21 01:08:45 +02:00
|
|
|
|
2024-06-25 21:37:58 +02:00
|
|
|
function getSortIcon(sortType: string | boolean): React.ReactNode {
|
|
|
|
switch (sortType) {
|
|
|
|
case "asc":
|
|
|
|
return <i className="fa-solid fa-chevron-up dir-table-head-direction"></i>;
|
|
|
|
case "desc":
|
|
|
|
return <i className="fa-solid fa-chevron-down dir-table-head-direction"></i>;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 21:14:59 +02:00
|
|
|
function cleanMimetype(input: string): string {
|
2024-06-28 03:13:42 +02:00
|
|
|
if (input == "") {
|
|
|
|
return "-";
|
|
|
|
}
|
2024-06-26 21:14:59 +02:00
|
|
|
const truncated = input.split(";")[0];
|
|
|
|
return truncated.trim();
|
|
|
|
}
|
|
|
|
|
2024-06-28 03:13:42 +02:00
|
|
|
function DirectoryTable({
|
|
|
|
data,
|
2024-07-03 23:34:55 +02:00
|
|
|
search,
|
2024-06-28 03:13:42 +02:00
|
|
|
focusIndex,
|
|
|
|
setFocusIndex,
|
|
|
|
setFileName,
|
|
|
|
setSearch,
|
|
|
|
setSelectedPath,
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion,
|
2024-06-28 03:13:42 +02:00
|
|
|
}: DirectoryTableProps) {
|
2024-06-22 09:41:49 +02:00
|
|
|
let settings = jotai.useAtomValue(atoms.settingsConfigAtom);
|
|
|
|
const getIconFromMimeType = React.useCallback(
|
|
|
|
(mimeType: string): string => {
|
|
|
|
while (mimeType.length > 0) {
|
|
|
|
let icon = settings.mimetypes[mimeType]?.icon ?? null;
|
|
|
|
if (isIconValid(icon)) {
|
|
|
|
return `fa fa-solid fa-${icon} fa-fw`;
|
|
|
|
}
|
|
|
|
mimeType = mimeType.slice(0, -1);
|
|
|
|
}
|
2024-06-25 21:37:58 +02:00
|
|
|
return "fa fa-solid fa-file fa-fw";
|
2024-06-22 09:41:49 +02:00
|
|
|
},
|
|
|
|
[settings.mimetypes]
|
|
|
|
);
|
2024-06-26 21:14:59 +02:00
|
|
|
const getIconColor = React.useCallback(
|
|
|
|
(mimeType: string): string => {
|
|
|
|
let iconColor = settings.mimetypes[mimeType]?.color ?? "inherit";
|
|
|
|
return iconColor;
|
|
|
|
},
|
|
|
|
[settings.mimetypes]
|
|
|
|
);
|
2024-06-22 09:41:49 +02:00
|
|
|
const columns = React.useMemo(
|
|
|
|
() => [
|
|
|
|
columnHelper.accessor("mimetype", {
|
2024-06-26 21:14:59 +02:00
|
|
|
cell: (info) => (
|
|
|
|
<i
|
|
|
|
className={getIconFromMimeType(info.getValue() ?? "")}
|
|
|
|
style={{ color: getIconColor(info.getValue() ?? "") }}
|
|
|
|
></i>
|
|
|
|
),
|
|
|
|
header: () => <span></span>,
|
2024-06-22 09:41:49 +02:00
|
|
|
id: "logo",
|
|
|
|
size: 25,
|
2024-06-25 21:37:58 +02:00
|
|
|
enableSorting: false,
|
2024-06-22 09:41:49 +02:00
|
|
|
}),
|
2024-06-27 21:30:08 +02:00
|
|
|
columnHelper.accessor("name", {
|
|
|
|
cell: (info) => <span className="dir-table-name">{info.getValue()}</span>,
|
2024-07-10 11:21:29 +02:00
|
|
|
header: () => <span className="dir-table-head-name">Name</span>,
|
2024-06-25 21:37:58 +02:00
|
|
|
sortingFn: "alphanumeric",
|
2024-06-22 09:41:49 +02:00
|
|
|
}),
|
|
|
|
columnHelper.accessor("modestr", {
|
2024-06-26 18:31:43 +02:00
|
|
|
cell: (info) => <span className="dir-table-modestr">{info.getValue()}</span>,
|
2024-07-09 01:42:01 +02:00
|
|
|
header: () => <span>Permissions</span>,
|
2024-06-22 09:41:49 +02:00
|
|
|
size: 91,
|
2024-06-25 21:37:58 +02:00
|
|
|
sortingFn: "alphanumeric",
|
2024-06-22 09:41:49 +02:00
|
|
|
}),
|
|
|
|
columnHelper.accessor("modtime", {
|
2024-06-28 03:13:42 +02:00
|
|
|
cell: (info) => <span className="dir-table-lastmod">{getLastModifiedTime(info.getValue())}</span>,
|
2024-06-22 09:41:49 +02:00
|
|
|
header: () => <span>Last Modified</span>,
|
|
|
|
size: 185,
|
2024-06-25 21:37:58 +02:00
|
|
|
sortingFn: "datetime",
|
2024-06-22 09:41:49 +02:00
|
|
|
}),
|
|
|
|
columnHelper.accessor("size", {
|
2024-06-26 18:31:43 +02:00
|
|
|
cell: (info) => <span className="dir-table-size">{getBestUnit(info.getValue())}</span>,
|
2024-07-09 01:42:01 +02:00
|
|
|
header: () => <span className="dir-table-head-size">Size</span>,
|
2024-06-22 09:41:49 +02:00
|
|
|
size: 55,
|
2024-06-25 21:37:58 +02:00
|
|
|
sortingFn: "auto",
|
2024-06-22 09:41:49 +02:00
|
|
|
}),
|
|
|
|
columnHelper.accessor("mimetype", {
|
2024-06-26 21:14:59 +02:00
|
|
|
cell: (info) => <span className="dir-table-type">{cleanMimetype(info.getValue() ?? "")}</span>,
|
2024-07-10 11:21:29 +02:00
|
|
|
header: () => <span className="dir-table-head-type">Type</span>,
|
2024-06-25 21:37:58 +02:00
|
|
|
sortingFn: "alphanumeric",
|
2024-06-22 09:41:49 +02:00
|
|
|
}),
|
2024-06-27 21:30:08 +02:00
|
|
|
columnHelper.accessor("path", {}),
|
2024-06-22 09:41:49 +02:00
|
|
|
],
|
|
|
|
[settings]
|
|
|
|
);
|
|
|
|
|
2024-05-21 01:08:45 +02:00
|
|
|
const table = useReactTable({
|
|
|
|
data,
|
|
|
|
columns,
|
|
|
|
columnResizeMode: "onChange",
|
2024-06-25 21:37:58 +02:00
|
|
|
getSortedRowModel: getSortedRowModel(),
|
2024-05-21 01:08:45 +02:00
|
|
|
getCoreRowModel: getCoreRowModel(),
|
2024-06-27 21:30:08 +02:00
|
|
|
|
2024-06-25 21:37:58 +02:00
|
|
|
initialState: {
|
|
|
|
sorting: [
|
|
|
|
{
|
2024-06-27 21:30:08 +02:00
|
|
|
id: "name",
|
2024-06-25 21:37:58 +02:00
|
|
|
desc: false,
|
|
|
|
},
|
|
|
|
],
|
2024-06-27 21:30:08 +02:00
|
|
|
columnVisibility: {
|
|
|
|
path: false,
|
|
|
|
},
|
2024-07-03 23:34:55 +02:00
|
|
|
rowPinning: {
|
|
|
|
top: [],
|
|
|
|
bottom: [],
|
|
|
|
},
|
2024-06-25 21:37:58 +02:00
|
|
|
},
|
|
|
|
enableMultiSort: false,
|
|
|
|
enableSortingRemoval: false,
|
2024-05-21 01:08:45 +02:00
|
|
|
});
|
|
|
|
|
2024-07-03 23:34:55 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
setSelectedPath((table.getSortedRowModel()?.flatRows[focusIndex]?.getValue("path") as string) ?? null);
|
|
|
|
}, [table, focusIndex, data]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
let rows = table.getRowModel()?.flatRows;
|
|
|
|
for (const row of rows) {
|
|
|
|
if (row.getValue("name") == "..") {
|
|
|
|
row.pin("top");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [data]);
|
2024-05-21 21:14:04 +02:00
|
|
|
const columnSizeVars = React.useMemo(() => {
|
|
|
|
const headers = table.getFlatHeaders();
|
|
|
|
const colSizes: { [key: string]: number } = {};
|
|
|
|
for (let i = 0; i < headers.length; i++) {
|
|
|
|
const header = headers[i]!;
|
|
|
|
colSizes[`--header-${header.id}-size`] = header.getSize();
|
|
|
|
colSizes[`--col-${header.column.id}-size`] = header.column.getSize();
|
|
|
|
}
|
|
|
|
return colSizes;
|
|
|
|
}, [table.getState().columnSizingInfo]);
|
|
|
|
|
2024-05-21 01:08:45 +02:00
|
|
|
return (
|
2024-05-21 21:14:04 +02:00
|
|
|
<div className="dir-table" style={{ ...columnSizeVars }}>
|
|
|
|
<div className="dir-table-head">
|
2024-05-21 01:08:45 +02:00
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
2024-05-21 21:14:04 +02:00
|
|
|
<div className="dir-table-head-row" key={headerGroup.id}>
|
2024-05-21 01:08:45 +02:00
|
|
|
{headerGroup.headers.map((header) => (
|
2024-07-10 11:21:29 +02:00
|
|
|
<div
|
|
|
|
className="dir-table-head-cell"
|
|
|
|
key={header.id}
|
|
|
|
style={{ width: `calc(var(--header-${header.id}-size) * 1px)` }}
|
|
|
|
>
|
2024-05-21 21:14:04 +02:00
|
|
|
<div
|
2024-07-09 01:42:01 +02:00
|
|
|
className="dir-table-head-cell-content"
|
|
|
|
onClick={() => header.column.toggleSorting()}
|
|
|
|
>
|
|
|
|
{header.isPlaceholder
|
|
|
|
? null
|
|
|
|
: flexRender(header.column.columnDef.header, header.getContext())}
|
|
|
|
{getSortIcon(header.column.getIsSorted())}
|
|
|
|
</div>
|
|
|
|
<div className="dir-table-head-resize-box">
|
|
|
|
<div
|
|
|
|
className="dir-table-head-resize"
|
|
|
|
onMouseDown={header.getResizeHandler()}
|
|
|
|
onTouchStart={header.getResizeHandler()}
|
|
|
|
/>
|
|
|
|
</div>
|
2024-05-21 21:14:04 +02:00
|
|
|
</div>
|
2024-05-21 01:08:45 +02:00
|
|
|
))}
|
2024-05-21 21:14:04 +02:00
|
|
|
</div>
|
2024-05-21 01:08:45 +02:00
|
|
|
))}
|
2024-05-21 21:14:04 +02:00
|
|
|
</div>
|
|
|
|
{table.getState().columnSizingInfo.isResizingColumn ? (
|
2024-06-27 01:59:45 +02:00
|
|
|
<MemoizedTableBody
|
2024-07-03 23:34:55 +02:00
|
|
|
data={data}
|
2024-06-27 01:59:45 +02:00
|
|
|
table={table}
|
2024-07-03 23:34:55 +02:00
|
|
|
search={search}
|
2024-06-27 01:59:45 +02:00
|
|
|
focusIndex={focusIndex}
|
|
|
|
setFileName={setFileName}
|
|
|
|
setFocusIndex={setFocusIndex}
|
2024-06-27 21:30:08 +02:00
|
|
|
setSearch={setSearch}
|
2024-06-28 03:13:42 +02:00
|
|
|
setSelectedPath={setSelectedPath}
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion={setRefreshVersion}
|
2024-06-27 01:59:45 +02:00
|
|
|
/>
|
2024-05-21 21:14:04 +02:00
|
|
|
) : (
|
2024-06-27 01:59:45 +02:00
|
|
|
<TableBody
|
2024-07-03 23:34:55 +02:00
|
|
|
data={data}
|
2024-06-27 01:59:45 +02:00
|
|
|
table={table}
|
2024-07-03 23:34:55 +02:00
|
|
|
search={search}
|
2024-06-27 01:59:45 +02:00
|
|
|
focusIndex={focusIndex}
|
|
|
|
setFileName={setFileName}
|
|
|
|
setFocusIndex={setFocusIndex}
|
2024-06-27 21:30:08 +02:00
|
|
|
setSearch={setSearch}
|
2024-06-28 03:13:42 +02:00
|
|
|
setSelectedPath={setSelectedPath}
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion={setRefreshVersion}
|
2024-06-27 01:59:45 +02:00
|
|
|
/>
|
2024-05-21 21:14:04 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-29 09:00:36 +02:00
|
|
|
interface TableBodyProps {
|
2024-07-03 23:34:55 +02:00
|
|
|
data: Array<FileInfo>;
|
2024-05-29 09:00:36 +02:00
|
|
|
table: Table<FileInfo>;
|
2024-07-03 23:34:55 +02:00
|
|
|
search: string;
|
2024-06-27 01:59:45 +02:00
|
|
|
focusIndex: number;
|
|
|
|
setFocusIndex: (_: number) => void;
|
2024-05-29 09:00:36 +02:00
|
|
|
setFileName: (_: string) => void;
|
2024-06-27 21:30:08 +02:00
|
|
|
setSearch: (_: string) => void;
|
2024-06-28 03:13:42 +02:00
|
|
|
setSelectedPath: (_: string) => void;
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion: React.Dispatch<React.SetStateAction<number>>;
|
2024-05-29 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
2024-06-28 03:13:42 +02:00
|
|
|
function TableBody({
|
2024-07-03 23:34:55 +02:00
|
|
|
data,
|
2024-06-28 03:13:42 +02:00
|
|
|
table,
|
2024-07-03 23:34:55 +02:00
|
|
|
search,
|
2024-06-28 03:13:42 +02:00
|
|
|
focusIndex,
|
|
|
|
setFocusIndex,
|
|
|
|
setFileName,
|
|
|
|
setSearch,
|
|
|
|
setSelectedPath,
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion,
|
2024-06-28 03:13:42 +02:00
|
|
|
}: TableBodyProps) {
|
2024-07-03 23:34:55 +02:00
|
|
|
const dummyLineRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const parentRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const warningBoxRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const [bodyHeight, setBodyHeight] = React.useState(0);
|
|
|
|
const [containerHeight, setContainerHeight] = React.useState(0);
|
|
|
|
|
2024-06-27 01:59:45 +02:00
|
|
|
React.useEffect(() => {
|
2024-07-03 23:34:55 +02:00
|
|
|
if (parentRef.current == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const resizeObserver = new ResizeObserver(() => {
|
|
|
|
setContainerHeight(parentRef.current.getBoundingClientRect().height); // 17 is height of breadcrumb
|
|
|
|
});
|
|
|
|
resizeObserver.observe(parentRef.current);
|
|
|
|
|
|
|
|
return () => resizeObserver.disconnect();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (dummyLineRef.current && data && parentRef.current) {
|
|
|
|
const rowHeight = dummyLineRef.current.offsetHeight;
|
|
|
|
const fullTBodyHeight = rowHeight * data.length;
|
|
|
|
const warningBoxHeight = warningBoxRef.current?.offsetHeight ?? 0;
|
|
|
|
const maxHeight = containerHeight - 1; // i don't know why, but the -1 makes the resize work
|
|
|
|
const maxHeightLessHeader = maxHeight - warningBoxHeight;
|
|
|
|
const tbodyHeight = Math.min(maxHeightLessHeader, fullTBodyHeight);
|
|
|
|
|
|
|
|
setBodyHeight(tbodyHeight);
|
|
|
|
}
|
|
|
|
}, [data, containerHeight]);
|
2024-06-28 03:13:42 +02:00
|
|
|
|
|
|
|
const handleFileContextMenu = React.useCallback(
|
|
|
|
(e: React.MouseEvent<HTMLDivElement>, path: string) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
let menu: ContextMenuItem[] = [];
|
|
|
|
menu.push({
|
|
|
|
label: "Open in New Block",
|
|
|
|
click: async () => {
|
|
|
|
const blockDef = {
|
|
|
|
view: "preview",
|
|
|
|
meta: { file: path },
|
|
|
|
};
|
|
|
|
await createBlock(blockDef);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
menu.push({
|
|
|
|
label: "Delete File",
|
|
|
|
click: async () => {
|
|
|
|
await services.FileService.DeleteFile(path).catch((e) => console.log(e)); //todo these errors need a popup
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion((current) => current + 1);
|
2024-06-28 03:13:42 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
menu.push({
|
|
|
|
label: "Download File",
|
|
|
|
click: async () => {
|
|
|
|
getApi().downloadFile(path);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
ContextMenuModel.showContextMenu(menu, e);
|
|
|
|
},
|
2024-07-09 01:36:30 +02:00
|
|
|
[setRefreshVersion]
|
2024-06-28 03:13:42 +02:00
|
|
|
);
|
2024-06-27 01:59:45 +02:00
|
|
|
|
2024-07-03 23:34:55 +02:00
|
|
|
const displayRow = React.useCallback(
|
|
|
|
(row: Row<FileInfo>, idx: number) => (
|
|
|
|
<div
|
|
|
|
className={clsx("dir-table-body-row", { focused: focusIndex === idx })}
|
|
|
|
key={row.id}
|
|
|
|
onDoubleClick={() => {
|
|
|
|
const newFileName = row.getValue("path") as string;
|
|
|
|
setFileName(newFileName);
|
|
|
|
setSearch("");
|
|
|
|
}}
|
|
|
|
onClick={() => setFocusIndex(idx)}
|
|
|
|
onContextMenu={(e) => handleFileContextMenu(e, row.getValue("path") as string)}
|
|
|
|
>
|
|
|
|
{row.getVisibleCells().map((cell) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx("dir-table-body-cell", "col-" + cell.column.id)}
|
|
|
|
key={cell.id}
|
|
|
|
style={{ width: `calc(var(--col-${cell.column.id}-size) * 1px)` }}
|
|
|
|
>
|
|
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
[setSearch, setFileName, handleFileContextMenu, setFocusIndex, focusIndex]
|
|
|
|
);
|
|
|
|
|
2024-05-21 21:14:04 +02:00
|
|
|
return (
|
2024-07-03 23:34:55 +02:00
|
|
|
<div className="dir-table-body" ref={parentRef}>
|
|
|
|
{search == "" || (
|
|
|
|
<div className="dir-table-body-search-display" ref={warningBoxRef}>
|
|
|
|
<span>Searching for "{search}"</span>
|
|
|
|
<div className="search-display-close-button dir-table-button" onClick={() => setSearch("")}>
|
|
|
|
<i className="fa-solid fa-xmark" />
|
|
|
|
<input type="text" value={search} onChange={() => {}}></input>
|
|
|
|
</div>
|
2024-05-21 21:14:04 +02:00
|
|
|
</div>
|
2024-07-03 23:34:55 +02:00
|
|
|
)}
|
|
|
|
<div className="dir-table-body-scroll-box" style={{ height: bodyHeight }}>
|
|
|
|
<div className="dummy dir-table-body-row" ref={dummyLineRef}>
|
|
|
|
<div className="dir-table-body-cell">dummy-data</div>
|
|
|
|
</div>
|
|
|
|
{table.getTopRows().map(displayRow)}
|
|
|
|
{table.getCenterRows().map((row, idx) => displayRow(row, idx + table.getTopRows().length))}
|
|
|
|
</div>
|
2024-05-21 21:14:04 +02:00
|
|
|
</div>
|
2024-05-21 01:08:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-21 21:14:04 +02:00
|
|
|
const MemoizedTableBody = React.memo(
|
|
|
|
TableBody,
|
|
|
|
(prev, next) => prev.table.options.data == next.table.options.data
|
|
|
|
) as typeof TableBody;
|
|
|
|
|
2024-05-29 09:00:36 +02:00
|
|
|
interface DirectoryPreviewProps {
|
|
|
|
fileNameAtom: jotai.WritableAtom<string, [string], void>;
|
2024-07-09 01:36:30 +02:00
|
|
|
model: PreviewModel;
|
2024-05-29 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 01:36:30 +02:00
|
|
|
function DirectoryPreview({ fileNameAtom, model }: DirectoryPreviewProps) {
|
2024-06-27 01:59:45 +02:00
|
|
|
const [searchText, setSearchText] = React.useState("");
|
2024-06-28 03:13:42 +02:00
|
|
|
const [focusIndex, setFocusIndex] = React.useState(0);
|
2024-07-10 11:21:29 +02:00
|
|
|
const [unfilteredData, setUnfilteredData] = React.useState<FileInfo[]>([]);
|
|
|
|
const [filteredData, setFilteredData] = React.useState<FileInfo[]>([]);
|
2024-06-28 03:13:42 +02:00
|
|
|
const [fileName, setFileName] = jotai.useAtom(fileNameAtom);
|
2024-07-09 08:13:12 +02:00
|
|
|
const hideHiddenFiles = jotai.useAtomValue(model.showHiddenFiles);
|
2024-06-28 03:13:42 +02:00
|
|
|
const [selectedPath, setSelectedPath] = React.useState("");
|
2024-07-09 08:13:12 +02:00
|
|
|
const [refreshVersion, setRefreshVersion] = jotai.useAtom(model.refreshVersion);
|
2024-07-09 01:36:30 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
model.refreshCallback = () => {
|
|
|
|
setRefreshVersion((refreshVersion) => refreshVersion + 1);
|
|
|
|
};
|
|
|
|
return () => {
|
|
|
|
model.refreshCallback = null;
|
|
|
|
};
|
|
|
|
}, [setRefreshVersion]);
|
2024-06-27 01:59:45 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const getContent = async () => {
|
|
|
|
const file = await services.FileService.ReadFile(fileName);
|
|
|
|
const serializedContent = util.base64ToString(file?.data64);
|
|
|
|
let content: FileInfo[] = JSON.parse(serializedContent);
|
2024-07-10 11:21:29 +02:00
|
|
|
setUnfilteredData(content);
|
2024-06-27 01:59:45 +02:00
|
|
|
};
|
|
|
|
getContent();
|
2024-07-10 11:21:29 +02:00
|
|
|
}, [fileName, refreshVersion]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
let filtered = unfilteredData.filter((fileInfo) => {
|
|
|
|
if (hideHiddenFiles && fileInfo.name.startsWith(".") && fileInfo.name != "..") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return fileInfo.name.toLowerCase().includes(searchText);
|
|
|
|
});
|
|
|
|
setFilteredData(filtered);
|
|
|
|
}, [unfilteredData, hideHiddenFiles, searchText]);
|
2024-06-27 01:59:45 +02:00
|
|
|
|
|
|
|
const handleKeyDown = React.useCallback(
|
2024-07-03 23:34:55 +02:00
|
|
|
(waveEvent: WaveKeyboardEvent): boolean => {
|
|
|
|
if (keyutil.checkKeyPressed(waveEvent, "Escape")) {
|
|
|
|
setSearchText("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (keyutil.checkKeyPressed(waveEvent, "ArrowUp")) {
|
|
|
|
setFocusIndex((idx) => Math.max(idx - 1, 0));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (keyutil.checkKeyPressed(waveEvent, "ArrowDown")) {
|
2024-07-10 11:21:29 +02:00
|
|
|
setFocusIndex((idx) => Math.min(idx + 1, filteredData.length - 1));
|
2024-07-03 23:34:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (keyutil.checkKeyPressed(waveEvent, "Enter")) {
|
2024-07-10 11:21:29 +02:00
|
|
|
if (filteredData.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-03 23:34:55 +02:00
|
|
|
setFileName(selectedPath);
|
|
|
|
setSearchText("");
|
|
|
|
return true;
|
2024-06-27 01:59:45 +02:00
|
|
|
}
|
|
|
|
},
|
2024-07-10 11:21:29 +02:00
|
|
|
[filteredData, setFocusIndex, selectedPath]
|
2024-06-27 01:59:45 +02:00
|
|
|
);
|
|
|
|
|
2024-07-10 11:21:29 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (filteredData.length != 0 && focusIndex > filteredData.length - 1) {
|
|
|
|
setFocusIndex(filteredData.length - 1);
|
|
|
|
}
|
|
|
|
}, [filteredData]);
|
|
|
|
|
2024-06-27 01:59:45 +02:00
|
|
|
return (
|
2024-07-03 23:34:55 +02:00
|
|
|
<div
|
|
|
|
className="dir-table-container"
|
|
|
|
onChangeCapture={(e) => {
|
|
|
|
const event = e as React.ChangeEvent<HTMLInputElement>;
|
|
|
|
setSearchText(event.target.value.toLowerCase());
|
|
|
|
}}
|
|
|
|
onKeyDownCapture={(e) => keyutil.keydownWrapper(handleKeyDown)(e)}
|
|
|
|
onFocusCapture={() => document.getSelection().collapseToEnd()}
|
|
|
|
>
|
2024-06-27 01:59:45 +02:00
|
|
|
<div className="dir-table-search-line">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="dir-table-search-box"
|
2024-07-03 23:34:55 +02:00
|
|
|
onChange={() => {}} //for nuisance warnings
|
2024-06-27 01:59:45 +02:00
|
|
|
maxLength={400}
|
|
|
|
autoFocus={true}
|
2024-06-27 21:30:08 +02:00
|
|
|
value={searchText}
|
2024-06-27 01:59:45 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<DirectoryTable
|
2024-07-10 11:21:29 +02:00
|
|
|
data={filteredData}
|
2024-07-03 23:34:55 +02:00
|
|
|
search={searchText}
|
2024-06-27 01:59:45 +02:00
|
|
|
focusIndex={focusIndex}
|
|
|
|
setFileName={setFileName}
|
|
|
|
setFocusIndex={setFocusIndex}
|
2024-06-27 21:30:08 +02:00
|
|
|
setSearch={setSearchText}
|
2024-06-28 03:13:42 +02:00
|
|
|
setSelectedPath={setSelectedPath}
|
2024-07-09 01:36:30 +02:00
|
|
|
setRefreshVersion={setRefreshVersion}
|
2024-06-27 01:59:45 +02:00
|
|
|
/>
|
2024-07-03 23:34:55 +02:00
|
|
|
</div>
|
2024-06-27 01:59:45 +02:00
|
|
|
);
|
2024-05-29 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { DirectoryPreview };
|