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

80 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-07-21 17:55:53 +02:00
import * as React from "bloatless-react";
2024-07-22 19:28:39 +02:00
import StorageModel, {
PATH_COMPONENT_SEPARATOR,
} from "../../Model/Global/storageModel";
2024-07-22 19:52:08 +02:00
import StorageViewModel from "../../ViewModel/Global/storageViewModel";
2024-07-21 17:55:53 +02:00
export function DirectoryItemList(
storageViewModel: StorageViewModel,
pathString: string = PATH_COMPONENT_SEPARATOR
2024-07-21 17:55:53 +02:00
) {
// converter
const StringToDirectoryItemList: React.StateItemConverter<string> = (
pathString: string
) => DirectoryItemList(storageViewModel, pathString);
2024-07-21 17:55:53 +02:00
// data
const path = StorageModel.stringToPathComponents(pathString);
2024-07-21 18:56:56 +02:00
const fileName = StorageModel.getFileName(path);
2024-07-21 17:55:53 +02:00
const items = new React.ListState<string>();
2024-07-22 19:28:39 +02:00
const style = `text-indent: ${path.length}rem`;
2024-07-21 17:55:53 +02:00
// methods
function loadItems() {
items.clear();
const directoryItems = storageViewModel.storageModel.list(path);
2024-07-21 17:55:53 +02:00
for (const directoryItem of directoryItems) {
const itemPath = [...path, directoryItem];
const pathString = StorageModel.pathComponentsToString(...itemPath);
items.add(pathString);
}
}
function select() {
storageViewModel.selectedPath.value = pathString;
2024-07-21 17:55:53 +02:00
}
// handlers
storageViewModel.lastDeletedItemPath.subscribe((lastDeletedItemPath) => {
if (!items.value.has(lastDeletedItemPath)) return;
select();
2024-07-22 19:28:39 +02:00
setTimeout(() => loadItems(), 50);
});
2024-07-21 17:55:53 +02:00
// state
const isSelected = React.createProxyState(
[storageViewModel.selectedPath],
() => storageViewModel.selectedPath.value == pathString
2024-07-21 17:55:53 +02:00
);
isSelected.subscribe(() => {
if (isSelected.value == false) return;
loadItems();
});
return (
<div class="flex-column">
<button
class="width-100 flex-1 clip"
toggle:selected={isSelected}
on:click={select}
>
<span class="ellipsis width-100 flex-1" style={style}>
{fileName}
</span>
</button>
<div
class="flex-column"
children:append={[items, StringToDirectoryItemList]}
></div>
</div>
);
}