import { html, nothing } from "lit"; import { localized, msg, str } from "@lit/localize"; import { fileSizeAsString, getExtension, getIcon, getProvider, handleSelectFilename } from "../utils/files"; import type { FileOrderByType, FileOrderType, FilesResultType, FileType } from "../types/files.types"; import type { FileDeleteForeverEventType, FileEditEventType, FileEditNameEventType, FileRestoreEventType, FileSubscribeEventType, FileTrashEventType, } from "../types/files.events"; import { repeat } from "lit/directives/repeat.js"; import { type WeavyType } from "../contexts/weavy-context"; import { inputConsume, clickOnSpace, clickOnEnterAndConsumeOnSpace } from "../utils/keyboard"; import { createRef, ref, RefOrCallback } from "lit/directives/ref.js"; import { autoFocusRef, autoScrollRef, isModifiedClick } from "../utils/dom"; import { partMap } from "../utils/directives/shadow-part-map"; import { customElement, property, state } from "lit/decorators.js"; import { ShadowPartsController } from "../controllers/shadow-parts-controller"; import { WeavySubTypeComponent } from "../classes/weavy-sub-type-component"; import { NamedEvent } from "../types/generic.types"; import { OrderEventType } from "../types/lists.events"; import { openUrl } from "../utils/urls"; import { Feature } from "../types/features.types"; import type { InfiniteData, InfiniteQueryObserverResult } from "@tanstack/query-core"; import { getFlatInfiniteResultData } from "../utils/query-cache"; export type FileOrderHeaderType = { by: FileOrderByType | undefined; title: string; col: string }[]; import hostContentsCss from "../scss/host-contents.scss"; import inputCss from "../scss/components/input.scss"; import tableCss from "../scss/components/table.scss"; import fileTableCss from "../scss/components/file-table.scss"; import pagerCss from "../scss/components/pager.scss"; import "./wy-file-menu"; import "./wy-file-draft-badge"; import "./ui/wy-icon"; import "./ui/wy-button"; import "./ui/wy-badge"; import { ActionType } from "../types/action.types"; declare global { interface HTMLElementTagNameMap { "wy-files-table": WyFilesTable; } } /** * Table view for file lists. * * **Used sub components:** * * - [``](./wy-file-menu.ts) * - [``](./wy-file-draft-badge.ts) * - [``](./ui/wy-icon.ts) * - [``](./ui/wy-button.ts) * * @csspart wy-table - Table root element * @csspart wy-table-interactive - Modifier for hoverable rows * @csspart wy-tr - Table rows * @csspart wy-th - Table header cells * @csspart wy-col-icon - Cells that contain icons/actions * @csspart wy-col-icon-content - Content in icon/action cells * @csspart wy-col-name - Cells that contain name * @csspart wy-col-time - Cells that contain time * @csspart wy-col-kind - Cells that contain kind * @csspart wy-col-size - Cells that contain size * @csspart wy-sort-link - Sortable table header cell * @csspart wy-input - Input field for file rename. * @csspart wy-highlight - Modifier for highlighted row * @csspart wy-trashed - Modifier for trashed row. * @csspart wy-badge - Badge used for comment counts * @csspart wy-pager - Pager anchor for infinite scrolling detection. * @csspart wy-pager-overlay - Modifier for overlaying pager. * @csspart wy-pager-top - Modifier for pager placed in the top. * @csspart wy-pager-bottom - Modifier for pager placed in the bottom. * * @fires {WyActionEventType} wy-action - Emitted when an action is performed on an entity. * @fires {OrderEventType} order - Emitted when the user changes sort order. */ @customElement("wy-files-table") @localized() export class WyFilesTable extends WeavySubTypeComponent { static override styles = [inputCss, tableCss, fileTableCss, pagerCss, hostContentsCss]; /** @internal */ protected exportParts = new ShadowPartsController(this); /** * Infinite query result backing the table view. * * @internal */ @property({ attribute: false }) infiniteQueryResult?: InfiniteQueryObserverResult>; /** * Active sort order for the table. */ @property({ attribute: false }) order: FileOrderType = { by: "name", descending: false }; /** * File id that is selected. */ @property({ type: Number }) selectedId?: number = NaN; /** * Current file id in rename mode. * * @internal */ @state() isRenamingId?: number = NaN; /** * File id that should be highlighted and scrolled into view. * * @internal */ @state() highlightId?: number = NaN; /** * Reference to the pager element for loading previous items. */ previousPagerRef: RefOrCallback = createRef(); /** * Reference to the pager element for loading next items. */ nextPagerRef: RefOrCallback = createRef(); constructor() { super(); this.addEventListener("wy-action", (e) => { if (e.detail.action === ActionType.EditName) { this.isRenamingId = e.detail.file ? e.detail.file.id : NaN; } else if (e.detail.action === ActionType.Rename) { this.isRenamingId = NaN; } }); } /** * Emit an `order` event with the provided sort order. * * @param order - Order to apply. * @returns Whether the event was not canceled. */ dispatchOrder(order: FileOrderType) { const event: OrderEventType = new (CustomEvent as NamedEvent)("order", { detail: { order } }); return this.dispatchEvent(event); } /** * Render the complete file table layout. * * @param files - Files to render. * @param order - Current table order. * @param isRenamingId - File id in rename mode. * @param highlightId - File id to highlight. * @param highlightRef - Reference for scrolling highlighted rows. */ renderFileTable(files: FileType[], order?: FileOrderType, isRenamingId?: number, highlightId?: number) { return files && files.length ? html` ${this.renderFileTableHeaders.call(this, order)} ${repeat( files, (file) => file.id, (file) => this.renderFileTableRow.call(this, this.weavy, { file }, isRenamingId, highlightId), )} ${files.length ? html`` : nothing} ${files.length ? html`` : nothing}
` : nothing; } /** * Render table header cells with sorting affordances. * * @param order - Current sort order. */ renderFileTableHeaders(order?: FileOrderType) { const headers: FileOrderHeaderType = [ { col: "icon", by: undefined, title: "" }, // File icon { col: "name", by: "name", title: msg("Name") }, { col: "icon", by: undefined, title: "" }, // Comments icon { col: "time", by: "updated_at", title: msg("Modified") }, { col: "kind", by: undefined, title: msg("Kind") }, { col: "size", by: "size", title: msg("Size") }, { col: "icon", by: undefined, title: "" }, // Menu ]; return html` ${headers.map((header) => { if (header.title) { const active = header.by === order?.by; const onHeaderClick = (e: Event) => { e.preventDefault(); header.by && this.dispatchOrder({ by: header.by, descending: active && !order?.descending }); }; return html` ${header.by ? html`
${header.title} ${(active && html``) || nothing}
` : header.title} `; } else { return html``; } })} `; } /** * Render a single file table row. * * @param weavy - Current Weavy context. * @param row - Row data containing the file. * @param isRenamingId - File id in rename mode. * @param highlightId - File id to highlight. */ renderFileTableRow( weavy: WeavyType | undefined, { file }: { file: FileType }, isRenamingId?: number, highlightId?: number, ) { const fileSize = !file.provider || file.size > 0 ? fileSizeAsString(file.size) : nothing; const fileChangedAt = file.updated_at || file.created_at; const fileDateFull = new Intl.DateTimeFormat(weavy?.locale, { dateStyle: "full", timeStyle: "short" }).format( new Date(fileChangedAt), ); const fileDateShort = new Intl.DateTimeFormat(weavy?.locale, { dateStyle: "short" }).format( new Date(fileChangedAt), ); const isRenaming = Boolean( this.componentFeatures?.allowsFeature(Feature.FileRename) && isRenamingId && isRenamingId === file.id, ); const isSelected = this.selectedId === file.id; const { icon } = getIcon(file.name); const ext = getExtension(file.name); const provider = getProvider(file.provider); const handleRename = (e: Event) => { e.stopImmediatePropagation(); const renameTarget = e.target as HTMLInputElement; if ( this.componentFeatures?.allowsFeature(Feature.FileRename) && renameTarget.value && renameTarget.value !== file.name ) { this.dispatchAction({ action: ActionType.Rename, file: { ...file, name: renameTarget.value } }); } else { this.dispatchAction({ action: ActionType.EditName, file: null }); } }; const handleRenameKey = (e: KeyboardEvent & Event) => { const target = e.target as HTMLInputElement; if (e.key === "Escape") { e.preventDefault(); target.value = file.name; target.blur(); } else if (e.key === "Enter") { e.preventDefault(); target.blur(); } }; const highlight = Boolean(highlightId && highlightId === file.id); return html` { if (file.is_trashed) { return; } if (isModifiedClick(e)) { if (!e.defaultPrevented && this.dispatchAction({ action: ActionType.Open, file })) { e.stopPropagation(); e.preventDefault(); openUrl(file.download_url ?? file.external_url, "_blank"); } } else { !e.defaultPrevented && this.dispatchAction({ action: ActionType.Select, file }); } }} ${highlight ? ref(autoScrollRef({ block: "center", behavior: "instant" })) : nothing} > ${isRenaming ? html` { inputConsume(e); handleRenameKey(e); }} @click=${(e: Event) => e.preventDefault()} @focus=${handleSelectFilename} ${ref(autoFocusRef())} /> ` : html`${file.name}`} ${this.componentFeatures?.allowsFeature(Feature.Comments) && file.comments?.count ? html` { if (!e.defaultPrevented && !file.is_trashed) { (e.target as HTMLElement).blur(); this.dispatchAction({ action: ActionType.Comment, file }); e.stopPropagation(); } }} title=${msg(str`${file.comments.count} comments`)} > ${file.comments.count} ` : nothing} ${file.kind} ${fileSize} this.dispatchAction({ action: ActionType.Edit, file: e.detail.file })} @edit-name=${(e: FileEditNameEventType) => this.dispatchAction({ action: ActionType.EditName, file: e.detail.editName ? e.detail.file : null, })} @trash=${(e: FileTrashEventType) => this.dispatchAction({ action: ActionType.Trash, file: e.detail.file })} @restore=${(e: FileRestoreEventType) => this.dispatchAction({ action: ActionType.Restore, file: e.detail.file })} @delete-forever=${(e: FileDeleteForeverEventType) => this.dispatchAction({ action: ActionType.DeleteForever, file: e.detail.file })} @subscribe=${(e: FileSubscribeEventType) => this.dispatchAction({ action: e.detail.subscribe ? ActionType.Subscribe : ActionType.Unsubscribe, file: e.detail.file, })} > `; } override render() { const { data } = this.infiniteQueryResult || {}; const files = getFlatInfiniteResultData(data); if (files && files.length) { return this.renderFileTable.call(this, files, this.order, this.isRenamingId, this.highlightId); } return nothing; } }