"use client" import { useMemo, useState } from "react" import { ChevronRight, Folder, ImageIcon, Loader2 } from "lucide-react" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { useDriveList } from "@/lib/api/hooks/use-drive-queries" import { fetchDrivePreviewBlob } from "@/lib/api/drive-download" import type { DriveFileInfo } from "@/lib/api/types" import { displayFileName } from "@/lib/drive/display-file-name" import { drivePreviewKind } from "@/lib/drive/drive-preview" import { DRIVE_BTN_GHOST, DRIVE_DIALOG_CONTENT, DRIVE_DIALOG_DIVIDER, DRIVE_DIALOG_OVERLAY, DRIVE_TEXT_PRIMARY, DRIVE_TEXT_SECONDARY, DRIVE_TEXT_TITLE, } from "@/lib/drive/drive-dialog-styles" import { normalizeDriveFolderPath } from "@/lib/drive/drive-sidebar-tree" import { cn } from "@/lib/utils" async function blobToDataUrl(blob: Blob): Promise { return new Promise((resolve, reject) => { const reader = new FileReader() reader.onload = () => resolve(reader.result as string) reader.onerror = () => reject(reader.error) reader.readAsDataURL(blob) }) } export function DocsDriveImagePickerDialog({ open, onOpenChange, onPickImage, }: { open: boolean onOpenChange: (open: boolean) => void onPickImage: (src: string, file: DriveFileInfo) => void | Promise }) { const [browsePath, setBrowsePath] = useState("/") const [loadingPath, setLoadingPath] = useState(null) const list = useDriveList(browsePath, 1, "", open) const folders = useMemo( () => (list.data?.files ?? []).filter((f) => f.type === "directory"), [list.data?.files] ) const images = useMemo( () => (list.data?.files ?? []).filter( (f) => f.type === "file" && drivePreviewKind(f) === "image" ), [list.data?.files] ) const crumbs = useMemo(() => { const normalized = normalizeDriveFolderPath(browsePath) if (normalized === "/") return [{ path: "/", label: "Mon Drive" }] const parts = normalized.slice(1).split("/") const out: { path: string; label: string }[] = [{ path: "/", label: "Mon Drive" }] for (let i = 0; i < parts.length; i++) { const path = "/" + parts.slice(0, i + 1).join("/") out.push({ path, label: displayFileName(parts[i]!) }) } return out }, [browsePath]) const pickImage = async (file: DriveFileInfo) => { setLoadingPath(file.path) try { const blob = await fetchDrivePreviewBlob(file) const src = await blobToDataUrl(blob) await onPickImage(src, file) onOpenChange(false) } catch { window.alert("Impossible de charger cette image depuis le Drive.") } finally { setLoadingPath(null) } } return ( Image depuis Drive Choisissez une image dans votre UltiDrive.
{crumbs.map((crumb, index) => ( {index > 0 ? : null} ))}
{list.isLoading ? (
) : folders.length === 0 && images.length === 0 ? (

Aucune image dans ce dossier.

) : (
    {folders.map((folder) => (
  • ))} {images.map((file) => (
  • ))}
)}
) }