Files
CT/src/View/Components/fileBrowser.tsx
T

89 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-07-21 17:55:53 +02:00
import "./fileBrowser.css";
import * as React from "bloatless-react";
2024-07-21 18:56:56 +02:00
import StorageModel, {
PATH_COMPONENT_SEPARATOR,
} from "../../Model/storageModel";
2024-07-21 17:55:53 +02:00
import { DirectoryItemList } from "./directoryItemList";
2024-07-21 19:44:10 +02:00
import { translations } from "../translations";
2024-07-21 17:55:53 +02:00
export function FileBrowser(
storageModel: StorageModel,
2024-07-21 19:44:10 +02:00
selectedPath: React.State<string>,
didMakeChanges: React.State<boolean>
2024-07-21 17:55:53 +02:00
) {
2024-07-21 18:56:56 +02:00
// state
2024-07-21 19:44:10 +02:00
const fileContentToShow = React.createProxyState([selectedPath], () => {
2024-07-21 17:55:53 +02:00
const path = StorageModel.stringToPathComponents(selectedPath.value);
const content = storageModel.restore(path);
2024-07-21 19:44:10 +02:00
return (
(content ?? translations.fileBrowser.notAFile) ||
translations.fileBrowser.contentEmpty
);
2024-07-21 17:55:53 +02:00
});
2024-07-21 18:56:56 +02:00
const selectedFileName = React.createProxyState(
[selectedPath],
() =>
StorageModel.getFileNameFromString(selectedPath.value) ??
PATH_COMPONENT_SEPARATOR
);
// view
2024-07-21 19:44:10 +02:00
const detailView = React.createProxyState([selectedPath], () => {
if (selectedPath.value == PATH_COMPONENT_SEPARATOR)
return (
<span class="secondary">{translations.fileBrowser.noItemSelected}</span>
);
return (
<div class="flex-column gap">
<div class="tile flex-no">
<div>
<b>{translations.fileBrowser.path}</b>
<span class="break-all" subscribe:innerText={selectedPath}></span>
</div>
</div>
<div class="tile flex-no">
<div>
<b>{translations.fileBrowser.content}</b>
<code subscribe:innerText={fileContentToShow}></code>
</div>
</div>
</div>
);
});
2024-07-21 18:56:56 +02:00
const view = (
2024-07-21 17:55:53 +02:00
<div class="file-browser">
<div>
2024-07-21 18:56:56 +02:00
<div class="scroll-area">
{DirectoryItemList(
storageModel,
PATH_COMPONENT_SEPARATOR,
selectedPath
)}
</div>
<div class="detail-button-wrapper">
<button class="ghost" on:click={scrollToDetails}>
<span
class="ellipsis"
subscribe:innerText={selectedFileName}
></span>
<span class="icon">arrow_forward</span>
</button>
</div>
</div>
2024-07-21 19:44:10 +02:00
<div class="scroll-area" children:set={detailView}></div>
2024-07-21 17:55:53 +02:00
</div>
);
2024-07-21 18:56:56 +02:00
function scrollToDetails() {
view.scrollLeft = view.scrollWidth;
}
return view;
2024-07-21 17:55:53 +02:00
}