Notes/apps/client/src/widgets/watched_file_update_status.ts

114 lines
3.7 KiB
TypeScript
Raw Normal View History

import { t } from "../services/i18n.js";
2023-05-03 10:23:20 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import server from "../services/server.js";
import fileWatcher from "../services/file_watcher.js";
2025-02-27 22:44:18 +01:00
import dayjs from "dayjs";
2025-03-16 00:45:46 +02:00
import type { EventData } from "../components/app_context.js";
import type FNote from "../entities/fnote.js";
2023-05-03 10:23:20 +02:00
const TPL = /*html*/`
2023-05-03 10:23:20 +02:00
<div class="dropdown watched-file-update-status-widget alert alert-warning">
<style>
.watched-file-update-status-widget {
margin: 10px;
contain: none;
}
</style>
2025-03-16 00:45:46 +02:00
<p>${t("watched_file_update_status.file_last_modified")}</p>
2023-05-03 10:23:20 +02:00
<div style="display: flex; flex-direction: row; justify-content: space-evenly;">
<button class="btn btn-sm file-upload-button">${t("watched_file_update_status.upload_modified_file")}</button>
2025-03-16 00:45:46 +02:00
<button class="btn btn-sm ignore-this-change-button">${t("watched_file_update_status.ignore_this_change")}</button>
2023-05-03 10:23:20 +02:00
</div>
</div>`;
export default class WatchedFileUpdateStatusWidget extends NoteContextAwareWidget {
2025-03-16 00:45:46 +02:00
private $filePath!: JQuery<HTMLElement>;
private $fileLastModified!: JQuery<HTMLElement>;
private $fileUploadButton!: JQuery<HTMLElement>;
private $ignoreThisChangeButton!: JQuery<HTMLElement>;
2023-05-03 10:23:20 +02:00
isEnabled() {
const { entityType, entityId } = this.getEntity();
2025-03-16 00:45:46 +02:00
return super.isEnabled()
&& !!entityType
&& !!entityId
&& !!fileWatcher.getFileModificationStatus(entityType, entityId);
2023-05-03 10:23:20 +02:00
}
doRender() {
this.$widget = $(TPL);
this.$filePath = this.$widget.find(".file-path");
this.$fileLastModified = this.$widget.find(".file-last-modified");
this.$fileUploadButton = this.$widget.find(".file-upload-button");
this.$fileUploadButton.on("click", async () => {
const { entityType, entityId } = this.getEntity();
await server.post(`${entityType}/${entityId}/upload-modified-file`, {
filePath: this.$filePath.text()
});
2025-03-16 00:45:46 +02:00
if (entityType && entityId) {
fileWatcher.fileModificationUploaded(entityType, entityId);
}
2023-05-03 10:23:20 +02:00
this.refresh();
});
this.$ignoreThisChangeButton = this.$widget.find(".ignore-this-change-button");
2025-01-09 18:07:02 +02:00
this.$ignoreThisChangeButton.on("click", () => {
2023-05-03 10:23:20 +02:00
const { entityType, entityId } = this.getEntity();
2025-03-16 00:45:46 +02:00
if (entityType && entityId) {
fileWatcher.ignoreModification(entityType, entityId);
}
2023-05-03 10:23:20 +02:00
this.refresh();
});
}
2025-03-16 00:45:46 +02:00
async refreshWithNote(note: FNote) {
2023-05-03 10:23:20 +02:00
const { entityType, entityId } = this.getEntity();
2025-03-16 00:45:46 +02:00
if (!entityType || !entityId) {
return;
}
2023-05-03 10:23:20 +02:00
const status = fileWatcher.getFileModificationStatus(entityType, entityId);
this.$filePath.text(status.filePath);
this.$fileLastModified.text(dayjs.unix(status.lastModifiedMs / 1000).format("HH:mm:ss"));
}
getEntity() {
if (!this.noteContext) {
return {};
}
const { viewScope } = this.noteContext;
2025-03-16 00:45:46 +02:00
if (viewScope?.viewMode === "attachments" && viewScope.attachmentId) {
2023-05-03 10:23:20 +02:00
return {
2025-01-09 18:07:02 +02:00
entityType: "attachments",
2023-05-03 10:23:20 +02:00
entityId: viewScope.attachmentId
};
} else {
return {
2025-01-09 18:07:02 +02:00
entityType: "notes",
2023-05-03 10:23:20 +02:00
entityId: this.noteId
};
}
}
2025-03-16 00:45:46 +02:00
openedFileUpdatedEvent(data: EventData<"openedFileUpdated">) {
2025-01-09 18:07:02 +02:00
console.log(data);
2023-05-03 10:23:20 +02:00
const { entityType, entityId } = this.getEntity();
if (data.entityType === entityType && data.entityId === entityId) {
this.refresh();
}
}
}