'use client' import { useEffect, useRef, useState } from 'react' import { usePathname, useRouter, useSearchParams } from 'next/navigation' import { useTranslation } from 'react-i18next' import type { TFunction } from '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 workspaceName(row: LearningOrigin, t: TFunction) { return row.content_workspace_name || row.content_workspace_id || t('Default workspace') } export function WorkspaceLabel({ row }: { row: LearningOrigin }) { const { t } = useTranslation() return {workspaceName(row, t)} } export function useLibraryFilter(rows: T[]) { const [filter, setFilter] = useState('*') const { t } = useTranslation() const options = new Map(rows.map(row => [row.content_workspace_id ?? '', workspaceName(row, t)])) // A workspace that no longer has rows cannot stay selected: its option is // gone, so nothing on screen would say why the list is empty. const active = filter !== '*' && options.has(filter) ? filter : '*' const control = // The same choice sized for a page's own toolbar row. With one workspace // there is nothing to choose between, so it is not shown at all. const picker = options.size > 1 ? : null return { rows: active === '*' ? rows : rows.filter(row => (row.content_workspace_id ?? '') === active), control, picker, // More than one workspace on screen at once: rows need to say whose they are. mixed: active === '*' && options.size > 1, } } /** Opens creation when a link arrives with `?create=1`, then drops the flag from the URL. */ export function useCreateRequest(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]) } /** 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() useCreateRequest(open) 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' }