Files
CT/src/ViewModel/Global/storageViewModel.ts
T

87 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-07-21 17:55:53 +02:00
import * as React from "bloatless-react";
2024-07-25 16:02:49 +02:00
import StorageModel, {
2026-04-01 08:29:39 +00:00
PATH_COMPONENT_SEPARATOR,
2024-07-25 16:02:49 +02:00
} from "../../Model/Global/storageModel";
2024-07-21 17:55:53 +02:00
2026-04-12 19:20:37 +00:00
import CoreViewModel, { Context } from "./coreViewModel";
import { CommonKeys } from "../../View/keystrokes";
export default class StorageViewModel extends Context {
2026-04-01 08:29:39 +00:00
// state
isShowingStorageModal: React.State<boolean> = new React.State(false);
selectedPath: React.State<string> = new React.State(
PATH_COMPONENT_SEPARATOR,
);
didMakeChanges: React.State<boolean> = new React.State(false);
2024-07-21 17:55:53 +02:00
2026-04-01 08:29:39 +00:00
selectedFileName: React.State<string>;
selectedFileContent: React.State<string>;
2026-04-01 08:29:39 +00:00
lastDeletedItemPath: React.State<string> = new React.State("");
2026-04-01 08:29:39 +00:00
// methods
getSelectedItemContent = (): string => {
2026-04-13 11:23:17 +00:00
const path: string[] = StorageModel.stringToPathComponents(
2026-04-01 08:29:39 +00:00
this.selectedPath.value,
);
2026-04-13 11:23:17 +00:00
const content: string | undefined =
this.coreViewModel.storageModel.read(path);
2026-04-01 08:29:39 +00:00
return (
(content ?? this.coreViewModel.translations.storage.notAFile) ||
this.coreViewModel.translations.storage.contentEmpty
2026-04-01 08:29:39 +00:00
);
};
2026-04-01 08:29:39 +00:00
deleteSelectedItem = (): void => {
2026-04-13 11:23:17 +00:00
const path: string[] = StorageModel.stringToPathComponents(
2026-04-01 08:29:39 +00:00
this.selectedPath.value,
);
this.lastDeletedItemPath.value = this.selectedPath.value;
2024-07-25 16:02:49 +02:00
this.coreViewModel.storageModel.removeRecursively(path);
2026-04-01 08:29:39 +00:00
this.didMakeChanges.value = true;
};
2026-04-01 08:29:39 +00:00
removeJunk = (): void => {
this.coreViewModel.storageModel.removeJunk();
2026-04-01 08:29:39 +00:00
this.selectedPath.value = PATH_COMPONENT_SEPARATOR;
};
2024-07-28 16:08:29 +02:00
2026-04-01 08:29:39 +00:00
// view
showStorageModal = (): void => {
2026-04-12 19:20:37 +00:00
this.coreViewModel.context = this;
2026-04-01 08:29:39 +00:00
this.isShowingStorageModal.value = true;
};
2024-07-21 17:55:53 +02:00
2026-04-14 17:19:35 +00:00
// exit
close = (): void => {
2026-04-12 19:20:37 +00:00
this.coreViewModel.closeContext(this.contextId);
2026-04-14 17:19:35 +00:00
};
handleContextClose = (): void => {
2026-04-01 08:29:39 +00:00
if (this.didMakeChanges.value == true) {
window.location.reload();
return;
}
this.isShowingStorageModal.value = false;
2026-04-14 17:19:35 +00:00
}
2024-07-21 17:55:53 +02:00
2026-04-01 08:29:39 +00:00
// init
2026-04-13 10:58:21 +00:00
constructor(public readonly coreViewModel: CoreViewModel) {
2026-04-13 19:45:52 +00:00
super("storage");
2026-04-12 19:20:37 +00:00
2026-04-01 08:29:39 +00:00
this.selectedFileName = React.createProxyState(
[this.selectedPath],
() => StorageModel.getFileNameFromString(this.selectedPath.value),
);
this.selectedFileContent = React.createProxyState(
[this.selectedPath],
() => this.getSelectedItemContent(),
);
2026-04-12 19:20:37 +00:00
// keystrokes
2026-04-14 17:19:35 +00:00
this.registerKeyStroke(CommonKeys.CloseOrCancel, this.close);
2026-04-01 08:29:39 +00:00
}
2024-07-21 17:55:53 +02:00
}