mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 22:34:56 +00:00
- codegen: Low-code React app with JSON-driven component system - packagerepo: Schema-driven package repository with backend/frontend - postgres: Next.js app with Drizzle ORM and PostgreSQL Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
39 lines
955 B
TypeScript
39 lines
955 B
TypeScript
/**
|
|
* Hook for fetching and managing table data
|
|
*/
|
|
import { useState, useCallback, useEffect } from 'react';
|
|
import { useApiCall } from './useApiCall';
|
|
|
|
export function useTableData(tableName?: string) {
|
|
const { loading, error, execute } = useApiCall();
|
|
const [queryResult, setQueryResult] = useState<any>(null);
|
|
|
|
const fetchTableData = useCallback(async (table: string) => {
|
|
try {
|
|
const result = await execute('/api/admin/table-data', {
|
|
method: 'POST',
|
|
body: { tableName: table },
|
|
});
|
|
setQueryResult(result);
|
|
return result;
|
|
} catch (err) {
|
|
console.error('Failed to fetch table data:', err);
|
|
throw err;
|
|
}
|
|
}, [execute]);
|
|
|
|
useEffect(() => {
|
|
if (tableName) {
|
|
fetchTableData(tableName);
|
|
}
|
|
}, [tableName, fetchTableData]);
|
|
|
|
return {
|
|
data: queryResult,
|
|
loading,
|
|
error,
|
|
refresh: () => tableName && fetchTableData(tableName),
|
|
fetchTableData,
|
|
};
|
|
}
|