|
1 | 1 | 'use client' |
2 | 2 |
|
3 | 3 | import { useState } from 'react' |
4 | | -import { ToolPermissions } from '@/app/(product)/o/[organizationId]/integrations/_components/tool-permissions/tool-permissions' |
| 4 | +import { EmptyState } from '@sim/emcn/empty-state' |
| 5 | +import { CountedLabel } from '@sim/emcn/overflow-text/counted-label' |
| 6 | +import { SearchField } from '@sim/emcn/search-field' |
| 7 | +import { SegmentedControl } from '@sim/emcn/segmented-control' |
| 8 | +import { ControlBar } from '@/app/(product)/_components/control-bar/control-bar' |
| 9 | +import styles from '@/app/(product)/o/[organizationId]/integrations/_components/fixtures/tool-permissions/tool-permissions.module.css' |
5 | 10 | import type { |
6 | 11 | CredentialToolPermissions, |
7 | 12 | IntegrationCredential, |
8 | 13 | } from '@/platform/lib/integrations/credential-model' |
9 | | -import type { ToolPermissionChange, ToolPolicy } from '@/platform/shared/integrations' |
| 14 | +import type { ToolPolicy } from '@/platform/shared/integrations' |
| 15 | + |
| 16 | +const ACTIONS = [ |
| 17 | + { value: 'allow', label: 'Allow' }, |
| 18 | + { value: 'ask', label: 'Ask' }, |
| 19 | + { value: 'block', label: 'Block' }, |
| 20 | +] as const |
| 21 | +const GROUPS = [ |
| 22 | + { kind: 'read', label: 'Read tools' }, |
| 23 | + { kind: 'write', label: 'Write tools' }, |
| 24 | +] as const |
10 | 25 |
|
11 | 26 | interface FixtureToolPermissionsProps { |
12 | 27 | account: Pick<IntegrationCredential, 'status'> |
13 | 28 | permissions: CredentialToolPermissions |
14 | 29 | onChange: (next: CredentialToolPermissions) => void |
15 | | - readOnly: boolean |
| 30 | + readOnly?: boolean |
16 | 31 | } |
17 | 32 |
|
18 | | -/** Example tool policies, edited in the product session through the live permissions owner. */ |
| 33 | +/** Example tool policies, edited in the product session. */ |
19 | 34 | export function FixtureToolPermissions({ |
20 | 35 | account, |
21 | 36 | permissions, |
22 | 37 | onChange, |
23 | 38 | readOnly, |
24 | 39 | }: FixtureToolPermissionsProps) { |
25 | 40 | const [query, setQuery] = useState('') |
26 | | - const override = (kind: 'read' | 'write', policy: ToolPolicy | null) => |
27 | | - policy === permissions[kind] ? null : policy |
28 | | - function apply(change: ToolPermissionChange) { |
29 | | - if (change.kind === 'group') |
30 | | - onChange({ |
31 | | - ...permissions, |
32 | | - [change.effect]: change.policy, |
33 | | - tools: permissions.tools.map((tool) => |
34 | | - tool.kind === change.effect ? { ...tool, override: null } : tool |
35 | | - ), |
36 | | - }) |
37 | | - else |
38 | | - onChange({ |
39 | | - ...permissions, |
40 | | - tools: permissions.tools.map((tool) => |
41 | | - tool.id === change.toolId |
42 | | - ? { ...tool, override: override(tool.kind, change.policy) } |
43 | | - : tool |
44 | | - ), |
45 | | - }) |
| 41 | + const canAllow = permissions.canAutoApprove && account.status === 'active' |
| 42 | + const choices = ACTIONS.map((option) => ({ |
| 43 | + ...option, |
| 44 | + disabled: readOnly || (option.value === 'allow' && !canAllow), |
| 45 | + })) |
| 46 | + const filter = query.trim().toLowerCase() |
| 47 | + const matches = permissions.tools.filter((tool) => |
| 48 | + `${tool.label} ${tool.description ?? ''}`.toLowerCase().includes(filter) |
| 49 | + ) |
| 50 | + |
| 51 | + function updateTool(id: string, value: ToolPolicy, defaultValue: ToolPolicy) { |
| 52 | + onChange({ |
| 53 | + ...permissions, |
| 54 | + tools: permissions.tools.map((tool) => |
| 55 | + tool.id === id ? { ...tool, override: value === defaultValue ? null : value } : tool |
| 56 | + ), |
| 57 | + }) |
46 | 58 | } |
| 59 | + |
47 | 60 | return ( |
48 | | - <ToolPermissions |
49 | | - permissions={{ |
50 | | - read: permissions.read, |
51 | | - write: permissions.write, |
52 | | - canAutoApprove: permissions.canAutoApprove, |
53 | | - accountActive: account.status === 'active', |
54 | | - tools: permissions.tools.map((tool) => ({ |
55 | | - id: tool.id, |
56 | | - label: tool.label, |
57 | | - description: tool.description ?? '', |
58 | | - kind: tool.kind, |
59 | | - policy: tool.override ?? permissions[tool.kind], |
60 | | - inherited: tool.override === null, |
61 | | - })), |
62 | | - }} |
63 | | - query={query} |
64 | | - onQueryChange={setQuery} |
65 | | - pending={readOnly} |
66 | | - onChange={apply} |
67 | | - /> |
| 61 | + <section className={styles.section} aria-label='Tool permissions'> |
| 62 | + <div className={styles.intro}> |
| 63 | + <h2 data-type='T093'>Tool permissions</h2> |
| 64 | + {!canAllow && ( |
| 65 | + <p className={styles.detail} data-type='T060'> |
| 66 | + {account.status === 'needs_reauth' |
| 67 | + ? 'Reconnect this account to allow tools automatically.' |
| 68 | + : 'Your organization requires approval before tools can run.'} |
| 69 | + </p> |
| 70 | + )} |
| 71 | + </div> |
| 72 | + <ControlBar |
| 73 | + search={ |
| 74 | + <SearchField |
| 75 | + variant='quiet' |
| 76 | + label='Search tools' |
| 77 | + placeholder='Search tools…' |
| 78 | + onValueChange={setQuery} |
| 79 | + /> |
| 80 | + } |
| 81 | + /> |
| 82 | + {GROUPS.map(({ kind, label }) => { |
| 83 | + const matching = matches.filter((tool) => tool.kind === kind) |
| 84 | + if (!matching.length) return null |
| 85 | + const tools = permissions.tools.filter((item) => item.kind === kind) |
| 86 | + const defaultValue = permissions[kind] |
| 87 | + const defaultLabel = |
| 88 | + ACTIONS.find((action) => action.value === defaultValue)?.label ?? defaultValue |
| 89 | + return ( |
| 90 | + <section key={kind} className={styles.group} aria-label={label}> |
| 91 | + <h3 data-type='T059'> |
| 92 | + <CountedLabel label={label} count={tools.length} /> |
| 93 | + </h3> |
| 94 | + <ul className={styles.list}> |
| 95 | + {matching.map((tool) => ( |
| 96 | + <li className={styles.tool} key={tool.id}> |
| 97 | + <div className={styles.copy}> |
| 98 | + <span data-type='T059'>{tool.label}</span> |
| 99 | + {tool.description && ( |
| 100 | + <span className={styles.detail} data-type='T060'> |
| 101 | + {tool.description} |
| 102 | + </span> |
| 103 | + )} |
| 104 | + </div> |
| 105 | + <div className={styles.actions}> |
| 106 | + <SegmentedControl |
| 107 | + label={`${tool.label} permission`} |
| 108 | + description={ |
| 109 | + tool.override |
| 110 | + ? `Choose ${defaultLabel} to inherit the default for ${label.toLowerCase()}.` |
| 111 | + : `Inherits ${defaultLabel} from ${label.toLowerCase()}.` |
| 112 | + } |
| 113 | + options={choices} |
| 114 | + value={tool.override ?? defaultValue} |
| 115 | + onSelect={(change) => updateTool(tool.id, change, defaultValue)} |
| 116 | + /> |
| 117 | + </div> |
| 118 | + </li> |
| 119 | + ))} |
| 120 | + </ul> |
| 121 | + </section> |
| 122 | + ) |
| 123 | + })} |
| 124 | + {!matches.length && ( |
| 125 | + <EmptyState title={filter ? 'No matching tools' : 'No tools available'} /> |
| 126 | + )} |
| 127 | + </section> |
68 | 128 | ) |
69 | 129 | } |
0 commit comments