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

82 lines
2.3 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
2024-07-25 16:02:49 +02:00
import CoreViewModel from "./coreViewModel";
2024-07-22 19:52:08 +02:00
import { translations } from "../../View/translations";
2024-07-21 17:55:53 +02:00
export default class StorageViewModel {
2026-04-01 08:29:39 +00:00
storageModel: StorageModel;
2024-07-21 17:55:53 +02:00
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 => {
const path = StorageModel.stringToPathComponents(
this.selectedPath.value,
);
const content = this.storageModel.read(path);
return (
(content ?? translations.storage.notAFile) ||
translations.storage.contentEmpty
);
};
2026-04-01 08:29:39 +00:00
deleteSelectedItem = (): void => {
const path = StorageModel.stringToPathComponents(
this.selectedPath.value,
);
this.lastDeletedItemPath.value = this.selectedPath.value;
2024-07-25 16:02:49 +02:00
2026-04-01 08:29:39 +00:00
this.storageModel.removeRecursively(path);
this.didMakeChanges.value = true;
};
2026-04-01 08:29:39 +00:00
removeJunk = (): void => {
this.storageModel.removeJunk();
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 => {
this.isShowingStorageModal.value = true;
};
2024-07-21 17:55:53 +02:00
2026-04-01 08:29:39 +00:00
hideStorageModal = (): void => {
if (this.didMakeChanges.value == true) {
window.location.reload();
return;
}
this.isShowingStorageModal.value = false;
};
2024-07-21 17:55:53 +02:00
2026-04-01 08:29:39 +00:00
// init
constructor(
public coreViewModel: CoreViewModel,
storageModel: StorageModel,
) {
this.storageModel = storageModel;
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(),
);
}
2024-07-21 17:55:53 +02:00
}