krishnasales_mobile/src/components/dv/useDetailViewData.ts
2026-07-14 19:16:44 +05:30

36 lines
1.1 KiB
TypeScript

import { useState, useEffect } from 'react';
import type { ZinoClient } from '../../api/client';
export function useDetailViewData(client: ZinoClient, dvUid: string | undefined, instanceId: string | number) {
const [data, setData] = useState<Record<string, any> | null>(null);
const [config, setConfig] = useState<any>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
let live = true;
async function run() {
setLoading(true);
setError(null);
try {
if (!dvUid) throw new Error("dvUid is required to fetch detail view data.");
const r = await client.detailView(dvUid, instanceId);
if (live) {
setData(r.data || {});
setConfig(r.config || {});
}
} catch (e) {
if (live) setError((e as { message?: string })?.message ?? 'Failed to load');
} finally {
if (live) setLoading(false);
}
}
run();
return () => {
live = false;
};
}, [client, dvUid, instanceId]);
return { data, config, loading, error };
}