|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { |
| 5 | + Chip, |
| 6 | + ChipInput, |
| 7 | + ChipModal, |
| 8 | + ChipModalBody, |
| 9 | + ChipModalError, |
| 10 | + ChipModalField, |
| 11 | + ChipModalFooter, |
| 12 | + ChipModalHeader, |
| 13 | + ChipTag, |
| 14 | + toast, |
| 15 | +} from '@sim/emcn' |
| 16 | +import { Database, Plus } from '@sim/emcn/icons' |
| 17 | +import { SettingsPanel } from '@/components/settings/settings-panel' |
| 18 | +import { SettingsEmptyState } from '@/app/workspace/[workspaceId]/settings/components/settings-empty-state' |
| 19 | +import { |
| 20 | + RESOURCE_LIST_STACK, |
| 21 | + SettingsResourceRow, |
| 22 | +} from '@/app/workspace/[workspaceId]/settings/components/settings-resource-row' |
| 23 | +import { |
| 24 | + useCreateMemorySpace, |
| 25 | + useMemorySpaces, |
| 26 | + useSelectMemorySpace, |
| 27 | +} from '@/hooks/queries/memory-spaces' |
| 28 | + |
| 29 | +interface KnowledgeGraphsProps { |
| 30 | + organizationId: string |
| 31 | +} |
| 32 | + |
| 33 | +export function KnowledgeGraphs({ organizationId }: KnowledgeGraphsProps) { |
| 34 | + const { data, isLoading, error } = useMemorySpaces(organizationId) |
| 35 | + const select = useSelectMemorySpace(organizationId) |
| 36 | + const [creating, setCreating] = useState(false) |
| 37 | + return ( |
| 38 | + <SettingsPanel |
| 39 | + actions={[ |
| 40 | + { |
| 41 | + text: 'New graph', |
| 42 | + icon: Plus, |
| 43 | + onSelect: () => setCreating(true), |
| 44 | + disabled: isLoading || !!error || select.isPending, |
| 45 | + }, |
| 46 | + ]} |
| 47 | + > |
| 48 | + {error ? ( |
| 49 | + <SettingsEmptyState tone='error'>{error.message}</SettingsEmptyState> |
| 50 | + ) : isLoading ? ( |
| 51 | + <SettingsEmptyState>Loading knowledge graphs...</SettingsEmptyState> |
| 52 | + ) : ( |
| 53 | + <div className={RESOURCE_LIST_STACK}> |
| 54 | + {data?.spaces.map((space) => ( |
| 55 | + <SettingsResourceRow |
| 56 | + key={space.id ?? 'default'} |
| 57 | + icon={<Database />} |
| 58 | + title={space.name} |
| 59 | + description={ |
| 60 | + space.id === null |
| 61 | + ? 'Your original knowledge graph, preserved.' |
| 62 | + : 'Private to you in this organization.' |
| 63 | + } |
| 64 | + trailing={ |
| 65 | + space.id === data.activeSpaceId ? ( |
| 66 | + <ChipTag>Active</ChipTag> |
| 67 | + ) : ( |
| 68 | + <Chip |
| 69 | + disabled={select.isPending || creating} |
| 70 | + onClick={() => |
| 71 | + select.mutate( |
| 72 | + { spaceId: space.id }, |
| 73 | + { onError: (failure) => toast.error(failure.message) } |
| 74 | + ) |
| 75 | + } |
| 76 | + > |
| 77 | + Make active |
| 78 | + </Chip> |
| 79 | + ) |
| 80 | + } |
| 81 | + /> |
| 82 | + ))} |
| 83 | + </div> |
| 84 | + )} |
| 85 | + {creating && ( |
| 86 | + <CreateKnowledgeGraph organizationId={organizationId} onClose={() => setCreating(false)} /> |
| 87 | + )} |
| 88 | + </SettingsPanel> |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +interface CreateKnowledgeGraphProps extends KnowledgeGraphsProps { |
| 93 | + onClose: () => void |
| 94 | +} |
| 95 | +function CreateKnowledgeGraph({ organizationId, onClose }: CreateKnowledgeGraphProps) { |
| 96 | + const create = useCreateMemorySpace(organizationId) |
| 97 | + const [name, setName] = useState('') |
| 98 | + const submit = () => create.mutate({ name: name.trim() }, { onSuccess: onClose }) |
| 99 | + return ( |
| 100 | + <ChipModal |
| 101 | + open |
| 102 | + onOpenChange={(open) => !open && onClose()} |
| 103 | + dismissDisabled={create.isPending} |
| 104 | + srTitle='New knowledge graph' |
| 105 | + > |
| 106 | + <ChipModalHeader onClose={onClose}>New knowledge graph</ChipModalHeader> |
| 107 | + <ChipModalBody> |
| 108 | + <ChipModalField |
| 109 | + type='custom' |
| 110 | + title='Name' |
| 111 | + hint='Creates an empty graph and makes it active for new chats. Your other graphs are kept.' |
| 112 | + > |
| 113 | + <ChipInput |
| 114 | + aria-label='Graph name' |
| 115 | + placeholder='e.g. Fresh exploration' |
| 116 | + value={name} |
| 117 | + maxLength={100} |
| 118 | + disabled={create.isPending} |
| 119 | + onChange={(event) => setName(event.target.value)} |
| 120 | + /> |
| 121 | + </ChipModalField> |
| 122 | + <ChipModalError>{create.error?.message}</ChipModalError> |
| 123 | + </ChipModalBody> |
| 124 | + <ChipModalFooter |
| 125 | + onCancel={onClose} |
| 126 | + primaryAction={{ |
| 127 | + label: create.isPending ? 'Creating...' : 'Create and activate', |
| 128 | + onClick: submit, |
| 129 | + disabled: create.isPending || !name.trim(), |
| 130 | + }} |
| 131 | + /> |
| 132 | + </ChipModal> |
| 133 | + ) |
| 134 | +} |
0 commit comments