mono/packages/ui/src/modules/layout/useLayouts.ts
2026-02-17 13:31:46 +01:00

41 lines
1.7 KiB
TypeScript

import { Database } from '@/integrations/supabase/types';
import { getLayouts, getLayout, createLayout, updateLayout, deleteLayout } from './client-layouts';
type Layout = Database['public']['Tables']['layouts']['Row'];
type LayoutInsert = Database['public']['Tables']['layouts']['Insert'];
type LayoutUpdate = Database['public']['Tables']['layouts']['Update'];
type LayoutVisibility = Database['public']['Enums']['layout_visibility'];
export interface UseLayoutsReturn {
getLayouts: (filters?: {
visibility?: LayoutVisibility;
type?: string;
limit?: number;
offset?: number;
}) => Promise<{ data: Layout[] | null; error: any }>;
getLayout: (id: string) => Promise<{ data: Layout | null; error: any }>;
createLayout: (layout: Omit<LayoutInsert, 'owner_id'>) => Promise<{ data: Layout | null; error: any }>;
updateLayout: (id: string, updates: LayoutUpdate) => Promise<{ data: Layout | null; error: any }>;
deleteLayout: (id: string) => Promise<{ error: any }>;
}
const safe = async <T>(fn: () => Promise<T>) => {
try {
const res: any = await fn();
return res?.data ? res : { data: res, error: null };
} catch (error) {
return { data: null, error };
}
};
export const useLayouts = (): UseLayoutsReturn => ({
getLayouts: (filters) => safe(() => getLayouts(filters as any)),
getLayout: (id) => safe(() => getLayout(id)),
createLayout: (layout) => safe(() => createLayout(layout)),
updateLayout: (id, updates) => safe(() => updateLayout(id, updates)),
deleteLayout: async (id) => {
try { await deleteLayout(id); return { error: null }; }
catch (error) { return { error }; }
}
});