'use client' import { useEffect, useRef, useState } from 'react' import { usePathname, useRouter, useSearchParams } from 'next/navigation' import { useTranslation } from 'react-i18next' import Modal from '@/components/common/Modal' import { useChatWorkspaces } from '@/hooks/useChatWorkspaces' import { activeWorkspaceId, scopedUrl } from '@/lib/workspace-scope' import type { LearningOrigin } from '@/lib/learning-library' export function WorkspaceLabel({ row }: { row: LearningOrigin }) { const { t } = useTranslation() return {row.content_workspace_name || (row.content_workspace_id ? row.content_workspace_id : t('Default workspace'))} } export function useLibraryFilter(rows: T[]) { const [filter, setFilter] = useState('*') const { t } = useTranslation() const options = new Map(rows.map(row => [row.content_workspace_id ?? '', row.content_workspace_name || row.content_workspace_id || t('Default workspace')])) const control = return { rows: filter === '*' ? rows : rows.filter(row => (row.content_workspace_id ?? '') === filter), control } } /** Pick a destination only when creating; URL scopes all ensuing uploads and writes. */ export function useLearningCreation(open: () => void) { const router = useRouter() const pathname = usePathname() const query = useSearchParams() const openRef = useRef(open) useEffect(() => { openRef.current = open }, [open]) const consumed = useRef(false) useEffect(() => { if (query.get('create') !== '1') { consumed.current = false; return } if (consumed.current) return consumed.current = true openRef.current() const next = new URLSearchParams(query.toString()) next.delete('create') router.replace(`${pathname}${next.size ? `?${next}` : ''}`, { scroll: false }) }, [pathname, query, router]) const { t } = useTranslation() const { workspaces, error } = useChatWorkspaces() const [choosing, setChoosing] = useState(false) const [destination, setDestination] = useState(activeWorkspaceId) const dialog = setChoosing(false)} title={t('Save new content to')} width="sm">

{t('Only new content is saved here. Your existing materials remain visible in all workspaces.')}

{error &&

{error}

}
// Course-created content inherits the course store so its references stay valid. return { begin: () => query.has("course") ? open() : setChoosing(true), dialog } } export function requestedLearningCreation() { return typeof window !== 'undefined' && new URLSearchParams(window.location.search).get('create') === '1' }