Consume the provided resource-envelope API snapshot. Preserve financial snapshots during refresh, observe synchronization without importing, and fence late responses across account and connection changes.
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import type { paths } from "@/shared/api";
|
|
|
|
type Meta =
|
|
paths["/api/v1/tbank-sync/{id}"]["get"]["responses"][200]["content"]["application/json"]["resources"][number];
|
|
type SectionState = {
|
|
stale?: boolean;
|
|
sourceError?: boolean;
|
|
partial?: boolean;
|
|
lastSuccessfulAt?: string | null;
|
|
hasData: boolean;
|
|
loading: boolean;
|
|
error: boolean;
|
|
refreshing: boolean;
|
|
retry: () => void;
|
|
};
|
|
function section(
|
|
query: {
|
|
data: unknown;
|
|
meta?: Meta;
|
|
isError: boolean;
|
|
isFetching: boolean;
|
|
refetch: () => unknown;
|
|
},
|
|
retry?: () => unknown,
|
|
): SectionState {
|
|
const error = query.isError || query.meta?.sync.state === "failed";
|
|
return {
|
|
stale: query.meta?.freshness === "stale",
|
|
sourceError: query.meta?.sync.state === "failed",
|
|
partial: query.meta?.availability === "partial",
|
|
lastSuccessfulAt: query.meta?.lastSuccessfulAt,
|
|
hasData: Boolean(query.data),
|
|
loading: !query.data && !error,
|
|
error,
|
|
refreshing:
|
|
Boolean(query.data) &&
|
|
(query.isFetching ||
|
|
query.meta?.sync.state === "running" ||
|
|
query.meta?.sync.state === "queued"),
|
|
retry: () => {
|
|
void (retry ?? query.refetch)();
|
|
},
|
|
};
|
|
}
|
|
|
|
export { type SectionState, section };
|