ultisuite-client/lib/drive/drive-open-item.ts
R3D347HR4Y ad1370ea7e
Some checks are pending
E2E / Playwright e2e (push) Waiting to run
feat: enhance configuration and add new demo layouts
- Introduced turbopack alias for canvas in next.config.mjs.
- Updated package.json scripts for development and branding tasks.
- Added new dependencies for Tiptap extensions.
- Implemented new demo layouts for agenda, contacts, drive, and mail applications.
- Enhanced globals.css for improved theming and splash screen animations.
- Added OAuth callback handling for drive mounts.
- Updated layout components to integrate new demo shells and improve structure.
2026-06-12 19:10:24 +02:00

95 lines
2.9 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,
shouldOpenInUltidrawEditor,
toPreviewTarget,
} from "@/lib/drive/drive-preview"
import { driveFolderHref } from "@/lib/drive/drive-sidebar-tree"
import { buildDriveEditHref } from "@/lib/drive/drive-url"
import { openRichTextDocument } from "@/lib/drive/open-rich-text-document"
import { openUltidrawDocument } from "@/lib/drive/open-ultidraw-document"
import { stashDriveEditorReturnTo } from "@/lib/drive/drive-editor-return"
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
routeRoot?: string
}
/** 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,
routeRoot,
} = options
const allowShare = view !== "shared"
if (file.type === "directory") {
router.push(driveFolderHref(view, file.path, undefined, routeRoot))
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 (shouldOpenInUltidrawEditor(file)) {
void openUltidrawDocument(file, router).catch(() => {
window.alert("Impossible douvrir le dessin.")
})
return
}
if (shouldOpenInRichTextEditor(file)) {
void openRichTextDocument(file, router).catch(() => {
window.alert("Impossible douvrir le document.")
})
return
}
if (shouldOpenInOnlyOffice(file)) {
stashDriveEditorReturnTo()
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.")
})
}