import { html, nothing, type PropertyValueMap } from "lit"; import { customElement } from "../utils/decorators/custom-element"; import { property, state } from "lit/decorators.js"; import { localized, msg } from "@lit/localize"; import type { FileType, FilesResultType } from "../types/files.types"; import { getExtension, getIcon, isFileEditable } from "../utils/files"; import { QueryController } from "../controllers/query-controller"; import { getApiOptions } from "../data/api"; import { repeat } from "lit/directives/repeat.js"; import { FileVersionDeleteMutationType, FileVersionMutationType, getFileVersionDeleteMutation, getFileVersionRestoreMutation, getFileVersionsKey, } from "../data/file-versions"; import { openUrl } from "../utils/urls"; import { relativeDays } from "../utils/datetime"; import { clickOnEnterAndConsumeOnSpace, clickOnSpace } from "../utils/keyboard"; import { WeavySubAppComponent } from "../classes/weavy-sub-app-component"; import { ShadowPartsController } from "../controllers/shadow-parts-controller"; import { FileEditEventType, FileVersionSelectEventType } from "../types/files.events"; import { NamedEvent } from "../types/generic.types"; import type { EditorDraftType } from "../types/editor.types"; import { getDraftOptions, getFileDraftKey, removeDraft } from "../data/drafts"; import "./ui/wy-dropdown"; import "./ui/wy-icon"; import "./ui/wy-item"; import "./ui/wy-progress-circular"; import "./wy-empty"; declare global { interface HTMLElementTagNameMap { "wy-file-versions": WyFileVersions; } } /** * File versions list for a file — displays available versions and actions (download, revert, remove). * * **Used sub components:** * * - [``](./ui/wy-item.ts) * - [``](./ui/wy-item.ts) * - [``](./ui/wy-dropdown.ts) * - [``](./ui/wy-dropdown.ts) * - [``](./ui/wy-dropdown.ts) * - [``](./ui/wy-icon.ts) * - [``](./ui/wy-progress-circular.ts) * - [``](./wy-empty.ts) * * @csspart wy-versions - Wrapper for the versions list. * * @fires {FileVersionSelectEventType} file-version-select - Emitted when a file version, or the unsaved draft (`draft` set), is selected. * @fires {FileEditEventType} edit - Emitted when the file/draft should be edited. * */ @customElement("wy-file-versions") @localized() export class WyFileVersions extends WeavySubAppComponent { /** @internal */ protected exportParts = new ShadowPartsController(this); /** * Source file whose version timeline should be displayed. */ @property({ attribute: false }) file?: FileType; /** * Currently selected version. Falls back to the latest when unset. */ @property({ attribute: false }) activeVersion?: FileType; /** * Whether the unsaved draft is the selected entry rather than a stored version. */ @property({ type: Boolean }) activeDraft: boolean = false; /** * Id used for handling drafts. * @internal */ @state() draftKey?: string; /** * Query controller for getting file draft. * @internal */ private draftQuery = new QueryController(this); /** * Query controller fetching the file versions list. * @internal */ private fileVersionsQuery = new QueryController(this); /** * Mutation used when reverting to an older version. * @internal */ private fileVersionRestoreMutation?: FileVersionMutationType; /** * Mutation used when removing a version. * @internal */ private fileVersionDeleteMutation?: FileVersionDeleteMutationType; /** * Selects the provided version and emits `file-version-select`. * * @param versionFile - Version to select. */ selectVersion(versionFile: FileType) { this.activeVersion = versionFile; this.activeDraft = false; this.dispatchFileVersionSelect(versionFile); } /** * Selects the unsaved draft and emits `file-version-select` with `draft` set, so the host can show the draft * without entering edit mode. */ selectDraft() { if (!this.file) { return; } this.activeVersion = this.file; this.activeDraft = true; this.dispatchFileVersionSelect(this.file, true); } /** * Emit a `file-version-select` event with the chosen version. * * @internal * @param versionFile - Version to announce. * @returns Whether the event was not canceled. */ dispatchFileVersionSelect(versionFile: FileType, draft: boolean = false) { const event: FileVersionSelectEventType = new (CustomEvent as NamedEvent)("file-version-select", { detail: { versionFile, draft }, }); return this.dispatchEvent(event); } /** * Emit a `edit` event for the file. * * @internal * @returns Whether the event was not canceled. */ dispatchFileEdit() { const file = this.file; if (!file) { return; } const event: FileEditEventType = new (CustomEvent as NamedEvent)("edit", { detail: { file, edit: true }, }); return this.dispatchEvent(event); } /** * Revert the file to the supplied version. * * @internal * @param versionFile - Version to restore. */ handleRevert(versionFile: FileType) { void this.fileVersionRestoreMutation?.mutate({ versionFile }); this.selectVersion(versionFile); } /** * Remove the supplied version and adjust the active selection if needed. * * @internal * @param versionFile - Version to delete. */ handleRemove(versionFile: FileType) { void this.fileVersionDeleteMutation?.mutate({ versionFile }); if (this.activeVersion === versionFile) { this.activeVersion = this.file; } } /** * Download the provided version via the browser. * * @internal * @param file - Version file to download. */ triggerDownload(file: FileType) { openUrl(file.download_url, "_top", file.name, true); } /** * Removes any draft for the current file. */ removeDraft() { if (this.weavy && this.draftKey) { void removeDraft(this.weavy, this.draftKey) } } override async willUpdate(changedProperties: PropertyValueMap): Promise { super.willUpdate(changedProperties); if ( (changedProperties.has("weavy") || changedProperties.has("file") || changedProperties.has("app")) && this.weavy && this.file && this.app ) { await this.fileVersionsQuery.trackQuery( getApiOptions( this.weavy, getFileVersionsKey(this.app, this.file), `/api/files/${this.file.id}/versions`, ), ); this.fileVersionRestoreMutation = getFileVersionRestoreMutation(this.weavy, this.app, this.file); this.fileVersionDeleteMutation = getFileVersionDeleteMutation(this.weavy, this.app, this.file); } if ( (changedProperties.has("weavy") || changedProperties.has("file")) && this.weavy ) { this.draftKey = this.file ? getFileDraftKey(this.file) : undefined; } if ((changedProperties.has("weavy") || changedProperties.has("draftKey")) && this.weavy) { if (this.draftKey) { await this.draftQuery.trackQuery(getDraftOptions(this.weavy, this.draftKey)); } else { this.draftQuery.untrackQuery(); } } } override render() { const { data, isPending } = this.fileVersionsQuery.result ?? { isPending: true }; if (isPending) { return html``; } let draft; const { data: draftContent } = this.draftQuery.result; // A cached draft the user can no longer act on (revoked permission, editing turned off) is left out. if (this.file && draftContent && isFileEditable(this.file, this.app, this.componentFeatures)) { const draftIcon = this.file && getIcon(this.file.name || "").icon; const ext = getExtension(this.file.name); const modifiedAt = new Date(draftContent.updatedAt); const dateFull = new Intl.DateTimeFormat(this.weavy?.locale, { dateStyle: "full", timeStyle: "short", }).format(modifiedAt); const dateFromNow = relativeDays(this.weavy?.locale, new Date(modifiedAt)); draft = html` this.selectDraft()} @keydown=${clickOnEnterAndConsumeOnSpace} @keyup=${clickOnSpace} > ${msg("Unsaved draft")} this.dispatchFileEdit()}> ${msg("Edit")} this.removeDraft()}> ${msg("Remove", { desc: "Button action to remove" })} `; } else { draft = nothing; } return data?.data ? html` ${draft} ${repeat( data.data, (versionFile) => versionFile.id, (versionFile: FileType, index) => { if (!this.file) { return nothing; } const versionIcon = getIcon(versionFile.name || "").icon; const num = data.data ? data.data.length - index : NaN; const ext = getExtension(versionFile.name); const modifiedAt = new Date(versionFile.updated_at || versionFile.created_at); const isExternal = Boolean(this.file.external_url); const dateFull = new Intl.DateTimeFormat(this.weavy?.locale, { dateStyle: "full", timeStyle: "short", }).format(modifiedAt); const dateFromNow = relativeDays(this.weavy?.locale, new Date(modifiedAt)); return isExternal ? html` ${msg("Versions are not available for external cloud files.")} ` : html` this.selectVersion(versionFile)} @keydown=${clickOnEnterAndConsumeOnSpace} @keyup=${clickOnSpace} > ${num}. ${versionFile.name} ${versionFile.updated_by ? html`· ${versionFile.updated_by?.name}` : nothing} this.triggerDownload(versionFile)}> ${msg("Download")} ${index !== 0 ? html` this.handleRevert(versionFile)}> ${"Revert"} this.handleRemove(versionFile)}> ${msg("Remove", { desc: "Button action to remove" })} ` : nothing} `; }, )} ` : nothing; } }