'use client' import { useEffect, useState } from 'react' import Link from 'next/link' import { useTranslation } from 'react-i18next' import { getWorkspaceResources, type WorkspaceResources, type WorkspaceResourceCatalog, } from '@/lib/workspaces-api' export function WorkspaceResourcePicker({ value, onChange, workspaceId = '', }: { value: WorkspaceResources onChange: (value: WorkspaceResources) => void workspaceId?: string }) { const { t } = useTranslation() const [catalog, setCatalog] = useState(null) const [error, setError] = useState('') const [attempt, setAttempt] = useState(0) useEffect(() => { let active = true getWorkspaceResources(workspaceId) .then(result => { if (active) { setCatalog(result) setError('') } }) .catch(err => { if (active) setError(err instanceof Error ? err.message : String(err)) }) return () => { active = false } }, [workspaceId, attempt]) const groups = [ { key: 'skills' as const, label: 'Skills', href: '/space/skills' }, { key: 'mcp' as const, label: 'MCP services', href: '/space/mcp' }, { key: 'knowledge_bases' as const, label: 'Knowledge bases', href: '/knowledge-bases' }, ] return (

{t('Assigned resources')}

{t( 'Resources are managed in Learning Space. Assigning them creates references; removing an assignment keeps the original resource.' )}

{error && (

{error}{' '}

)} {groups.map(({ key, label, href }) => { const selected = value[key] const items = catalog?.[key] ?? [] const missing = (selected ?? []).filter(id => !items.some(item => item.id === id)) return (
{t(label)}
{t('Manage library')}
{selected === null ? (

{t( 'Keeps existing access rules. Newly available resources are included automatically.' )}

) : ( <> {!catalog && !error && (

{t('Loading resources…')}

)} {catalog && !items.length && (

{t('No resources available. Add resources in Learning Space first.')}

)}
{items.map(item => ( ))} {catalog && missing.map(id => ( ))}

{selected.length ? t('{{count}} resources selected', { count: selected.length }) : t('None selected. This resource type is unavailable in this workspace.')}

)}
) })}
) }