| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- // 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<void>
- }
- export function useSidebarPages({
- workspaceId,
- }: UseSidebarPagesOptions = {}): UseSidebarPagesResult {
- const [pages, setPages] = useState<SidebarPage[]>([])
- const [isLoading, setIsLoading] = useState(false)
- const [error, setError] = useState<string | null>(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<SidebarPage[]>(
- `/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
|