@vritti/quantum-ui 0.6.11 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Select.js.map +1 -1
- package/dist/SelectFilter.js.map +1 -1
- package/dist/SingleSelect.js.map +1 -1
- package/dist/components/Select.js.map +1 -1
- package/dist/lib/components/ContextBar/index.d.ts +1 -1
- package/dist/lib/components/ContextBar/index.d.ts.map +1 -1
- package/dist/lib/components/ContextBar/useContextState.d.ts +10 -6
- package/dist/lib/components/ContextBar/useContextState.d.ts.map +1 -1
- package/dist/lib/components/Select/Select.d.ts +15 -15
- package/dist/lib/components/Select/Select.d.ts.map +1 -1
- package/dist/lib/components/Select/SelectFilter.d.ts +8 -7
- package/dist/lib/components/Select/SelectFilter.d.ts.map +1 -1
- package/dist/lib/components/Select/components/MultiSelect/MultiSelect.d.ts +9 -7
- package/dist/lib/components/Select/components/MultiSelect/MultiSelect.d.ts.map +1 -1
- package/dist/lib/components/Select/components/MultiSelect/MultiSelectFilter.d.ts +8 -6
- package/dist/lib/components/Select/components/MultiSelect/MultiSelectFilter.d.ts.map +1 -1
- package/dist/lib/components/Select/components/SingleSelect/SingleSelect.d.ts +18 -16
- package/dist/lib/components/Select/components/SingleSelect/SingleSelect.d.ts.map +1 -1
- package/dist/lib/components/Select/components/SingleSelect/SingleSelectFilter.d.ts +8 -6
- package/dist/lib/components/Select/components/SingleSelect/SingleSelectFilter.d.ts.map +1 -1
- package/dist/lib/components/Select/hooks/useMultiSelect.d.ts +9 -9
- package/dist/lib/components/Select/hooks/useMultiSelect.d.ts.map +1 -1
- package/dist/lib/components/Select/hooks/useSelect.d.ts +5 -5
- package/dist/lib/components/Select/hooks/useSelect.d.ts.map +1 -1
- package/dist/lib/components/Select/hooks/useSingleSelect.d.ts +9 -9
- package/dist/lib/components/Select/hooks/useSingleSelect.d.ts.map +1 -1
- package/dist/lib/components/Select/index.d.ts +1 -1
- package/dist/lib/components/Select/index.d.ts.map +1 -1
- package/dist/lib/components/Select/types.d.ts +23 -6
- package/dist/lib/components/Select/types.d.ts.map +1 -1
- package/dist/useContextState.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useContextState.js","sources":["../lib/services/context-states.service.ts","../lib/components/ContextBar/useContextSave.ts","../lib/components/ContextBar/useSettlingFields.ts","../lib/components/ContextBar/useContextState.ts"],"sourcesContent":["import { getConfig } from '../config';\nimport type { FilterCondition } from '../types/conditions';\nimport { axios } from '../utils/axios';\n\nconst silent = { showSuccessToast: false, showErrorToast: false } as const;\n\n// Fetches a view's saved context; none when nothing is saved\nexport function getContextState(slug: string): Promise<FilterCondition[]> {\n return axios\n .get<{ context: FilterCondition[] }>(getConfig().views.contextStatesEndpoint, { params: { slug }, ...silent })\n .then((r) => r.data.context);\n}\n\n// Replaces a view's saved context — what its context bar holds\nexport function putContextState(slug: string, context: FilterCondition[]): Promise<void> {\n return axios.put(getConfig().views.contextStatesEndpoint, { slug, context }, silent).then(() => undefined);\n}\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport { putContextState } from '../../services/context-states.service';\nimport type { FilterCondition } from '../../types/conditions';\n\nconst SAVE_DELAY_MS = 150;\n\nexport interface ContextSave {\n slug: string;\n context: FilterCondition[];\n}\n\ninterface NumberedSave extends ContextSave {\n // Counts up with every save, so a save can tell whether a newer one has replaced it\n number: number;\n}\n\ninterface UseContextSaveOptions {\n // Called when the latest save has reached the server\n onSaved: (save: ContextSave) => unknown;\n // Called when the latest save could not be stored\n onFailed: () => unknown;\n}\n\n// Saves a view's context to the server, 150 ms after the last change:\n// - saves reach the server in the order they were made, each one after the previous one finished\n// - only the latest save reports back, so an older one finishing late changes nothing\n// - a change still waiting when the view goes away (or switches to another slug) is saved straight away\nexport function useContextSave({ onSaved, onFailed }: UseContextSaveOptions) {\n const [isSaving, setIsSaving] = useState(false);\n const latestNumber = useRef(0);\n const waiting = useRef<NumberedSave | null>(null);\n const timer = useRef<ReturnType<typeof setTimeout> | null>(null);\n const previousRequest = useRef<Promise<boolean>>(Promise.resolve(true));\n const callbacks = useRef({ onSaved, onFailed });\n callbacks.current = { onSaved, onFailed };\n\n // Sends one save once the previous one has finished. Resolves whether it reached the server; it never rejects, so one\n // failed save doesn't stop the ones after it.\n const send = useCallback((save: ContextSave): Promise<boolean> => {\n const request = previousRequest.current.then(() =>\n putContextState(save.slug, save.context).then(\n () => true,\n () => false,\n ),\n );\n previousRequest.current = request;\n return request;\n }, []);\n\n // Sends the waiting save, then reports back unless a newer save has replaced it\n const sendWaiting = useCallback(async () => {\n const save = waiting.current;\n if (!save) return;\n waiting.current = null;\n\n const reachedServer = await send(save);\n const isLatest = save.number === latestNumber.current;\n if (!isLatest) return;\n\n if (reachedServer) await callbacks.current.onSaved(save);\n else await callbacks.current.onFailed();\n setIsSaving(false);\n }, [send]);\n\n const save = useCallback(\n (slug: string, context: FilterCondition[]) => {\n const previous = waiting.current;\n if (previous && previous.slug !== slug) void send(previous);\n\n latestNumber.current += 1;\n waiting.current = { slug, context, number: latestNumber.current };\n setIsSaving(true);\n\n if (timer.current) clearTimeout(timer.current);\n timer.current = setTimeout(sendWaiting, SAVE_DELAY_MS);\n },\n [send, sendWaiting],\n );\n\n // Leaving the view saves a change that is still waiting\n useEffect(\n () => () => {\n if (timer.current) clearTimeout(timer.current);\n if (waiting.current) void send(waiting.current);\n },\n [send],\n );\n\n return { save, isSaving };\n}\n","import { useCallback, useState } from 'react';\n\nconst NO_FIELDS: ReadonlySet<string> = new Set();\n\n// The context fields whose control is still settling its value — picking its first option, or checking that its held\n// value still exists\nexport function useSettlingFields() {\n const [fields, setFields] = useState(NO_FIELDS);\n\n const markFieldSettling = useCallback((field: string, settling: boolean) => {\n setFields((current) => {\n if (current.has(field) === settling) return current;\n const next = new Set(current);\n if (settling) next.add(field);\n else next.delete(field);\n return next;\n });\n }, []);\n\n return { hasSettlingFields: fields.size > 0, markFieldSettling };\n}\n","import { useQuery, useQueryClient } from '@tanstack/react-query';\nimport { deepEqual } from 'fast-equals';\nimport { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';\nimport { getContextState } from '../../services/context-states.service';\nimport type { FilterCondition } from '../../types/conditions';\nimport { readValue, readValues, withChange, withDefaults } from '../../utils/conditions';\nimport type { SelectOption } from '../Select/types';\nimport { toast } from '../Sonner/toast';\nimport { type ContextSave, useContextSave } from './useContextSave';\nimport { useSettlingFields } from './useSettlingFields';\n\ninterface UseContextStateOptions {\n /** Where the context is saved; without one the context lives only in memory */\n slug?: string;\n /** Values the context always has — filled whenever a field is missing */\n defaultContext?: FilterCondition[];\n /** Values an in-memory context (no slug) starts with, which can still be changed or cleared */\n initialContext?: FilterCondition[];\n /** Fields whose default replaces the saved value every time the view opens */\n resetOnOpen?: string[];\n /** Fields cleared when another field changes, e.g. `{ site: ['supplierIds'] }` */\n resets?: Record<string, string[]>;\n /** Called after a change is saved — the view refetches its data here */\n onStatePush?: () => unknown;\n /** Called with each field a change sets or clears, as the change is shown */\n onChange?: (field: string, condition: FilterCondition | null) => void;\n}\n\nexport interface ContextStateInstance {\n context: FilterCondition[];\n contextValue: (field: string) => string | undefined;\n contextValues: (field: string) => string[];\n setContext: (field: string, condition: FilterCondition | null) => void;\n required: ReadonlySet<string>;\n isLoaded: boolean;\n isSaving: boolean;\n /** The context isn't final yet: it is loading, a change is saving, or a control is still settling its value (picking\n * a first option, checking a held one). A view holds its data request until it settles. */\n isSettling: boolean;\n markFieldSettling: (field: string, settling: boolean) => void;\n /** The options a Select given this context holds, in the order of its value — picked, looked up, or looked up again\n * under new filters; in memory only, never saved */\n selectedOptions: SelectOption[];\n /** The first of them, for a single select */\n selectedOption: SelectOption | null;\n setSelectedOptions: (options: SelectOption[]) => void;\n}\n\nconst NO_CONDITIONS: FilterCondition[] = [];\nconst NO_OPTIONS: SelectOption[] = [];\nconst NO_FIELDS: string[] = [];\nconst NO_RESETS: Record<string, string[]> = {};\n\nfunction contextQueryKey(slug: string | undefined) {\n return ['context-state', slug ?? ''] as const;\n}\n\n// A view's context bar: the saved conditions for this visit, with defaults filled in. Every change is shown at once\n// and saved in the background.\nexport function useContextState({\n slug,\n defaultContext,\n initialContext,\n resetOnOpen = NO_FIELDS,\n resets = NO_RESETS,\n onStatePush,\n onChange,\n}: UseContextStateOptions): ContextStateInstance {\n const queryClient = useQueryClient();\n const queryKey = useMemo(() => contextQueryKey(slug), [slug]);\n\n // Read when the view opens (and after a failed save) — never in the background, where a read landing mid-save would\n // bring the old context back\n const {\n data: stored,\n isError: readFailed,\n refetch,\n } = useQuery({\n queryKey,\n queryFn: () => getContextState(slug ?? ''),\n enabled: slug != null,\n staleTime: Number.POSITIVE_INFINITY,\n refetchOnMount: 'always',\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n });\n\n // The defaults, kept stable while their contents don't change\n const nextDefaults = defaultContext ?? NO_CONDITIONS;\n const [defaults, setDefaults] = useState(nextDefaults);\n if (!deepEqual(defaults, nextDefaults)) setDefaults(nextDefaults);\n\n // The context on screen, held per slug so switching views never shows another view's context\n const [held, setHeld] = useState<{ slug: string | undefined; context: FilterCondition[] } | null>(null);\n const hasHeld = held != null && held.slug === slug;\n const context = hasHeld ? held.context : NO_CONDITIONS;\n\n const onStatePushRef = useRef(onStatePush);\n onStatePushRef.current = onStatePush;\n const onChangeRef = useRef(onChange);\n onChangeRef.current = onChange;\n // Read once, as the context starts\n const initialRef = useRef(initialContext ?? NO_CONDITIONS);\n\n const { save, isSaving } = useContextSave({\n onSaved: ({ slug: savedSlug, context: savedContext }: ContextSave) => {\n queryClient.setQueryData(contextQueryKey(savedSlug), savedContext);\n return onStatePushRef.current?.();\n },\n onFailed: () => {\n toast.error(\"Couldn't save the change\", { description: 'The view keeps what was saved before.' });\n return refetch();\n },\n });\n\n // Saves a context: it becomes the cached read at once, so a read already on its way can't bring the old one back\n const persist = useCallback(\n (next: FilterCondition[]) => {\n if (slug == null) return;\n void queryClient.cancelQueries({ queryKey });\n queryClient.setQueryData(queryKey, next);\n save(slug, next);\n },\n [slug, queryClient, queryKey, save],\n );\n\n // Shows a context and saves it\n const showAndPersist = useCallback(\n (next: FilterCondition[]) => {\n setHeld({ slug, context: next });\n persist(next);\n },\n [slug, persist],\n );\n\n // 1. Nothing to read (no slug, or the read failed): start from the initial values and the defaults, so the view still\n // answers\n useLayoutEffect(() => {\n const canRead = slug != null && !readFailed;\n if (hasHeld || canRead) return;\n setHeld({ slug, context: withDefaults(initialRef.current, defaults, { isOpening: true, resetOnOpen }) });\n }, [hasHeld, slug, readFailed, defaults, resetOnOpen]);\n\n // 2. The saved context arrives: show it with its defaults filled, and save it if filling changed it\n useLayoutEffect(() => {\n if (hasHeld || !stored) return;\n const next = withDefaults(stored, defaults, { isOpening: true, resetOnOpen });\n setHeld({ slug, context: next });\n if (!deepEqual(next, stored)) persist(next);\n }, [hasHeld, stored, defaults, resetOnOpen, slug, persist]);\n\n // 3. The saved context was read again (after a failed save): show it, unless a change of ours is still saving\n useLayoutEffect(() => {\n if (!hasHeld || isSaving || !stored) return;\n const next = withDefaults(stored, defaults);\n if (deepEqual(next, held.context)) return;\n setHeld({ slug, context: next });\n if (!deepEqual(next, stored)) persist(next);\n }, [hasHeld, isSaving, stored, held, defaults, slug, persist]);\n\n // 4. A default that becomes known after the context loaded is filled in\n useLayoutEffect(() => {\n if (!hasHeld) return;\n const next = withDefaults(held.context, defaults);\n if (next !== held.context) showAndPersist(next);\n }, [hasHeld, held, defaults, showAndPersist]);\n\n const setContext = useCallback(\n (field: string, condition: FilterCondition | null) => {\n if (!hasHeld) return;\n const current = held.context.find((existing) => existing.field === field) ?? null;\n if (deepEqual(current, condition)) return;\n const cleared = resets[field] ?? NO_FIELDS;\n const next = withDefaults(withChange(held.context, field, condition, cleared), defaults);\n showAndPersist(next);\n onChangeRef.current?.(field, condition);\n // The fields the change cleared are reported too, as they now stand: gone, or back on their default\n for (const reset of cleared) {\n const before = held.context.find((existing) => existing.field === reset) ?? null;\n const after = next.find((existing) => existing.field === reset) ?? null;\n if (!deepEqual(before, after)) onChangeRef.current?.(reset, after);\n }\n },\n [hasHeld, held, resets, defaults, showAndPersist],\n );\n\n const contextValue = useCallback((field: string) => readValue(context, field), [context]);\n const contextValues = useCallback((field: string) => readValues(context, field), [context]);\n const required = useMemo(() => new Set(defaults.map((condition) => condition.field)), [defaults]);\n const { hasSettlingFields, markFieldSettling } = useSettlingFields();\n const [selectedOptions, setHeldOptions] = useState<SelectOption[]>(NO_OPTIONS);\n // The same options again leave the held ones in place, so the page does not render for nothing\n const setSelectedOptions = useCallback(\n (options: SelectOption[]) => setHeldOptions((current) => (deepEqual(current, options) ? current : options)),\n [],\n );\n\n const isLoaded = hasHeld;\n const isSettling = !isLoaded || isSaving || hasSettlingFields;\n\n return useMemo(\n () => ({\n context,\n contextValue,\n contextValues,\n setContext,\n required,\n isLoaded,\n isSaving,\n isSettling,\n markFieldSettling,\n selectedOptions,\n selectedOption: selectedOptions[0] ?? null,\n setSelectedOptions,\n }),\n [\n context,\n contextValue,\n contextValues,\n setContext,\n required,\n isLoaded,\n isSaving,\n isSettling,\n markFieldSettling,\n selectedOptions,\n setSelectedOptions,\n ],\n );\n}\n"],"names":["save","NO_FIELDS"],"mappings":";;;;;;;AAIA,MAAM,MAAA,GAAS,EAAE,gBAAA,EAAkB,KAAA,EAAO,gBAAgB,KAAA,EAAM;AAGzD,SAAS,gBAAgB,IAAA,EAA0C;AACxE,EAAA,OAAO,KAAA,CACJ,IAAoC,SAAA,EAAU,CAAE,MAAM,qBAAA,EAAuB,EAAE,QAAQ,EAAE,IAAA,IAAQ,GAAG,MAAA,EAAQ,CAAA,CAC5G,IAAA,CAAK,CAAC,CAAA,KAAM,CAAA,CAAE,KAAK,OAAO,CAAA;AAC/B;AAGO,SAAS,eAAA,CAAgB,MAAc,OAAA,EAA2C;AACvF,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,SAAA,EAAU,CAAE,MAAM,qBAAA,EAAuB,EAAE,IAAA,EAAM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ources":["../lib/services/context-states.service.ts","../lib/components/ContextBar/useContextSave.ts","../lib/components/ContextBar/useSettlingFields.ts","../lib/components/ContextBar/useContextState.ts"],"sourcesContent":["import { getConfig } from '../config';\nimport type { FilterCondition } from '../types/conditions';\nimport { axios } from '../utils/axios';\n\nconst silent = { showSuccessToast: false, showErrorToast: false } as const;\n\n// Fetches a view's saved context; none when nothing is saved\nexport function getContextState(slug: string): Promise<FilterCondition[]> {\n return axios\n .get<{ context: FilterCondition[] }>(getConfig().views.contextStatesEndpoint, { params: { slug }, ...silent })\n .then((r) => r.data.context);\n}\n\n// Replaces a view's saved context — what its context bar holds\nexport function putContextState(slug: string, context: FilterCondition[]): Promise<void> {\n return axios.put(getConfig().views.contextStatesEndpoint, { slug, context }, silent).then(() => undefined);\n}\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport { putContextState } from '../../services/context-states.service';\nimport type { FilterCondition } from '../../types/conditions';\n\nconst SAVE_DELAY_MS = 150;\n\nexport interface ContextSave {\n slug: string;\n context: FilterCondition[];\n}\n\ninterface NumberedSave extends ContextSave {\n // Counts up with every save, so a save can tell whether a newer one has replaced it\n number: number;\n}\n\ninterface UseContextSaveOptions {\n // Called when the latest save has reached the server\n onSaved: (save: ContextSave) => unknown;\n // Called when the latest save could not be stored\n onFailed: () => unknown;\n}\n\n// Saves a view's context to the server, 150 ms after the last change:\n// - saves reach the server in the order they were made, each one after the previous one finished\n// - only the latest save reports back, so an older one finishing late changes nothing\n// - a change still waiting when the view goes away (or switches to another slug) is saved straight away\nexport function useContextSave({ onSaved, onFailed }: UseContextSaveOptions) {\n const [isSaving, setIsSaving] = useState(false);\n const latestNumber = useRef(0);\n const waiting = useRef<NumberedSave | null>(null);\n const timer = useRef<ReturnType<typeof setTimeout> | null>(null);\n const previousRequest = useRef<Promise<boolean>>(Promise.resolve(true));\n const callbacks = useRef({ onSaved, onFailed });\n callbacks.current = { onSaved, onFailed };\n\n // Sends one save once the previous one has finished. Resolves whether it reached the server; it never rejects, so one\n // failed save doesn't stop the ones after it.\n const send = useCallback((save: ContextSave): Promise<boolean> => {\n const request = previousRequest.current.then(() =>\n putContextState(save.slug, save.context).then(\n () => true,\n () => false,\n ),\n );\n previousRequest.current = request;\n return request;\n }, []);\n\n // Sends the waiting save, then reports back unless a newer save has replaced it\n const sendWaiting = useCallback(async () => {\n const save = waiting.current;\n if (!save) return;\n waiting.current = null;\n\n const reachedServer = await send(save);\n const isLatest = save.number === latestNumber.current;\n if (!isLatest) return;\n\n if (reachedServer) await callbacks.current.onSaved(save);\n else await callbacks.current.onFailed();\n setIsSaving(false);\n }, [send]);\n\n const save = useCallback(\n (slug: string, context: FilterCondition[]) => {\n const previous = waiting.current;\n if (previous && previous.slug !== slug) void send(previous);\n\n latestNumber.current += 1;\n waiting.current = { slug, context, number: latestNumber.current };\n setIsSaving(true);\n\n if (timer.current) clearTimeout(timer.current);\n timer.current = setTimeout(sendWaiting, SAVE_DELAY_MS);\n },\n [send, sendWaiting],\n );\n\n // Leaving the view saves a change that is still waiting\n useEffect(\n () => () => {\n if (timer.current) clearTimeout(timer.current);\n if (waiting.current) void send(waiting.current);\n },\n [send],\n );\n\n return { save, isSaving };\n}\n","import { useCallback, useState } from 'react';\n\nconst NO_FIELDS: ReadonlySet<string> = new Set();\n\n// The context fields whose control is still settling its value — picking its first option, or checking that its held\n// value still exists\nexport function useSettlingFields() {\n const [fields, setFields] = useState(NO_FIELDS);\n\n const markFieldSettling = useCallback((field: string, settling: boolean) => {\n setFields((current) => {\n if (current.has(field) === settling) return current;\n const next = new Set(current);\n if (settling) next.add(field);\n else next.delete(field);\n return next;\n });\n }, []);\n\n return { hasSettlingFields: fields.size > 0, markFieldSettling };\n}\n","import { useQuery, useQueryClient } from '@tanstack/react-query';\nimport { deepEqual } from 'fast-equals';\nimport { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';\nimport { getContextState } from '../../services/context-states.service';\nimport type { FilterCondition } from '../../types/conditions';\nimport { readValue, readValues, withChange, withDefaults } from '../../utils/conditions';\nimport type { AnySelectOption } from '../Select/types';\nimport { toast } from '../Sonner/toast';\nimport { type ContextSave, useContextSave } from './useContextSave';\nimport { useSettlingFields } from './useSettlingFields';\n\ninterface UseContextStateOptions {\n /** Where the context is saved; without one the context lives only in memory */\n slug?: string;\n /** Values the context always has — filled whenever a field is missing */\n defaultContext?: FilterCondition[];\n /** Values an in-memory context (no slug) starts with, which can still be changed or cleared */\n initialContext?: FilterCondition[];\n /** Fields whose default replaces the saved value every time the view opens */\n resetOnOpen?: string[];\n /** Fields cleared when another field changes, e.g. `{ site: ['supplierIds'] }` */\n resets?: Record<string, string[]>;\n /** Called after a change is saved — the view refetches its data here */\n onStatePush?: () => unknown;\n /** Called with each field a change sets or clears, as the change is shown */\n onChange?: (field: string, condition: FilterCondition | null) => void;\n}\n\nexport interface ContextStateInstance<TOption extends AnySelectOption = AnySelectOption> {\n context: FilterCondition[];\n contextValue: (field: string) => string | undefined;\n contextValues: (field: string) => string[];\n setContext: (field: string, condition: FilterCondition | null) => void;\n required: ReadonlySet<string>;\n isLoaded: boolean;\n isSaving: boolean;\n /** The context isn't final yet: it is loading, a change is saving, or a control is still settling its value (picking\n * a first option, checking a held one). A view holds its data request until it settles. */\n isSettling: boolean;\n markFieldSettling: (field: string, settling: boolean) => void;\n /** The options a Select given this context holds, in the order of its value — picked, looked up, or looked up again\n * under new filters; in memory only, never saved */\n selectedOptions: TOption[];\n /** The first of them, for a single select */\n selectedOption: TOption | null;\n setSelectedOptions: (options: TOption[]) => void;\n}\n\n// The context a Select reports its options to: any context whose options the select's options are — a page may keep a\n// narrower view of them (the fields it reads) than the select's own\nexport type SelectContextInstance<TOption extends AnySelectOption> = Omit<\n ContextStateInstance,\n 'selectedOptions' | 'selectedOption' | 'setSelectedOptions'\n> & {\n selectedOptions: readonly AnySelectOption[];\n setSelectedOptions: (options: TOption[]) => void;\n};\n\nconst NO_CONDITIONS: FilterCondition[] = [];\nconst NO_OPTIONS: AnySelectOption[] = [];\nconst NO_FIELDS: string[] = [];\nconst NO_RESETS: Record<string, string[]> = {};\n\nfunction contextQueryKey(slug: string | undefined) {\n return ['context-state', slug ?? ''] as const;\n}\n\n// A view's context bar: the saved conditions for this visit, with defaults filled in. Every change is shown at once\n// and saved in the background. TOption types the options the context's Select holds (selectedOptions)\nexport function useContextState<TOption extends AnySelectOption = AnySelectOption>({\n slug,\n defaultContext,\n initialContext,\n resetOnOpen = NO_FIELDS,\n resets = NO_RESETS,\n onStatePush,\n onChange,\n}: UseContextStateOptions): ContextStateInstance<TOption> {\n const queryClient = useQueryClient();\n const queryKey = useMemo(() => contextQueryKey(slug), [slug]);\n\n // Read when the view opens (and after a failed save) — never in the background, where a read landing mid-save would\n // bring the old context back\n const {\n data: stored,\n isError: readFailed,\n refetch,\n } = useQuery({\n queryKey,\n queryFn: () => getContextState(slug ?? ''),\n enabled: slug != null,\n staleTime: Number.POSITIVE_INFINITY,\n refetchOnMount: 'always',\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n });\n\n // The defaults, kept stable while their contents don't change\n const nextDefaults = defaultContext ?? NO_CONDITIONS;\n const [defaults, setDefaults] = useState(nextDefaults);\n if (!deepEqual(defaults, nextDefaults)) setDefaults(nextDefaults);\n\n // The context on screen, held per slug so switching views never shows another view's context\n const [held, setHeld] = useState<{ slug: string | undefined; context: FilterCondition[] } | null>(null);\n const hasHeld = held != null && held.slug === slug;\n const context = hasHeld ? held.context : NO_CONDITIONS;\n\n const onStatePushRef = useRef(onStatePush);\n onStatePushRef.current = onStatePush;\n const onChangeRef = useRef(onChange);\n onChangeRef.current = onChange;\n // Read once, as the context starts\n const initialRef = useRef(initialContext ?? NO_CONDITIONS);\n\n const { save, isSaving } = useContextSave({\n onSaved: ({ slug: savedSlug, context: savedContext }: ContextSave) => {\n queryClient.setQueryData(contextQueryKey(savedSlug), savedContext);\n return onStatePushRef.current?.();\n },\n onFailed: () => {\n toast.error(\"Couldn't save the change\", { description: 'The view keeps what was saved before.' });\n return refetch();\n },\n });\n\n // Saves a context: it becomes the cached read at once, so a read already on its way can't bring the old one back\n const persist = useCallback(\n (next: FilterCondition[]) => {\n if (slug == null) return;\n void queryClient.cancelQueries({ queryKey });\n queryClient.setQueryData(queryKey, next);\n save(slug, next);\n },\n [slug, queryClient, queryKey, save],\n );\n\n // Shows a context and saves it\n const showAndPersist = useCallback(\n (next: FilterCondition[]) => {\n setHeld({ slug, context: next });\n persist(next);\n },\n [slug, persist],\n );\n\n // 1. Nothing to read (no slug, or the read failed): start from the initial values and the defaults, so the view still\n // answers\n useLayoutEffect(() => {\n const canRead = slug != null && !readFailed;\n if (hasHeld || canRead) return;\n setHeld({ slug, context: withDefaults(initialRef.current, defaults, { isOpening: true, resetOnOpen }) });\n }, [hasHeld, slug, readFailed, defaults, resetOnOpen]);\n\n // 2. The saved context arrives: show it with its defaults filled, and save it if filling changed it\n useLayoutEffect(() => {\n if (hasHeld || !stored) return;\n const next = withDefaults(stored, defaults, { isOpening: true, resetOnOpen });\n setHeld({ slug, context: next });\n if (!deepEqual(next, stored)) persist(next);\n }, [hasHeld, stored, defaults, resetOnOpen, slug, persist]);\n\n // 3. The saved context was read again (after a failed save): show it, unless a change of ours is still saving\n useLayoutEffect(() => {\n if (!hasHeld || isSaving || !stored) return;\n const next = withDefaults(stored, defaults);\n if (deepEqual(next, held.context)) return;\n setHeld({ slug, context: next });\n if (!deepEqual(next, stored)) persist(next);\n }, [hasHeld, isSaving, stored, held, defaults, slug, persist]);\n\n // 4. A default that becomes known after the context loaded is filled in\n useLayoutEffect(() => {\n if (!hasHeld) return;\n const next = withDefaults(held.context, defaults);\n if (next !== held.context) showAndPersist(next);\n }, [hasHeld, held, defaults, showAndPersist]);\n\n const setContext = useCallback(\n (field: string, condition: FilterCondition | null) => {\n if (!hasHeld) return;\n const current = held.context.find((existing) => existing.field === field) ?? null;\n if (deepEqual(current, condition)) return;\n const cleared = resets[field] ?? NO_FIELDS;\n const next = withDefaults(withChange(held.context, field, condition, cleared), defaults);\n showAndPersist(next);\n onChangeRef.current?.(field, condition);\n // The fields the change cleared are reported too, as they now stand: gone, or back on their default\n for (const reset of cleared) {\n const before = held.context.find((existing) => existing.field === reset) ?? null;\n const after = next.find((existing) => existing.field === reset) ?? null;\n if (!deepEqual(before, after)) onChangeRef.current?.(reset, after);\n }\n },\n [hasHeld, held, resets, defaults, showAndPersist],\n );\n\n const contextValue = useCallback((field: string) => readValue(context, field), [context]);\n const contextValues = useCallback((field: string) => readValues(context, field), [context]);\n const required = useMemo(() => new Set(defaults.map((condition) => condition.field)), [defaults]);\n const { hasSettlingFields, markFieldSettling } = useSettlingFields();\n const [selectedOptions, setHeldOptions] = useState<TOption[]>(NO_OPTIONS as TOption[]);\n // The same options again leave the held ones in place, so the page does not render for nothing\n const setSelectedOptions = useCallback(\n (options: TOption[]) => setHeldOptions((current) => (deepEqual(current, options) ? current : options)),\n [],\n );\n\n const isLoaded = hasHeld;\n const isSettling = !isLoaded || isSaving || hasSettlingFields;\n\n return useMemo(\n () => ({\n context,\n contextValue,\n contextValues,\n setContext,\n required,\n isLoaded,\n isSaving,\n isSettling,\n markFieldSettling,\n selectedOptions,\n selectedOption: selectedOptions[0] ?? null,\n setSelectedOptions,\n }),\n [\n context,\n contextValue,\n contextValues,\n setContext,\n required,\n isLoaded,\n isSaving,\n isSettling,\n markFieldSettling,\n selectedOptions,\n setSelectedOptions,\n ],\n );\n}\n"],"names":["save","NO_FIELDS"],"mappings":";;;;;;;AAIA,MAAM,MAAA,GAAS,EAAE,gBAAA,EAAkB,KAAA,EAAO,gBAAgB,KAAA,EAAM;AAGzD,SAAS,gBAAgB,IAAA,EAA0C;AACxE,EAAA,OAAO,KAAA,CACJ,IAAoC,SAAA,EAAU,CAAE,MAAM,qBAAA,EAAuB,EAAE,QAAQ,EAAE,IAAA,IAAQ,GAAG,MAAA,EAAQ,CAAA,CAC5G,IAAA,CAAK,CAAC,CAAA,KAAM,CAAA,CAAE,KAAK,OAAO,CAAA;AAC/B;AAGO,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