useBlock.ts
1 import useSWR from 'swr'; 2 import { fetchBlockById, type MemoryBlock } from '@/utils/blocks'; 3 4 /** 5 * Hook for fetching a single MemoryBlock by ID 6 * @param id - The ID of the block to fetch 7 * @param branch - Optional branch name to fetch block from (defaults to 'main') 8 * @param namespace - Optional namespace to filter blocks (defaults to 'legacy') 9 */ 10 export function useBlock(id: string | null, branch?: string, namespace?: string) { 11 const key = id ? ['block', id, branch, namespace] : null; 12 const { data, error, isLoading, mutate } = useSWR( 13 key, 14 () => id ? fetchBlockById(id, branch, namespace) : null 15 ); 16 17 return { 18 block: data as MemoryBlock | undefined, 19 isLoading, 20 isError: error, 21 mutate 22 }; 23 }