useSidebarPages.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Hook to fetch sidebar pages for a workspace
  2. import { useState, useEffect, useCallback } from 'react'
  3. import apiClient from '@/api/client'
  4. import type { PageSettings } from '@/types'
  5. interface SidebarPage {
  6. id: string
  7. workspace_id: string
  8. name: string
  9. slug: string
  10. created_by: string
  11. settings: PageSettings
  12. }
  13. interface UseSidebarPagesOptions {
  14. workspaceId?: string
  15. }
  16. interface UseSidebarPagesResult {
  17. pages: SidebarPage[]
  18. isLoading: boolean
  19. error: string | null
  20. refetch: () => Promise<void>
  21. }
  22. export function useSidebarPages({
  23. workspaceId,
  24. }: UseSidebarPagesOptions = {}): UseSidebarPagesResult {
  25. const [pages, setPages] = useState<SidebarPage[]>([])
  26. const [isLoading, setIsLoading] = useState(false)
  27. const [error, setError] = useState<string | null>(null)
  28. const fetchPages = useCallback(async () => {
  29. if (!workspaceId) {
  30. setPages([])
  31. return
  32. }
  33. setIsLoading(true)
  34. setError(null)
  35. try {
  36. // Backend already filters by:
  37. // 1. User permissions (read_all, owns, or in shared_with_groups)
  38. // 2. show_in_sidebar = true
  39. const response = await apiClient.get<SidebarPage[]>(
  40. `/workspaces/${workspaceId}/pages/sidebar`
  41. )
  42. // Pages are already sorted by menu_order from the backend
  43. setPages(response || [])
  44. } catch (err) {
  45. setError(err instanceof Error ? err.message : 'Failed to fetch sidebar pages')
  46. setPages([])
  47. } finally {
  48. setIsLoading(false)
  49. }
  50. }, [workspaceId])
  51. useEffect(() => {
  52. fetchPages()
  53. }, [fetchPages])
  54. return {
  55. pages,
  56. isLoading,
  57. error,
  58. refetch: fetchPages,
  59. }
  60. }
  61. export default useSidebarPages