|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { Chip, ChipLink, toast } from '@sim/emcn' |
| 4 | +import type { OrganizationAccountConnectionResponse } from '@/lib/api/contracts/organization-accounts' |
| 5 | +import { connectorDisplayName } from '@/lib/sim-search/connectors' |
| 6 | +import { DisconnectAccountMenu } from '@/app/o/[organizationId]/integrations/disconnect-account-menu' |
| 7 | +import { SettingsResourceRow } from '@/app/workspace/[workspaceId]/settings/components/settings-resource-row' |
| 8 | +import { |
| 9 | + useConnectOrganizationAccount, |
| 10 | + useOrganizationAccounts, |
| 11 | + useReconnectPersonalOrganizationAccount, |
| 12 | +} from '@/hooks/queries/organization-accounts' |
| 13 | + |
| 14 | +const SOURCES: Record<string, string> = { |
| 15 | + 'google-drive': 'google_drive', |
| 16 | + gmail: 'gmail', |
| 17 | + 'google-email': 'gmail', |
| 18 | + 'google-calendar': 'google_calendar', |
| 19 | + 'google-docs': 'google_drive', |
| 20 | + 'google-sheets': 'google_drive', |
| 21 | + 'google-slides': 'google_drive', |
| 22 | + 'github-repositories': 'github', |
| 23 | + slack: 'slack', |
| 24 | + jira: 'jira', |
| 25 | + confluence: 'confluence', |
| 26 | +} |
| 27 | + |
| 28 | +/** Uses the member OAuth enrollment flow directly, without creating or configuring an index. */ |
| 29 | +export function LiveMemberIntegrations({ |
| 30 | + organizationId, |
| 31 | + search, |
| 32 | +}: { |
| 33 | + organizationId: string |
| 34 | + search: string |
| 35 | +}) { |
| 36 | + const inventory = useOrganizationAccounts(organizationId) |
| 37 | + const connect = useConnectOrganizationAccount() |
| 38 | + const reconnect = useReconnectPersonalOrganizationAccount() |
| 39 | + const navigate = (result: OrganizationAccountConnectionResponse) => |
| 40 | + window.location.assign(result.authorizationUrl ?? result.invitationLink) |
| 41 | + const onError = (error: Error) => toast.error(error.message) |
| 42 | + if (inventory.isError) |
| 43 | + return ( |
| 44 | + <div className='p-4'> |
| 45 | + <p>Could not load connected accounts.</p> |
| 46 | + <Chip onClick={() => void inventory.refetch()}>Retry</Chip> |
| 47 | + </div> |
| 48 | + ) |
| 49 | + if (!inventory.data) return <p className='p-4'>Loading connected accounts…</p> |
| 50 | + const data = inventory.data |
| 51 | + const options = |
| 52 | + data.credentialGroup?.status === 'active' |
| 53 | + ? data.credentialGroup.options.filter( |
| 54 | + (option) => option.status === 'active' && SOURCES[option.provider] |
| 55 | + ) |
| 56 | + : [] |
| 57 | + const visible = options.filter((option) => |
| 58 | + `${option.label} ${option.provider}`.toLowerCase().includes(search.toLowerCase()) |
| 59 | + ) |
| 60 | + return ( |
| 61 | + <div className='flex flex-col gap-3'> |
| 62 | + <p className='px-4 py-2 text-[var(--text-muted)] text-small'> |
| 63 | + Search runs directly through your accounts. Connect or reconnect here to grant permission; |
| 64 | + no indexing setup is needed. |
| 65 | + </p> |
| 66 | + {visible.map((option) => { |
| 67 | + const accounts = (data.viewerAccounts ?? []).filter( |
| 68 | + (account) => account.optionId === option.id |
| 69 | + ) |
| 70 | + const name = connectorDisplayName(SOURCES[option.provider]) |
| 71 | + return ( |
| 72 | + <SettingsResourceRow |
| 73 | + key={option.id} |
| 74 | + title={option.label || name} |
| 75 | + description={ |
| 76 | + accounts.length |
| 77 | + ? accounts |
| 78 | + .map( |
| 79 | + (account) => |
| 80 | + `${account.displayName}${account.status === 'needs_reauth' ? ' (reconnect needed)' : ''}` |
| 81 | + ) |
| 82 | + .join(', ') |
| 83 | + : `Connect your ${name} account to search as you.` |
| 84 | + } |
| 85 | + trailing={ |
| 86 | + <div className='flex items-center gap-2'> |
| 87 | + <DisconnectAccountMenu |
| 88 | + organizationId={organizationId} |
| 89 | + integrationName={name} |
| 90 | + accounts={accounts} |
| 91 | + /> |
| 92 | + {accounts.map((account) => ( |
| 93 | + <Chip |
| 94 | + key={account.credentialId} |
| 95 | + disabled={reconnect.isPending} |
| 96 | + onClick={() => |
| 97 | + reconnect.mutate(account.credentialId, { onSuccess: navigate, onError }) |
| 98 | + } |
| 99 | + > |
| 100 | + Reconnect {accounts.length > 1 ? account.displayName : ''} |
| 101 | + </Chip> |
| 102 | + ))} |
| 103 | + <Chip |
| 104 | + variant='primary' |
| 105 | + disabled={connect.isPending} |
| 106 | + onClick={() => |
| 107 | + connect.mutate( |
| 108 | + { organizationId, optionId: option.id }, |
| 109 | + { onSuccess: navigate, onError } |
| 110 | + ) |
| 111 | + } |
| 112 | + > |
| 113 | + {accounts.length ? 'Add account' : 'Connect'} |
| 114 | + </Chip> |
| 115 | + </div> |
| 116 | + } |
| 117 | + /> |
| 118 | + ) |
| 119 | + })} |
| 120 | + {options.length === 0 && ( |
| 121 | + <p className='px-4 text-small'> |
| 122 | + An organization admin needs to enable account providers in Credential Groups. |
| 123 | + </p> |
| 124 | + )} |
| 125 | + {data.canManage && ( |
| 126 | + <div className='px-4'> |
| 127 | + <ChipLink href={`/o/${organizationId}/settings/connected-accounts`}> |
| 128 | + Configure account providers |
| 129 | + </ChipLink> |
| 130 | + </div> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + ) |
| 134 | +} |
0 commit comments