"use client" import { useEffect, useState } from "react" import { Check, Copy } from "lucide-react" import { toast } from "sonner" import { TechBrandSelectLabel } from "@/components/admin/settings/tech-brand-select-label" import { SettingsCard, SettingsField, SettingsGrid, SettingsHint, SettingsToggleRow, } from "@/components/settings/settings-kit" import { useOrgSettingsStore } from "@/lib/admin-settings/org-settings-store" import type { DriveMountOAuthProvider, DriveMountOAuthSettings } from "@/lib/admin-settings/org-settings-types" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { buildDriveMountOAuthRedirectURI } from "@/lib/drive/drive-mount-oauth" const PROVIDERS: { id: DriveMountOAuthProvider label: string hint: string icon: string }[] = [ { id: "google", label: "Google Drive", hint: "Console Google Cloud — API Drive, redirect URI ci-dessous", icon: "logos:google-drive", }, { id: "dropbox", label: "Dropbox", hint: "App Dropbox — permissions files.metadata.read, files.content.read/write", icon: "logos:dropbox", }, { id: "microsoft", label: "Microsoft OneDrive", hint: "Azure AD — Microsoft Graph Files.ReadWrite", icon: "logos:microsoft-onedrive", }, ] const SECRET_KEYS: Record = { google: "mount_oauth_google", dropbox: "mount_oauth_dropbox", microsoft: "mount_oauth_microsoft", } export function DriveMountOAuthSection({ draft, onChange, embedded = false, }: { draft: DriveMountOAuthSettings onChange: (next: DriveMountOAuthSettings) => void embedded?: boolean }) { const secrets = useOrgSettingsStore((s) => s.meta?.secrets) const [redirectUri, setRedirectUri] = useState("") const [copied, setCopied] = useState(false) useEffect(() => { setRedirectUri(buildDriveMountOAuthRedirectURI()) }, []) const updateProvider = (provider: DriveMountOAuthProvider, patch: Partial) => { onChange({ ...draft, [provider]: { ...draft[provider], ...patch }, }) } const copyRedirectUri = async () => { const uri = redirectUri || buildDriveMountOAuthRedirectURI() try { await navigator.clipboard.writeText(uri) setCopied(true) toast.success("URI de redirection copiée") window.setTimeout(() => setCopied(false), 2000) } catch { toast.error("Impossible de copier l'URI") } } const content = ( <>
{PROVIDERS.map(({ id, label, hint, icon }) => { const provider = draft[id] const configured = Boolean(secrets?.[SECRET_KEYS[id]]?.configured) return (
{label} } description={hint} checked={provider.enabled} onCheckedChange={(enabled) => updateProvider(id, { enabled })} /> {provider.enabled ? ( updateProvider(id, { client_id: e.target.value })} autoComplete="off" /> Secret configuré ) : undefined } > updateProvider(id, { client_secret: e.target.value })} placeholder={configured ? "•••••••• (laisser vide pour conserver)" : "Coller le secret"} autoComplete="off" /> ) : null}
) })}
) if (embedded) return
{content}
return ( {content} ) }