Files
loyaly_cutomerweb/src/shared/hooks/useScope.ts
2026-09-28 16:35:34 +05:30

34 lines
1.2 KiB
TypeScript

'use client';
import {useMemo} from 'react';
import {useWorkspace} from '@/shared/providers/WorkspaceProvider';
import type {Scope} from '@/shared/services/httpClient';
/**
* The {storeId, range} every scoped request is filtered by, in one hook.
*
* Feature hooks call this instead of taking scope as an argument, so a page
* never threads query parameters through its component tree — which is how the
* dashboard's six panels are guaranteed to agree about what period they are
* showing.
*
* Memoised on its two fields so the object identity only changes when the
* scope actually does. useResource keys off the serialized URL rather than
* this reference, but a stable object keeps it out of other dependency arrays.
*/
export function useScope(): Scope {
const {storeId, range, customRange} = useWorkspace();
const from = customRange?.start;
const to = customRange?.end;
return useMemo(
() => ({
storeId,
range,
// Picked dates travel only with a custom range; they are the calendar
// days the merchant chose, as plain strings, so no zone can shift them.
custom: range === 'custom' && from && to ? {from, to} : null,
}),
[storeId, range, from, to],
);
}