import { useEffect, useRef, useState } from "react"; import type { ApiSettings, FileConnectorScope, FileConnectorTargetOption, FilesConnectorsUiCapability } from "@govoplan/core-webui"; import { AdminPageLayout, Card, adminErrorMessage, useDeltaWatermarks, usePlatformUiCapability } from "@govoplan/core-webui"; import { fetchGroupsDelta, fetchUsersDelta, type GroupSummary, type UserAdminItem } from "../../api/admin"; import { loadDeltaRows } from "./utils/deltaRows"; type Props = { settings: ApiSettings; scopeType: Extract; canWrite: boolean; }; const copy: Record = { user: { title: "i18n:govoplan-access.user_file_connections.996444a0", description: "i18n:govoplan-access.user_scoped_file_server_connections_and_credenti.ae7a4be2", targetLabel: "i18n:govoplan-access.user.9f8a2389", panelTitle: "i18n:govoplan-access.user_file_connections.996444a0" }, group: { title: "i18n:govoplan-access.group_file_connections.73985bda", description: "i18n:govoplan-access.group_scoped_file_server_connections_and_credent.f32a51bc", targetLabel: "i18n:govoplan-access.group.171a0606", panelTitle: "i18n:govoplan-access.group_file_connections.73985bda" } }; export default function FileConnectorsPanel({ settings, scopeType, canWrite }: Props) { const fileConnectorsUi = usePlatformUiCapability("files.connectors"); const FileConnectorScopeManager = fileConnectorsUi?.FileConnectorScopeManager ?? null; const [targets, setTargets] = useState([]); const usersRef = useRef([]); const groupsRef = useRef([]); const { getDeltaWatermark, setDeltaWatermark, resetDeltaWatermark } = useDeltaWatermarks(); const [loadingTargets, setLoadingTargets] = useState(Boolean(FileConnectorScopeManager)); const [targetError, setTargetError] = useState(""); useEffect(() => { if (!FileConnectorScopeManager) { setTargets([]); setLoadingTargets(false); setTargetError(""); return; } usersRef.current = []; groupsRef.current = []; resetDeltaWatermark(); void loadTargets(); }, [settings.accessToken, settings.apiBaseUrl, settings.apiKey, scopeType, FileConnectorScopeManager, resetDeltaWatermark]); async function loadTargets() { setLoadingTargets(true); setTargetError(""); try { if (scopeType === "user") { const users = await loadDeltaRows(usersRef.current, "access:file-connector-users", getDeltaWatermark, setDeltaWatermark, (since) => fetchUsersDelta(settings, { since }), (response) => response.users, (user) => user.id, "access_user", sortUsers); usersRef.current = users; setTargets(users.map((user) => ({ id: user.id, label: user.display_name || user.email, secondary: user.display_name ? user.email : null }))); } else { const groups = await loadDeltaRows(groupsRef.current, "access:file-connector-groups", getDeltaWatermark, setDeltaWatermark, (since) => fetchGroupsDelta(settings, { since }), (response) => response.groups, (group) => group.id, "access_group", sortGroups); groupsRef.current = groups; setTargets(groups.map((group) => ({ id: group.id, label: group.name, secondary: group.slug }))); } } catch (err) { setTargets([]); setTargetError(adminErrorMessage(err)); } finally { setLoadingTargets(false); } } const labels = copy[scopeType]; if (!FileConnectorScopeManager) { return (

i18n:govoplan-access.install_and_enable_the_files_module_to_manage_fi.f842c153

); } return ( ); } function sortUsers(left: UserAdminItem, right: UserAdminItem): number { return left.email.localeCompare(right.email); } function sortGroups(left: GroupSummary, right: GroupSummary): number { return left.name.localeCompare(right.name) || left.slug.localeCompare(right.slug); }