Skip to content

Commit f05046a

Browse files
waleedlatif1claude
andcommitted
refactor: replace raw fetch with useWorkspacesQuery in knowledge header
Remove useState + useEffect + fetch anti-pattern for loading workspaces. Use useWorkspacesQuery from React Query with inline filter for write/admin permissions. Eliminates ~30 lines of manual state management, any casts, and the Pick type workaround. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent decf938 commit f05046a

1 file changed

Lines changed: 9 additions & 39 deletions

File tree

apps/sim/app/workspace/[workspaceId]/knowledge/components/knowledge-header/knowledge-header.tsx

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useEffect, useState } from 'react'
3+
import { useState } from 'react'
44
import { createLogger } from '@sim/logger'
55
import { AlertTriangle, LibraryBig, MoreHorizontal } from 'lucide-react'
66
import Link from 'next/link'
@@ -17,9 +17,7 @@ import { ChevronDown } from '@/components/emcn/icons'
1717
import { Trash } from '@/components/emcn/icons/trash'
1818
import { filterButtonClass } from '@/app/workspace/[workspaceId]/knowledge/components/constants'
1919
import { 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

2422
const logger = createLogger('KnowledgeHeader')
2523

@@ -54,43 +52,15 @@ interface KnowledgeHeaderProps {
5452
export 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

Comments
 (0)