2026-07-11 00:07:48 +03:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
|
import { getLocalDayIsoRange } from '@/components/appointments/appointmentTime';
|
|
|
|
|
import { todayApi } from '@/lib/api/today';
|
|
|
|
|
import type { TodaySummaryData } from '@/types/today';
|
|
|
|
|
import type { ApiError } from '@/types/api';
|
|
|
|
|
|
|
|
|
|
interface UseTodaySummaryResult {
|
|
|
|
|
data: TodaySummaryData | null;
|
|
|
|
|
loading: boolean;
|
2026-07-11 00:53:40 +03:30
|
|
|
isInitialLoad: boolean;
|
2026-07-11 00:07:48 +03:30
|
|
|
error: ApiError | null;
|
|
|
|
|
reload: () => Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-11 00:53:40 +03:30
|
|
|
export function useTodaySummary(organizationId?: string | null): UseTodaySummaryResult {
|
|
|
|
|
const enabled = Boolean(organizationId);
|
2026-07-11 00:07:48 +03:30
|
|
|
const [data, setData] = useState<TodaySummaryData | null>(null);
|
|
|
|
|
const [loading, setLoading] = useState(enabled);
|
|
|
|
|
const [error, setError] = useState<ApiError | null>(null);
|
|
|
|
|
|
|
|
|
|
const reload = useCallback(async () => {
|
2026-07-11 00:53:40 +03:30
|
|
|
if (!organizationId) {
|
2026-07-11 00:07:48 +03:30
|
|
|
setData(null);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
setError(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const range = getLocalDayIsoRange(new Date());
|
|
|
|
|
const response = await todayApi.summary(range);
|
|
|
|
|
setData(response.data);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err as ApiError);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
2026-07-11 00:53:40 +03:30
|
|
|
}, [organizationId]);
|
2026-07-11 00:07:48 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-07-11 00:53:40 +03:30
|
|
|
setData(null);
|
|
|
|
|
setError(null);
|
|
|
|
|
if (!organizationId) {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setLoading(true);
|
2026-07-11 00:07:48 +03:30
|
|
|
void reload();
|
2026-07-11 00:53:40 +03:30
|
|
|
}, [organizationId, reload]);
|
2026-07-11 00:07:48 +03:30
|
|
|
|
2026-07-11 00:53:40 +03:30
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
loading,
|
|
|
|
|
isInitialLoad: loading && !data,
|
|
|
|
|
error,
|
|
|
|
|
reload,
|
|
|
|
|
};
|
2026-07-11 00:07:48 +03:30
|
|
|
}
|