ultisuite-client/lib/drive/drive-open-item.ts
R3D347HR4Y cdff12490a
Some checks are pending
E2E / Playwright e2e (push) Waiting to run
hocuspocus
2026-06-09 14:31:07 +02:00

83 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { downloadDriveFile } from "@/lib/api/drive-download"
import type { DriveFileInfo } from "@/lib/api/types"
import {
drivePreviewKind,
isPreviewNavigable,
shouldOpenInOnlyOffice,
shouldOpenInRichTextEditor,
toPreviewTarget,
} from "@/lib/drive/drive-preview"
import { driveFolderHref } from "@/lib/drive/drive-sidebar-tree"
import { buildDriveEditHref } from "@/lib/drive/drive-url"
import type { DrivePreviewContext } from "@/lib/stores/drive-ui-store"
export interface OpenDriveItemRouter {
push: (href: string) => void
}
export interface OpenDriveItemOptions {
router: OpenDriveItemRouter
openPreview: (
files: ReturnType<typeof toPreviewTarget>[],
index: number,
context?: Partial<DrivePreviewContext>
) => void
view: "files" | "shared"
/** Items available for in-preview navigation (current folder, search hits, etc.). */
contextItems?: DriveFileInfo[]
isTrash?: boolean
}
/** Open a drive file or folder the same way as the file browser (preview, editor, download). */
export function openDriveItem(file: DriveFileInfo, options: OpenDriveItemOptions) {
const {
router,
openPreview,
view,
contextItems = [file],
isTrash = false,
} = options
const allowShare = view !== "shared"
if (file.type === "directory") {
router.push(driveFolderHref(view, file.path))
return
}
if (drivePreviewKind(file)) {
const ext = file.name.split(".").pop()?.toLowerCase() ?? ""
const richTextPreview =
ext === "md" || ext === "markdown" || ext === "txt" || ext === "html" || ext === "htm"
if (!richTextPreview) {
const navigable = contextItems
.filter((item) => isPreviewNavigable(item))
.map((item) => toPreviewTarget(item))
const index = navigable.findIndex((item) => item.path === file.path)
openPreview(navigable, index >= 0 ? index : 0, { allowShare, isTrash })
return
}
}
if (shouldOpenInRichTextEditor(file)) {
const returnTo =
typeof window !== "undefined"
? window.location.pathname + window.location.search
: undefined
router.push(buildDriveEditHref(file.path, returnTo, "richtext"))
return
}
if (shouldOpenInOnlyOffice(file)) {
const returnTo =
typeof window !== "undefined"
? window.location.pathname + window.location.search
: undefined
router.push(buildDriveEditHref(file.path, returnTo))
return
}
void downloadDriveFile(file.path, file.name, file.name).catch(() => {
window.alert("Impossible douvrir le fichier.")
})
}