11'use client'
22
3- import { useEffect , useState } from 'react'
3+ import { useState } from 'react'
44import { createLogger } from '@sim/logger'
55import { AlertTriangle , LibraryBig , MoreHorizontal } from 'lucide-react'
66import Link from 'next/link'
@@ -17,9 +17,7 @@ import { ChevronDown } from '@/components/emcn/icons'
1717import { Trash } from '@/components/emcn/icons/trash'
1818import { filterButtonClass } from '@/app/workspace/[workspaceId]/knowledge/components/constants'
1919import { useUpdateKnowledgeBase } from '@/hooks/queries/kb/knowledge'
20- import type { Workspace } from '@/hooks/queries/workspace'
21-
22- type WorkspaceOption = Pick < Workspace , 'id' | 'name' > & { permissions : string }
20+ import { useWorkspacesQuery } from '@/hooks/queries/workspace'
2321
2422const logger = createLogger ( 'KnowledgeHeader' )
2523
@@ -54,43 +52,15 @@ interface KnowledgeHeaderProps {
5452export function KnowledgeHeader ( { breadcrumbs, options } : KnowledgeHeaderProps ) {
5553 const [ isActionsMenuOpen , setIsActionsMenuOpen ] = useState ( false )
5654 const [ isWorkspaceMenuOpen , setIsWorkspaceMenuOpen ] = useState ( false )
57- const [ workspaces , setWorkspaces ] = useState < WorkspaceOption [ ] > ( [ ] )
58- const [ isLoadingWorkspaces , setIsLoadingWorkspaces ] = useState ( false )
59-
60- const updateKnowledgeBase = useUpdateKnowledgeBase ( )
6155
62- useEffect ( ( ) => {
63- if ( ! options ?. knowledgeBaseId ) return
64-
65- const fetchWorkspaces = async ( ) => {
66- try {
67- setIsLoadingWorkspaces ( true )
68-
69- const response = await fetch ( '/api/workspaces' )
70- if ( ! response . ok ) {
71- throw new Error ( 'Failed to fetch workspaces' )
72- }
73-
74- const data = await response . json ( )
75-
76- const availableWorkspaces = data . workspaces
77- . filter ( ( ws : any ) => ws . permissions === 'write' || ws . permissions === 'admin' )
78- . map ( ( ws : any ) => ( {
79- id : ws . id ,
80- name : ws . name ,
81- permissions : ws . permissions ,
82- } ) )
83-
84- setWorkspaces ( availableWorkspaces )
85- } catch ( err ) {
86- logger . error ( 'Error fetching workspaces:' , err )
87- } finally {
88- setIsLoadingWorkspaces ( false )
89- }
90- }
56+ const { data : allWorkspaces = [ ] , isLoading : isLoadingWorkspaces } = useWorkspacesQuery (
57+ ! ! options ?. knowledgeBaseId
58+ )
59+ const workspaces = allWorkspaces . filter (
60+ ( ws ) => ws . permissions === 'write' || ws . permissions === 'admin'
61+ )
9162
92- fetchWorkspaces ( )
93- } , [ options ?. knowledgeBaseId ] )
63+ const updateKnowledgeBase = useUpdateKnowledgeBase ( )
9464
9565 const handleWorkspaceChange = async ( workspaceId : string | null ) => {
9666 if ( updateKnowledgeBase . isPending || ! options ?. knowledgeBaseId ) return
0 commit comments