"use client"; import { useState } from "react"; import { useForm, Controller, useFormContext } from "react-hook-form"; import { Dialog } from "@calcom/features/components/controlled-dialog"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { ServiceAccountKey } from "@calcom/lib/server/serviceAccountKey"; import { serviceAccountKeySchema } from "@calcom/prisma/zod-utils"; import { trpc } from "@calcom/trpc/react"; import { Badge } from "@calcom/ui/components/badge"; import { InfoBadge } from "@calcom/ui/components/badge"; import { Button } from "@calcom/ui/components/button"; import { DialogContent, DialogFooter, ConfirmationDialogContent } from "@calcom/ui/components/dialog"; import { EmptyScreen } from "@calcom/ui/components/empty-screen"; import { Form } from "@calcom/ui/components/form"; import { TextAreaField } from "@calcom/ui/components/form"; import { TextField } from "@calcom/ui/components/form"; import { SelectField } from "@calcom/ui/components/form"; import { Switch } from "@calcom/ui/components/form"; import { SkeletonContainer, SkeletonText } from "@calcom/ui/components/skeleton"; import { DropdownActions } from "@calcom/ui/components/table"; import { showToast } from "@calcom/ui/components/toast"; interface DelegationItemProps { delegation: { id: string; domain: string; enabled: boolean; serviceAccountClientId: string | null; workspacePlatform: { name: string; slug: string; }; }; toggleDelegation: (delegation: DelegationItemProps["delegation"]) => void; onEdit: (delegation: DelegationItemProps["delegation"]) => void; onDelete: (id: string) => void; } type WorkspacePlatform = { id: number; name: string; slug: string; }; function getWorkspacePlatformOptions(workspacePlatforms: WorkspacePlatform[]) { return workspacePlatforms.map((platform) => ({ value: platform.slug, label: platform.name, })); } const SkeletonLoader = () => { return (
); }; function DelegationListItemActions({ delegation, toggleDelegation, onEdit, onDelete, }: { delegation: DelegationItemProps["delegation"]; toggleDelegation: DelegationItemProps["toggleDelegation"]; onEdit: (delegation: DelegationItemProps["delegation"]) => void; onDelete: (id: string) => void; }) { const { t } = useLocale(); return (
toggleDelegation(delegation)} /> onEdit(delegation), icon: "pencil", }, { id: "delete", label: t("delete"), disabled: true, onClick: () => onDelete(delegation.id), icon: "trash", }, ]} />
); } function DelegationListItem({ delegation, toggleDelegation, onEdit, onDelete }: DelegationItemProps) { const { t } = useLocale(); return (
  • {delegation.serviceAccountClientId}
    https://www.googleapis.com/auth/calendar
    {delegation.workspacePlatform.name} {delegation.domain}
  • ); } type CreateDelegationData = { domain: string; workspacePlatformSlug: string; enabled: boolean; serviceAccountKey: ServiceAccountKey; }; type CreateDelegationFormData = Omit & { serviceAccountKey: string; }; type EditDelegationData = { id: string; domain: string; workspacePlatformSlug: string; enabled: boolean; }; function CreateDelegationDialog({ isOpen, onClose, workspacePlatforms, handleCreate, }: { isOpen: boolean; onClose: () => void; workspacePlatforms: WorkspacePlatform[]; handleCreate: (data: CreateDelegationData) => void; }) { const { t } = useLocale(); const form = useForm(); const handleSubmit = (values: CreateDelegationFormData) => { try { const parsedKey = JSON.parse(values.serviceAccountKey); const validatedKey = serviceAccountKeySchema.safeParse(parsedKey); if (!validatedKey.success) { form.setError("serviceAccountKey", { message: t("invalid_service_account_key") }); return; } handleCreate({ ...values, serviceAccountKey: validatedKey.data, }); } catch (e) { console.error(e); form.setError("serviceAccountKey", { message: t("invalid_service_account_key") }); return; } }; return (
    ); } function EditDelegationDialog({ isOpen, onClose, delegation, workspacePlatforms, handleEdit, }: { isOpen: boolean; onClose: () => void; delegation: DelegationItemProps["delegation"]; workspacePlatforms: WorkspacePlatform[]; handleEdit: (data: EditDelegationData) => void; }) { const { t } = useLocale(); const form = useForm({ defaultValues: { id: delegation.id, domain: delegation.domain, workspacePlatformSlug: delegation.workspacePlatform.slug, }, }); return (
    ); } function DelegationFormFields({ workspacePlatforms, isCreate, }: { workspacePlatforms: WorkspacePlatform[]; isCreate: boolean; }) { const { t } = useLocale(); const form = useFormContext(); return (
    { const platformOptions = getWorkspacePlatformOptions(workspacePlatforms); const selectedPlatform = platformOptions.find((opt) => opt.value === value); return ( onChange(option?.value)} value={selectedPlatform} options={platformOptions} /> ); }} /> {isCreate && ( )}
    ); } function DelegationCredentialList() { const { t } = useLocale(); const utils = trpc.useContext(); const { data: delegations, isLoading, error } = trpc.viewer.delegationCredential.list.useQuery(); const updateMutation = trpc.viewer.delegationCredential.update.useMutation({ onSuccess: () => utils.viewer.delegationCredential.list.invalidate(), onError: (error) => { showToast(error.message, "error"); }, }); const toggleEnabledMutation = trpc.viewer.delegationCredential.toggleEnabled.useMutation({ onSuccess: (data) => { if (data) { showToast( data.enabled ? t("delegation_credential_enabled") : t("delegation_credential_disabled"), "success" ); utils.viewer.delegationCredential.list.invalidate(); } }, onError: (error) => { showToast(error.message, "error"); }, }); const createMutation = trpc.viewer.delegationCredential.add.useMutation({ onSuccess: () => utils.viewer.delegationCredential.list.invalidate(), onError: (error) => { showToast(error.message, "error"); }, }); const deleteMutation = trpc.viewer.delegationCredential.delete.useMutation({ onSuccess: () => utils.viewer.delegationCredential.list.invalidate(), onError: (error) => { showToast(error.message, "error"); }, }); const onDelete = (id: string) => { deleteMutation.mutate({ id }); }; const [delegationToToggle, setDelegationToToggle] = useState( null ); const toggleDelegation = (delegation: DelegationItemProps["delegation"]) => { setDelegationToToggle(delegation); }; const handleToggleConfirm = () => { if (delegationToToggle) { toggleEnabledMutation.mutate({ id: delegationToToggle.id, enabled: !delegationToToggle.enabled, }); setDelegationToToggle(null); } }; const [createEditDialog, setCreateEditDialog] = useState<{ isOpen: boolean; delegation: DelegationItemProps["delegation"] | null; }>({ isOpen: false, delegation: null, }); const { data: workspacePlatforms, isLoading: isLoadingWorkspacePlatforms } = trpc.viewer.delegationCredential.listWorkspacePlatforms.useQuery(); const enabledWorkspacePlatforms = workspacePlatforms?.filter((platform) => platform.enabled) || []; const onEditClick = (delegation: DelegationItemProps["delegation"]) => { setCreateEditDialog({ isOpen: true, delegation }); }; const onCreateClick = () => setCreateEditDialog({ isOpen: true, delegation: null }); const handleCreate = (data: CreateDelegationData) => { createMutation.mutate({ domain: data.domain, workspacePlatformSlug: data.workspacePlatformSlug, serviceAccountKey: data.serviceAccountKey, }); setCreateEditDialog({ isOpen: false, delegation: null }); }; const handleEdit = (data: EditDelegationData) => { if (!createEditDialog.delegation) { return; } updateMutation.mutate({ id: createEditDialog.delegation.id, domain: data.domain, workspacePlatformSlug: data.workspacePlatformSlug, }); setCreateEditDialog({ isOpen: false, delegation: null }); }; const AddDwDButton = () => { return ( ); }; if (isLoading || isLoadingWorkspacePlatforms) { return ; } if (error) { return
    {t(error.message)}
    ; } if (!delegations || !workspacePlatforms) { return
    {t("something_went_wrong")}
    ; } return (
    {delegations?.length ? ( <>
      {delegations.map( (delegation) => delegation && ( ) )}
    {/* Disable till we thoroughly test multiple delegation credentials support */} {/* */} ) : ( } /> )} {createEditDialog.delegation ? ( setCreateEditDialog({ isOpen: false, delegation: null })} delegation={createEditDialog.delegation} workspacePlatforms={enabledWorkspacePlatforms} handleEdit={handleEdit} /> ) : ( setCreateEditDialog({ isOpen: false, delegation: null })} workspacePlatforms={enabledWorkspacePlatforms} handleCreate={handleCreate} /> )} setDelegationToToggle(null)} onConfirm={handleToggleConfirm} />
    ); } function MembersThatWillBeAffectedOnDisablingDelegationCredential({ delegationCredentialId, }: { delegationCredentialId: string; }) { const { t } = useLocale(); const { data: affectedMembers, isLoading: isLoadingAffectedMembers } = trpc.viewer.delegationCredential.getAffectedMembersForDisable.useQuery({ id: delegationCredentialId }); return (
    {t("members_affected_by_disabling_delegation_credential")} {isLoadingAffectedMembers ? (
    {t("loading")}
    ) : affectedMembers?.length ? ( <>
      {affectedMembers.slice(0, 5).map((m) => (
    • {m.name ? `${m.name} (${m.email})` : m.email}
    • ))}
    {affectedMembers.length > 5 && (

    {t("and_count_more", { count: affectedMembers.length - 5 })}

    )} ) : (
    {t("no_members_affected_by_disabling_delegation_credential")}
    )}
    ); } const ToggleDelegationDialog = ({ delegation, onConfirm, onClose, }: { delegation: DelegationItemProps["delegation"] | null; onConfirm: () => void; onClose: () => void; }) => { const { t } = useLocale(); if (!delegation) { return null; } const isDisablingDelegation = delegation.enabled; return ( (!open ? onClose() : undefined)}>

    {t( isDisablingDelegation ? "disable_delegation_credential_description" : "enable_delegation_credential_description" )}

    {isDisablingDelegation && ( )}
    ); }; export default function DelegationCredentialListPage() { return ; }