// Hook to fetch sidebar pages for a workspace import { useState, useEffect, useCallback } from 'react' import apiClient from '@/api/client' import type { PageSettings } from '@/types' interface SidebarPage { id: string workspace_id: string name: string slug: string created_by: string settings: PageSettings } interface UseSidebarPagesOptions { workspaceId?: string } interface UseSidebarPagesResult { pages: SidebarPage[] isLoading: boolean error: string | null refetch: () => Promise } export function useSidebarPages({ workspaceId, }: UseSidebarPagesOptions = {}): UseSidebarPagesResult { const [pages, setPages] = useState([]) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const fetchPages = useCallback(async () => { if (!workspaceId) { setPages([]) return } setIsLoading(true) setError(null) try { // Backend already filters by: // 1. User permissions (read_all, owns, or in shared_with_groups) // 2. show_in_sidebar = true const response = await apiClient.get( `/workspaces/${workspaceId}/pages/sidebar` ) // Pages are already sorted by menu_order from the backend setPages(response || []) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch sidebar pages') setPages([]) } finally { setIsLoading(false) } }, [workspaceId]) useEffect(() => { fetchPages() }, [fetchPages]) return { pages, isLoading, error, refetch: fetchPages, } } export default useSidebarPages