|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { createContext, type ReactNode, useContext, useState } from 'react' |
| 4 | +import { |
| 5 | + type IntegrationsData, |
| 6 | + memberSearchMethod, |
| 7 | + type OrganizationIntegration, |
| 8 | + type WorkspaceAccess, |
| 9 | +} from '@/platform/lib/integrations/model' |
| 10 | + |
| 11 | +function useEdits() { |
| 12 | + const [records, setRecords] = useState<Record<string, OrganizationIntegration | null>>({}) |
| 13 | + const [grants, setGrants] = useState<Record<string, WorkspaceAccess> | null>(null) |
| 14 | + return { records, setRecords, grants, setGrants } |
| 15 | +} |
| 16 | + |
| 17 | +const Changes = createContext<ReturnType<typeof useEdits> | null>(null) |
| 18 | + |
| 19 | +interface IntegrationChangesProps { |
| 20 | + children: ReactNode |
| 21 | +} |
| 22 | + |
| 23 | +export function IntegrationChanges({ children }: IntegrationChangesProps) { |
| 24 | + return <Changes.Provider value={useEdits()}>{children}</Changes.Provider> |
| 25 | +} |
| 26 | + |
| 27 | +export function useOrganizationIntegrations(data: IntegrationsData | null) { |
| 28 | + const changes = useContext(Changes) |
| 29 | + if (!changes) throw new Error('Integration controls must be inside the product layout') |
| 30 | + const { records, setRecords, grants, setGrants } = changes |
| 31 | + const configured = new Map(data?.integrations.map((item) => [item.providerId, item])) |
| 32 | + for (const [id, edit] of Object.entries(records)) { |
| 33 | + if (edit) configured.set(id, edit) |
| 34 | + else configured.delete(id) |
| 35 | + } |
| 36 | + function save(next: OrganizationIntegration) { |
| 37 | + if (!data) return |
| 38 | + const current = configured.get(next.providerId) |
| 39 | + if (!data.canManage && !current) return |
| 40 | + const provider = data.providers.find((entry) => entry.id === next.providerId) |
| 41 | + if (!provider) return |
| 42 | + const requiredMethod = next.searchApproved && memberSearchMethod(provider) |
| 43 | + const updated = |
| 44 | + requiredMethod && !next.accountMethods.includes(requiredMethod) |
| 45 | + ? { ...next, accountMethods: [...next.accountMethods, requiredMethod] } |
| 46 | + : next |
| 47 | + const previouslyActive = new Set( |
| 48 | + current?.people.filter((person) => person.status !== 'revoked').map((person) => person.id) |
| 49 | + ) |
| 50 | + const revoked = new Set( |
| 51 | + next.people |
| 52 | + .filter((person) => person.status === 'revoked' && previouslyActive.has(person.id)) |
| 53 | + .map((person) => person.id) |
| 54 | + ) |
| 55 | + setRecords((previous) => { |
| 56 | + const updates = { ...previous } |
| 57 | + if (data.canManage && revoked.size) { |
| 58 | + for (const item of configured.values()) { |
| 59 | + if (item.people.some((person) => revoked.has(person.id) && person.status !== 'revoked')) { |
| 60 | + updates[item.providerId] = { |
| 61 | + ...item, |
| 62 | + people: item.people.map((person) => |
| 63 | + revoked.has(person.id) ? { ...person, status: 'revoked' } : person |
| 64 | + ), |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + updates[next.providerId] = |
| 70 | + data.canManage || !current ? updated : { ...current, accounts: next.accounts } |
| 71 | + return updates |
| 72 | + }) |
| 73 | + } |
| 74 | + return { |
| 75 | + configured, |
| 76 | + grants: grants ?? data?.workspaceGrants ?? {}, |
| 77 | + save, |
| 78 | + remove(id: string) { |
| 79 | + if (data?.canManage) setRecords((previous) => ({ ...previous, [id]: null })) |
| 80 | + }, |
| 81 | + setAccess(workspaceId: string, access: WorkspaceAccess | null) { |
| 82 | + if (!data?.canManage) return |
| 83 | + setGrants((previous) => { |
| 84 | + const next = { ...(previous ?? data.workspaceGrants) } |
| 85 | + if (access) next[workspaceId] = access |
| 86 | + else delete next[workspaceId] |
| 87 | + return next |
| 88 | + }) |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
0 commit comments