1- import { useEffect , useMemo , useState } from 'react'
1+ import { useMemo , useState } from 'react'
22import { Check , ChevronDown } from 'lucide-react'
33import { useParams } from 'next/navigation'
44import { Button } from '@/components/emcn'
@@ -22,7 +22,8 @@ import {
2222 filterButtonClass ,
2323 folderDropdownListStyle ,
2424} from '@/app/workspace/[workspaceId]/logs/components/filters/components/shared'
25- import { useFolderStore } from '@/stores/folders/store'
25+ import { useFolders } from '@/hooks/queries/folders'
26+ import { type FolderTreeNode , useFolderStore } from '@/stores/folders/store'
2627import { useFilterStore } from '@/stores/logs/filters/store'
2728
2829const logger = createLogger ( 'LogsFolderFilter' )
@@ -36,56 +37,37 @@ interface FolderOption {
3637
3738export default function FolderFilter ( ) {
3839 const { folderIds, toggleFolderId, setFolderIds } = useFilterStore ( )
39- const { getFolderTree, fetchFolders } = useFolderStore ( )
40+ const { getFolderTree } = useFolderStore ( )
4041 const params = useParams ( )
4142 const workspaceId = params . workspaceId as string
42- const [ folders , setFolders ] = useState < FolderOption [ ] > ( [ ] )
43- const [ loading , setLoading ] = useState ( true )
4443 const [ search , setSearch ] = useState ( '' )
44+ const { isLoading : foldersLoading } = useFolders ( workspaceId )
4545
46- // Fetch all available folders from the API
47- useEffect ( ( ) => {
48- const fetchFoldersData = async ( ) => {
49- try {
50- setLoading ( true )
51- if ( workspaceId ) {
52- await fetchFolders ( workspaceId )
53- const folderTree = getFolderTree ( workspaceId )
46+ const folderTree = workspaceId ? getFolderTree ( workspaceId ) : [ ]
5447
55- // Flatten the folder tree and create options with full paths
56- const flattenFolders = ( nodes : any [ ] , parentPath = '' ) : FolderOption [ ] => {
57- const result : FolderOption [ ] = [ ]
48+ const folders : FolderOption [ ] = useMemo ( ( ) => {
49+ const flattenFolders = ( nodes : FolderTreeNode [ ] , parentPath = '' ) : FolderOption [ ] => {
50+ const result : FolderOption [ ] = [ ]
5851
59- for ( const node of nodes ) {
60- const currentPath = parentPath ? `${ parentPath } / ${ node . name } ` : node . name
61- result . push ( {
62- id : node . id ,
63- name : node . name ,
64- color : node . color || '#6B7280' ,
65- path : currentPath ,
66- } )
52+ for ( const node of nodes ) {
53+ const currentPath = parentPath ? `${ parentPath } / ${ node . name } ` : node . name
54+ result . push ( {
55+ id : node . id ,
56+ name : node . name ,
57+ color : node . color || '#6B7280' ,
58+ path : currentPath ,
59+ } )
6760
68- // Add children recursively
69- if ( node . children && node . children . length > 0 ) {
70- result . push ( ...flattenFolders ( node . children , currentPath ) )
71- }
72- }
73-
74- return result
75- }
76-
77- const folderOptions = flattenFolders ( folderTree )
78- setFolders ( folderOptions )
61+ if ( node . children && node . children . length > 0 ) {
62+ result . push ( ...flattenFolders ( node . children , currentPath ) )
7963 }
80- } catch ( error ) {
81- logger . error ( 'Failed to fetch folders' , { error } )
82- } finally {
83- setLoading ( false )
8464 }
65+
66+ return result
8567 }
8668
87- fetchFoldersData ( )
88- } , [ workspaceId , fetchFolders , getFolderTree ] )
69+ return flattenFolders ( folderTree )
70+ } , [ folderTree ] )
8971
9072 // Get display text for the dropdown button
9173 const getSelectedFoldersText = ( ) => {
@@ -111,7 +93,7 @@ export default function FolderFilter() {
11193 < DropdownMenu >
11294 < DropdownMenuTrigger asChild >
11395 < Button variant = 'outline' className = { filterButtonClass } >
114- { loading ? 'Loading folders...' : getSelectedFoldersText ( ) }
96+ { foldersLoading ? 'Loading folders...' : getSelectedFoldersText ( ) }
11597 < ChevronDown className = 'ml-2 h-4 w-4 text-muted-foreground' />
11698 </ Button >
11799 </ DropdownMenuTrigger >
@@ -125,7 +107,9 @@ export default function FolderFilter() {
125107 < Command >
126108 < CommandInput placeholder = 'Search folders...' onValueChange = { ( v ) => setSearch ( v ) } />
127109 < CommandList className = { commandListClass } style = { folderDropdownListStyle } >
128- < CommandEmpty > { loading ? 'Loading folders...' : 'No folders found.' } </ CommandEmpty >
110+ < CommandEmpty >
111+ { foldersLoading ? 'Loading folders...' : 'No folders found.' }
112+ </ CommandEmpty >
129113 < CommandGroup >
130114 < CommandItem
131115 value = 'all-folders'
0 commit comments