@entifix/react-integration 0.21.0 → 0.22.1
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/hooks/use-data-loading/use-data-loading.d.ts +1 -1
- package/dist/hooks/use-data-loading/use-data-loading.d.ts.map +1 -1
- package/dist/hooks/use-data-loading/use-data-loading.js +4 -3
- package/dist/hooks/use-data-loading/use-data-loading.js.map +1 -1
- package/dist/hooks/use-data-loading/use-data-loading.types.d.ts +10 -0
- package/dist/hooks/use-data-loading/use-data-loading.types.d.ts.map +1 -1
- package/dist/hooks/use-data-loading/use-data-loading.types.js.map +1 -1
- package/dist/hooks/use-entity-link-source/use-entity-link-source.d.ts +1 -1
- package/dist/hooks/use-entity-link-source/use-entity-link-source.d.ts.map +1 -1
- package/dist/hooks/use-entity-link-source/use-entity-link-source.js +35 -15
- package/dist/hooks/use-entity-link-source/use-entity-link-source.js.map +1 -1
- package/dist/hooks/use-entity-link-source/use-entity-link-source.types.d.ts +7 -1
- package/dist/hooks/use-entity-link-source/use-entity-link-source.types.d.ts.map +1 -1
- package/dist/hooks/use-entity-link-source/use-entity-link-source.types.js.map +1 -1
- package/package.json +5 -5
|
@@ -9,7 +9,7 @@ import type { UseDataLoadingOptions } from './use-data-loading.types.js';
|
|
|
9
9
|
* `uc`/`ctx` come in the same way and the Effect runs against the same provided
|
|
10
10
|
* Tags, so callers and organisms are untouched.
|
|
11
11
|
*/
|
|
12
|
-
export declare function useDataLoading<TEntity extends Entity, TContext>({ uc, ctx, initialPageSize, queryKey, enabled, baseFiltering, initialFiltering, }: UseDataLoadingOptions<TEntity, TContext>): {
|
|
12
|
+
export declare function useDataLoading<TEntity extends Entity, TContext>({ uc, ctx, initialPageSize, queryKey, enabled, baseFiltering, initialFiltering, initialSorting, }: UseDataLoadingOptions<TEntity, TContext>): {
|
|
13
13
|
isLoading: boolean;
|
|
14
14
|
items: TEntity[];
|
|
15
15
|
totalItems: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-data-loading.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-data-loading/use-data-loading.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,MAAM,EAGX,KAAK,aAAa,EAClB,KAAK,WAAW,EAGjB,MAAM,eAAe,CAAC;AAavB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"use-data-loading.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-data-loading/use-data-loading.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,MAAM,EAGX,KAAK,aAAa,EAClB,KAAK,WAAW,EAGjB,MAAM,eAAe,CAAC;AAavB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAqCzE;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,OAAO,SAAS,MAAM,EAAE,QAAQ,EAAE,EAC/D,EAAE,EACF,GAAG,EACH,eAAmC,EACnC,QAAQ,EACR,OAAc,EACd,aAAa,EACb,gBAAgB,EAChB,cAAc,GACf,EAAE,qBAAqB,CAAC,OAAO,EAAE,QAAQ,CAAC;;;;;;;;;4BAmHE,MAAM;oCAIE,MAAM;mCAMP,WAAW,CAAC,OAAO,CAAC;+BAIxB,aAAa,CAAC,OAAO,CAAC;IAiBlE,yEAAyE;;EAG5E"}
|
|
@@ -14,7 +14,7 @@ function initialRequest(seed) {
|
|
|
14
14
|
currentPage: 1,
|
|
15
15
|
pageSize: seed.pageSize,
|
|
16
16
|
filtering: seed.filtering,
|
|
17
|
-
sorting:
|
|
17
|
+
sorting: seed.sorting
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
@@ -25,12 +25,13 @@ function initialRequest(seed) {
|
|
|
25
25
|
* and reactive events invalidate the list. The public shape is unchanged: the
|
|
26
26
|
* `uc`/`ctx` come in the same way and the Effect runs against the same provided
|
|
27
27
|
* Tags, so callers and organisms are untouched.
|
|
28
|
-
*/ export function useDataLoading({ uc, ctx, initialPageSize = DEFAULT_PAGE_SIZE, queryKey, enabled = true, baseFiltering, initialFiltering }) {
|
|
28
|
+
*/ export function useDataLoading({ uc, ctx, initialPageSize = DEFAULT_PAGE_SIZE, queryKey, enabled = true, baseFiltering, initialFiltering, initialSorting }) {
|
|
29
29
|
var _ref, _ref1, _query_error;
|
|
30
30
|
var _query_data, _query_data1;
|
|
31
31
|
const [request, dispatch] = useReducer(requestReducer, {
|
|
32
32
|
pageSize: initialPageSize,
|
|
33
|
-
filtering: initialFiltering
|
|
33
|
+
filtering: initialFiltering,
|
|
34
|
+
sorting: initialSorting
|
|
34
35
|
}, initialRequest);
|
|
35
36
|
// Callers rebuild `uc`/`ctx` inline every render, so hold the latest in refs
|
|
36
37
|
// and keep them out of the query key — keying on their identity would refetch
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/use-data-loading/use-data-loading.ts"],"sourcesContent":["import { EntityLoadRequestTag } from '@entifix/business';\nimport {\n type EntifixFailure,\n type Entity,\n type EntityLoadRequest,\n type EntityPage,\n type EntitySorting,\n type FilterGroup,\n serializeRsql,\n serializeSort,\n} from '@entifix/core';\nimport { keepPreviousData, useQuery } from '@tanstack/react-query';\nimport { Context, Effect } from 'effect';\nimport {\n useCallback,\n useEffect,\n useId,\n useMemo,\n useReducer,\n useRef,\n} from 'react';\n\nimport { combineFilterGroups } from '../../query/combine-filtering.js';\nimport type { UseDataLoadingOptions } from './use-data-loading.types.js';\n\nconst DEFAULT_PAGE_SIZE = 10;\n\n/**\n * The request the caller controls — page, size, and the *applied* filter/sort.\n * The response (items, loading, error) is owned by the query cache, not here.\n */\ninterface RequestState<T extends Entity> {\n currentPage: number;\n pageSize: number;\n filtering: FilterGroup<T> | undefined;\n sorting: EntitySorting<T> | undefined;\n}\n\ntype RequestAction<T extends Entity> = { set: Partial<RequestState<T>> };\n\nfunction requestReducer<T extends Entity>(\n state: RequestState<T>,\n action: RequestAction<T>,\n): RequestState<T> {\n return { ...state, ...action.set };\n}\n\nfunction initialRequest<T extends Entity>(seed: {\n pageSize: number;\n filtering: FilterGroup<T> | undefined;\n}): RequestState<T> {\n return {\n currentPage: 1,\n pageSize: seed.pageSize,\n filtering: seed.filtering,\n sorting: undefined,\n };\n}\n\n/**\n * Runs an entity list use-case and caches the result through TanStack Query.\n *\n * The reducer owns the *request* (page/size/filter/sort); `useQuery` owns the\n * *fetch* — deduping, caching, and (with a shared `queryKey`) letting mutations\n * and reactive events invalidate the list. The public shape is unchanged: the\n * `uc`/`ctx` come in the same way and the Effect runs against the same provided\n * Tags, so callers and organisms are untouched.\n */\nexport function useDataLoading<TEntity extends Entity, TContext>({\n uc,\n ctx,\n initialPageSize = DEFAULT_PAGE_SIZE,\n queryKey,\n enabled = true,\n baseFiltering,\n initialFiltering,\n}: UseDataLoadingOptions<TEntity, TContext>) {\n const [request, dispatch] = useReducer(\n requestReducer as (\n state: RequestState<TEntity>,\n action: RequestAction<TEntity>,\n ) => RequestState<TEntity>,\n { pageSize: initialPageSize, filtering: initialFiltering },\n initialRequest<TEntity>,\n );\n\n // Callers rebuild `uc`/`ctx` inline every render, so hold the latest in refs\n // and keep them out of the query key — keying on their identity would refetch\n // on every render. Written in an effect (refs may not be set during render);\n // effects fire in order, so the query below always reads the latest.\n const ucRef = useRef(uc);\n const ctxRef = useRef(ctx);\n useEffect(() => {\n ucRef.current = uc;\n ctxRef.current = ctx;\n });\n\n // What actually goes on the wire: the caller's standing restriction ANDed with\n // whatever the user applied. Kept separate from `request.filtering` because the\n // restriction is not the user's filter — the panel must keep showing only what\n // they set, while the request keeps carrying both.\n const effectiveFiltering = useMemo(\n () => combineFilterGroups<TEntity>(baseFiltering, request.filtering),\n [baseFiltering, request.filtering],\n );\n\n // The applied filter/sort are rebuilt each render, so their *serialized* form\n // — the same codec the request needs anyway — is what keys the query: a stable\n // string that changes exactly when the query does.\n const rsql = useMemo(\n () => serializeRsql(effectiveFiltering ? [effectiveFiltering] : undefined),\n [effectiveFiltering],\n );\n const sort = useMemo(\n () => serializeSort(request.sorting ? [request.sorting] : undefined),\n [request.sorting],\n );\n\n const loadRequest = useMemo<EntityLoadRequest<TEntity>>(() => {\n const built: EntityLoadRequest<TEntity> = {\n page: request.currentPage,\n pageSize: request.pageSize,\n };\n // An empty group serializes to nothing; omit it rather than send a filter\n // that matches everything.\n if (rsql !== '' && effectiveFiltering) {\n built.filtering = [effectiveFiltering];\n }\n if (sort !== '' && request.sorting) {\n built.sorting = [request.sorting];\n }\n return built;\n }, [\n request.currentPage,\n request.pageSize,\n effectiveFiltering,\n request.sorting,\n rsql,\n sort,\n ]);\n\n // Falls back to a per-instance id so an un-scoped list never collides with\n // another in the shared cache — correct, just unshared (see the type doc).\n const instanceId = useId();\n const scope = queryKey ?? [instanceId];\n\n const query = useQuery<EntityPage<TEntity>, EntifixFailure>({\n enabled,\n /**\n * Page, size, filter and sort are all part of the key, so every one of them\n * is a *different* query — and without this, `data` goes undefined the\n * instant any of them changes. The table would then see `items.length === 0`\n * with `isFetching` true, read it as a first load, and blank populated rows\n * to skeleton on every pagination click: the grey flash the loading contract\n * exists to prevent. Keeping the previous page is what makes \"skeleton only\n * on first load\" true rather than merely intended.\n */\n placeholderData: keepPreviousData,\n queryKey: [\n ...scope,\n 'load',\n request.currentPage,\n request.pageSize,\n rsql,\n sort,\n ],\n queryFn: () =>\n Effect.runPromise(\n Effect.provide(\n ucRef.current,\n ctxRef.current.pipe(\n Context.add(\n EntityLoadRequestTag,\n // The tag carries no generic, so the entity-typed request crosses\n // it — the same cast `loadUCFactory` makes reading it back.\n loadRequest as unknown as EntityLoadRequest,\n ),\n ),\n ),\n ),\n });\n\n const { refetch: refetchQuery } = query;\n const refetch = useCallback(() => {\n void refetchQuery();\n }, [refetchQuery]);\n\n const onPageChange = useCallback((newPage: number) => {\n dispatch({ set: { currentPage: newPage } });\n }, []);\n\n const onPageSizeChange = useCallback((newPageSize: number) => {\n dispatch({ set: { pageSize: newPageSize, currentPage: 1 } });\n }, []);\n\n // Narrowing the result set invalidates the page number: page 4 of the old\n // result is very likely past the end of the new one.\n const onFilteringChange = useCallback((filtering: FilterGroup<TEntity>) => {\n dispatch({ set: { filtering, currentPage: 1 } });\n }, []);\n\n const onSortingChange = useCallback((sorting: EntitySorting<TEntity>) => {\n dispatch({ set: { sorting, currentPage: 1 } });\n }, []);\n\n return {\n isLoading: query.isFetching,\n items: query.data?.items ?? ([] as Array<TEntity>),\n totalItems: query.data?.total ?? 0,\n currentPage: request.currentPage,\n pageSize: request.pageSize,\n error: query.error ?? undefined,\n filtering: request.filtering,\n sorting: request.sorting,\n onPageChange,\n onPageSizeChange,\n onFilteringChange,\n onSortingChange,\n /** Reads the current page again — what a failed load offers as Retry. */\n refetch,\n };\n}\n"],"names":["EntityLoadRequestTag","serializeRsql","serializeSort","keepPreviousData","useQuery","Context","Effect","useCallback","useEffect","useId","useMemo","useReducer","useRef","combineFilterGroups","DEFAULT_PAGE_SIZE","requestReducer","state","action","set","initialRequest","seed","currentPage","pageSize","filtering","sorting","undefined","useDataLoading","uc","ctx","initialPageSize","queryKey","enabled","baseFiltering","initialFiltering","query","request","dispatch","ucRef","ctxRef","current","effectiveFiltering","rsql","sort","loadRequest","built","page","instanceId","scope","placeholderData","queryFn","runPromise","provide","pipe","add","refetch","refetchQuery","onPageChange","newPage","onPageSizeChange","newPageSize","onFilteringChange","onSortingChange","isLoading","isFetching","items","data","totalItems","total","error"],"mappings":";AAAA,SAASA,oBAAoB,QAAQ,oBAAoB;AACzD,SAOEC,aAAa,EACbC,aAAa,QACR,gBAAgB;AACvB,SAASC,gBAAgB,EAAEC,QAAQ,QAAQ,wBAAwB;AACnE,SAASC,OAAO,EAAEC,MAAM,QAAQ,SAAS;AACzC,SACEC,WAAW,EACXC,SAAS,EACTC,KAAK,EACLC,OAAO,EACPC,UAAU,EACVC,MAAM,QACD,QAAQ;AAEf,S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D,MAAMuC,oBAAoBrD,YAAY,CAACgB;QACrCa,SAAS;YAAElB,KAAK;gBAAEK;gBAAWF,aAAa;YAAE;QAAE;IAChD,GAAG,EAAE;IAEL,MAAMwC,kBAAkBtD,YAAY,CAACiB;QACnCY,SAAS;YAAElB,KAAK;gBAAEM;gBAASH,aAAa;YAAE;QAAE;IAC9C,GAAG,EAAE;IAEL,OAAO;QACLyC,WAAW5B,MAAM6B,UAAU;QAC3BC,KAAK,WAAE9B,cAAAA,MAAM+B,IAAI,qBAAV/B,YAAY8B,KAAK,mBAAK,EAAE;QAC/BE,UAAU,YAAEhC,eAAAA,MAAM+B,IAAI,qBAAV/B,aAAYiC,KAAK,oBAAI;QACjC9C,aAAac,QAAQd,WAAW;QAChCC,UAAUa,QAAQb,QAAQ;QAC1B8C,KAAK,GAAElC,eAAAA,MAAMkC,KAAK,YAAXlC,eAAeT;QACtBF,WAAWY,QAAQZ,SAAS;QAC5BC,SAASW,QAAQX,OAAO;QACxBgC;QACAE;QACAE;QACAC;QACA,uEAAuE,GACvEP;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/use-data-loading/use-data-loading.ts"],"sourcesContent":["import { EntityLoadRequestTag } from '@entifix/business';\nimport {\n type EntifixFailure,\n type Entity,\n type EntityLoadRequest,\n type EntityPage,\n type EntitySorting,\n type FilterGroup,\n serializeRsql,\n serializeSort,\n} from '@entifix/core';\nimport { keepPreviousData, useQuery } from '@tanstack/react-query';\nimport { Context, Effect } from 'effect';\nimport {\n useCallback,\n useEffect,\n useId,\n useMemo,\n useReducer,\n useRef,\n} from 'react';\n\nimport { combineFilterGroups } from '../../query/combine-filtering.js';\nimport type { UseDataLoadingOptions } from './use-data-loading.types.js';\n\nconst DEFAULT_PAGE_SIZE = 10;\n\n/**\n * The request the caller controls — page, size, and the *applied* filter/sort.\n * The response (items, loading, error) is owned by the query cache, not here.\n */\ninterface RequestState<T extends Entity> {\n currentPage: number;\n pageSize: number;\n filtering: FilterGroup<T> | undefined;\n sorting: EntitySorting<T> | undefined;\n}\n\ntype RequestAction<T extends Entity> = { set: Partial<RequestState<T>> };\n\nfunction requestReducer<T extends Entity>(\n state: RequestState<T>,\n action: RequestAction<T>,\n): RequestState<T> {\n return { ...state, ...action.set };\n}\n\nfunction initialRequest<T extends Entity>(seed: {\n pageSize: number;\n filtering: FilterGroup<T> | undefined;\n sorting: EntitySorting<T> | undefined;\n}): RequestState<T> {\n return {\n currentPage: 1,\n pageSize: seed.pageSize,\n filtering: seed.filtering,\n sorting: seed.sorting,\n };\n}\n\n/**\n * Runs an entity list use-case and caches the result through TanStack Query.\n *\n * The reducer owns the *request* (page/size/filter/sort); `useQuery` owns the\n * *fetch* — deduping, caching, and (with a shared `queryKey`) letting mutations\n * and reactive events invalidate the list. The public shape is unchanged: the\n * `uc`/`ctx` come in the same way and the Effect runs against the same provided\n * Tags, so callers and organisms are untouched.\n */\nexport function useDataLoading<TEntity extends Entity, TContext>({\n uc,\n ctx,\n initialPageSize = DEFAULT_PAGE_SIZE,\n queryKey,\n enabled = true,\n baseFiltering,\n initialFiltering,\n initialSorting,\n}: UseDataLoadingOptions<TEntity, TContext>) {\n const [request, dispatch] = useReducer(\n requestReducer as (\n state: RequestState<TEntity>,\n action: RequestAction<TEntity>,\n ) => RequestState<TEntity>,\n {\n pageSize: initialPageSize,\n filtering: initialFiltering,\n sorting: initialSorting,\n },\n initialRequest<TEntity>,\n );\n\n // Callers rebuild `uc`/`ctx` inline every render, so hold the latest in refs\n // and keep them out of the query key — keying on their identity would refetch\n // on every render. Written in an effect (refs may not be set during render);\n // effects fire in order, so the query below always reads the latest.\n const ucRef = useRef(uc);\n const ctxRef = useRef(ctx);\n useEffect(() => {\n ucRef.current = uc;\n ctxRef.current = ctx;\n });\n\n // What actually goes on the wire: the caller's standing restriction ANDed with\n // whatever the user applied. Kept separate from `request.filtering` because the\n // restriction is not the user's filter — the panel must keep showing only what\n // they set, while the request keeps carrying both.\n const effectiveFiltering = useMemo(\n () => combineFilterGroups<TEntity>(baseFiltering, request.filtering),\n [baseFiltering, request.filtering],\n );\n\n // The applied filter/sort are rebuilt each render, so their *serialized* form\n // — the same codec the request needs anyway — is what keys the query: a stable\n // string that changes exactly when the query does.\n const rsql = useMemo(\n () => serializeRsql(effectiveFiltering ? [effectiveFiltering] : undefined),\n [effectiveFiltering],\n );\n const sort = useMemo(\n () => serializeSort(request.sorting ? [request.sorting] : undefined),\n [request.sorting],\n );\n\n const loadRequest = useMemo<EntityLoadRequest<TEntity>>(() => {\n const built: EntityLoadRequest<TEntity> = {\n page: request.currentPage,\n pageSize: request.pageSize,\n };\n // An empty group serializes to nothing; omit it rather than send a filter\n // that matches everything.\n if (rsql !== '' && effectiveFiltering) {\n built.filtering = [effectiveFiltering];\n }\n if (sort !== '' && request.sorting) {\n built.sorting = [request.sorting];\n }\n return built;\n }, [\n request.currentPage,\n request.pageSize,\n effectiveFiltering,\n request.sorting,\n rsql,\n sort,\n ]);\n\n // Falls back to a per-instance id so an un-scoped list never collides with\n // another in the shared cache — correct, just unshared (see the type doc).\n const instanceId = useId();\n const scope = queryKey ?? [instanceId];\n\n const query = useQuery<EntityPage<TEntity>, EntifixFailure>({\n enabled,\n /**\n * Page, size, filter and sort are all part of the key, so every one of them\n * is a *different* query — and without this, `data` goes undefined the\n * instant any of them changes. The table would then see `items.length === 0`\n * with `isFetching` true, read it as a first load, and blank populated rows\n * to skeleton on every pagination click: the grey flash the loading contract\n * exists to prevent. Keeping the previous page is what makes \"skeleton only\n * on first load\" true rather than merely intended.\n */\n placeholderData: keepPreviousData,\n queryKey: [\n ...scope,\n 'load',\n request.currentPage,\n request.pageSize,\n rsql,\n sort,\n ],\n queryFn: () =>\n Effect.runPromise(\n Effect.provide(\n ucRef.current,\n ctxRef.current.pipe(\n Context.add(\n EntityLoadRequestTag,\n // The tag carries no generic, so the entity-typed request crosses\n // it — the same cast `loadUCFactory` makes reading it back.\n loadRequest as unknown as EntityLoadRequest,\n ),\n ),\n ),\n ),\n });\n\n const { refetch: refetchQuery } = query;\n const refetch = useCallback(() => {\n void refetchQuery();\n }, [refetchQuery]);\n\n const onPageChange = useCallback((newPage: number) => {\n dispatch({ set: { currentPage: newPage } });\n }, []);\n\n const onPageSizeChange = useCallback((newPageSize: number) => {\n dispatch({ set: { pageSize: newPageSize, currentPage: 1 } });\n }, []);\n\n // Narrowing the result set invalidates the page number: page 4 of the old\n // result is very likely past the end of the new one.\n const onFilteringChange = useCallback((filtering: FilterGroup<TEntity>) => {\n dispatch({ set: { filtering, currentPage: 1 } });\n }, []);\n\n const onSortingChange = useCallback((sorting: EntitySorting<TEntity>) => {\n dispatch({ set: { sorting, currentPage: 1 } });\n }, []);\n\n return {\n isLoading: query.isFetching,\n items: query.data?.items ?? ([] as Array<TEntity>),\n totalItems: query.data?.total ?? 0,\n currentPage: request.currentPage,\n pageSize: request.pageSize,\n error: query.error ?? undefined,\n filtering: request.filtering,\n sorting: request.sorting,\n onPageChange,\n onPageSizeChange,\n onFilteringChange,\n onSortingChange,\n /** Reads the current page again — what a failed load offers as Retry. */\n refetch,\n };\n}\n"],"names":["EntityLoadRequestTag","serializeRsql","serializeSort","keepPreviousData","useQuery","Context","Effect","useCallback","useEffect","useId","useMemo","useReducer","useRef","combineFilterGroups","DEFAULT_PAGE_SIZE","requestReducer","state","action","set","initialRequest","seed","currentPage","pageSize","filtering","sorting","useDataLoading","uc","ctx","initialPageSize","queryKey","enabled","baseFiltering","initialFiltering","initialSorting","query","request","dispatch","ucRef","ctxRef","current","effectiveFiltering","rsql","undefined","sort","loadRequest","built","page","instanceId","scope","placeholderData","queryFn","runPromise","provide","pipe","add","refetch","refetchQuery","onPageChange","newPage","onPageSizeChange","newPageSize","onFilteringChange","onSortingChange","isLoading","isFetching","items","data","totalItems","total","error"],"mappings":";AAAA,SAASA,oBAAoB,QAAQ,oBAAoB;AACzD,SAOEC,aAAa,EACbC,aAAa,QACR,gBAAgB;AACvB,SAASC,gBAAgB,EAAEC,QAAQ,QAAQ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AE;IAC3C,GAAG,EAAE;IAEL,MAAMC,mBAAmBpD,YAAY,CAACqD;QACpCxB,SAAS;YAAElB,KAAK;gBAAEI,UAAUsC;gBAAavC,aAAa;YAAE;QAAE;IAC5D,GAAG,EAAE;IAEL,0EAA0E;IAC1E,qDAAqD;IACrD,MAAMwC,oBAAoBtD,YAAY,CAACgB;QACrCa,SAAS;YAAElB,KAAK;gBAAEK;gBAAWF,aAAa;YAAE;QAAE;IAChD,GAAG,EAAE;IAEL,MAAMyC,kBAAkBvD,YAAY,CAACiB;QACnCY,SAAS;YAAElB,KAAK;gBAAEM;gBAASH,aAAa;YAAE;QAAE;IAC9C,GAAG,EAAE;IAEL,OAAO;QACL0C,WAAW7B,MAAM8B,UAAU;QAC3BC,KAAK,WAAE/B,cAAAA,MAAMgC,IAAI,qBAAVhC,YAAY+B,KAAK,mBAAK,EAAE;QAC/BE,UAAU,YAAEjC,eAAAA,MAAMgC,IAAI,qBAAVhC,aAAYkC,KAAK,oBAAI;QACjC/C,aAAac,QAAQd,WAAW;QAChCC,UAAUa,QAAQb,QAAQ;QAC1B+C,KAAK,GAAEnC,eAAAA,MAAMmC,KAAK,YAAXnC,eAAeQ;QACtBnB,WAAWY,QAAQZ,SAAS;QAC5BC,SAASW,QAAQX,OAAO;QACxBiC;QACAE;QACAE;QACAC;QACA,uEAAuE,GACvEP;IACF;AACF"}
|
|
@@ -60,5 +60,15 @@ export interface UseDataLoadingOptions<TEntity extends Entity, TContext> {
|
|
|
60
60
|
* Read once, on mount, like {@link initialPageSize}; a later value is ignored.
|
|
61
61
|
*/
|
|
62
62
|
initialFiltering?: FilterGroup<TEntity>;
|
|
63
|
+
/**
|
|
64
|
+
* The order the list *opens* in: sent with the first load and handed back as
|
|
65
|
+
* `sorting`, so the header reads as sorted by it exactly as if the user had
|
|
66
|
+
* clicked it — and a click replaces it. What a worklist opens on: the stock
|
|
67
|
+
* that is out first, the oldest order first.
|
|
68
|
+
*
|
|
69
|
+
* Read once, on mount, like {@link initialFiltering}; a later value is
|
|
70
|
+
* ignored.
|
|
71
|
+
*/
|
|
72
|
+
initialSorting?: EntitySorting<TEntity>;
|
|
63
73
|
}
|
|
64
74
|
//# sourceMappingURL=use-data-loading.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-data-loading.types.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-data-loading/use-data-loading.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EACL,KAAK,cAAc,EACnB,MAAM,EACN,UAAU,EACV,aAAa,EACb,WAAW,EACZ,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEzC,MAAM,WAAW,mBAAmB,CAAC,CAAC,SAAS,MAAM;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,cAAc,GAAG,SAAS,CAAC;IAClC;;;;OAIG;IACH,SAAS,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IACtC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;CACvC;AAED,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,MAAM,IAAI;IACnD,GAAG,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC;CACtC,CAAC;AAEF,MAAM,WAAW,qBAAqB,CAAC,OAAO,SAAS,MAAM,EAAE,QAAQ;IACrE,EAAE,EAAE,MAAM,CAAC,MAAM,CACf,UAAU,CAAC,OAAO,CAAC,EACnB,cAAc,EACd,oBAAoB,GAAG,QAAQ,CAChC,CAAC;IACF,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC/B;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAClC;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC;;;;;;;;;OASG;IACH,gBAAgB,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;CACzC"}
|
|
1
|
+
{"version":3,"file":"use-data-loading.types.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-data-loading/use-data-loading.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EACL,KAAK,cAAc,EACnB,MAAM,EACN,UAAU,EACV,aAAa,EACb,WAAW,EACZ,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEzC,MAAM,WAAW,mBAAmB,CAAC,CAAC,SAAS,MAAM;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,cAAc,GAAG,SAAS,CAAC;IAClC;;;;OAIG;IACH,SAAS,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IACtC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;CACvC;AAED,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,MAAM,IAAI;IACnD,GAAG,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC;CACtC,CAAC;AAEF,MAAM,WAAW,qBAAqB,CAAC,OAAO,SAAS,MAAM,EAAE,QAAQ;IACrE,EAAE,EAAE,MAAM,CAAC,MAAM,CACf,UAAU,CAAC,OAAO,CAAC,EACnB,cAAc,EACd,oBAAoB,GAAG,QAAQ,CAChC,CAAC;IACF,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC/B;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAClC;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC;;;;;;;;;OASG;IACH,gBAAgB,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IACxC;;;;;;;;OAQG;IACH,cAAc,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;CACzC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/use-data-loading/use-data-loading.types.ts"],"sourcesContent":["import { EntityLoadRequestTag } from '@entifix/business';\nimport {\n type EntifixFailure,\n Entity,\n EntityPage,\n EntitySorting,\n FilterGroup,\n} from '@entifix/core';\nimport { Context, Effect } from 'effect';\n\nexport interface UseDataLoadingState<T extends Entity> {\n isLoading: boolean;\n items: Array<T>;\n totalItems: number;\n currentPage: number;\n pageSize: number;\n error: EntifixFailure | undefined;\n /**\n * The filtering and sorting currently *applied* — not a draft. The controls\n * commit through `onFilteringChange`/`onSortingChange`, so every value here\n * has already been requested from the server.\n */\n filtering: FilterGroup<T> | undefined;\n sorting: EntitySorting<T> | undefined;\n}\n\nexport type UseDataLoadingAction<T extends Entity> = {\n set: Partial<UseDataLoadingState<T>>;\n};\n\nexport interface UseDataLoadingOptions<TEntity extends Entity, TContext> {\n uc: Effect.Effect<\n EntityPage<TEntity>,\n EntifixFailure,\n EntityLoadRequestTag | TContext\n >;\n ctx: Context.Context<TContext>;\n /**\n * Page size for the first load; defaults to 10. Callers that need the whole\n * set rather than a page — a relation picker, say — raise this.\n */\n initialPageSize?: number;\n /**\n * The stable prefix that scopes this list in the shared query cache — pass\n * {@link entityQueryScope}`(Ctor)` so refetches, optimistic writes, and\n * reactive invalidation of the same entity all address one cache entry.\n * Omit it and the hook falls back to a per-instance id: correct in isolation\n * (no cross-list collisions) but unshared, so nothing outside this hook can\n * invalidate it.\n */\n queryKey?: ReadonlyArray<unknown>;\n /**\n * `false` holds the request back without unmounting the hook — what a picker's\n * browse dialog needs, since a list nobody has opened yet must not be fetched.\n * Defaults to `true`, so every existing caller loads on mount as before.\n */\n enabled?: boolean;\n /**\n * A standing restriction ANDed into every request and never shown in the filter\n * panel — \"these are the rows this caller may see at all\", as opposed to the\n * filter the user is editing. A link picker narrowing what may be assigned uses\n * it; so would a listing scoped to one tenant.\n */\n baseFiltering?: FilterGroup<TEntity>;\n /**\n * The filter the list *opens* with: applied to the first load and handed back\n * as `filtering`, so the filter panel shows it exactly as if the user had set\n * it — and the user can change or clear it. The opposite of\n * {@link baseFiltering}, a restriction the user never sees and cannot lift.\n * A worklist is this: \"orders awaiting dispatch\" is the default view of the\n * orders, not the only one.\n *\n * Read once, on mount, like {@link initialPageSize}; a later value is ignored.\n */\n initialFiltering?: FilterGroup<TEntity>;\n}\n"],"names":[],"mappings":"AA8BA,
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/use-data-loading/use-data-loading.types.ts"],"sourcesContent":["import { EntityLoadRequestTag } from '@entifix/business';\nimport {\n type EntifixFailure,\n Entity,\n EntityPage,\n EntitySorting,\n FilterGroup,\n} from '@entifix/core';\nimport { Context, Effect } from 'effect';\n\nexport interface UseDataLoadingState<T extends Entity> {\n isLoading: boolean;\n items: Array<T>;\n totalItems: number;\n currentPage: number;\n pageSize: number;\n error: EntifixFailure | undefined;\n /**\n * The filtering and sorting currently *applied* — not a draft. The controls\n * commit through `onFilteringChange`/`onSortingChange`, so every value here\n * has already been requested from the server.\n */\n filtering: FilterGroup<T> | undefined;\n sorting: EntitySorting<T> | undefined;\n}\n\nexport type UseDataLoadingAction<T extends Entity> = {\n set: Partial<UseDataLoadingState<T>>;\n};\n\nexport interface UseDataLoadingOptions<TEntity extends Entity, TContext> {\n uc: Effect.Effect<\n EntityPage<TEntity>,\n EntifixFailure,\n EntityLoadRequestTag | TContext\n >;\n ctx: Context.Context<TContext>;\n /**\n * Page size for the first load; defaults to 10. Callers that need the whole\n * set rather than a page — a relation picker, say — raise this.\n */\n initialPageSize?: number;\n /**\n * The stable prefix that scopes this list in the shared query cache — pass\n * {@link entityQueryScope}`(Ctor)` so refetches, optimistic writes, and\n * reactive invalidation of the same entity all address one cache entry.\n * Omit it and the hook falls back to a per-instance id: correct in isolation\n * (no cross-list collisions) but unshared, so nothing outside this hook can\n * invalidate it.\n */\n queryKey?: ReadonlyArray<unknown>;\n /**\n * `false` holds the request back without unmounting the hook — what a picker's\n * browse dialog needs, since a list nobody has opened yet must not be fetched.\n * Defaults to `true`, so every existing caller loads on mount as before.\n */\n enabled?: boolean;\n /**\n * A standing restriction ANDed into every request and never shown in the filter\n * panel — \"these are the rows this caller may see at all\", as opposed to the\n * filter the user is editing. A link picker narrowing what may be assigned uses\n * it; so would a listing scoped to one tenant.\n */\n baseFiltering?: FilterGroup<TEntity>;\n /**\n * The filter the list *opens* with: applied to the first load and handed back\n * as `filtering`, so the filter panel shows it exactly as if the user had set\n * it — and the user can change or clear it. The opposite of\n * {@link baseFiltering}, a restriction the user never sees and cannot lift.\n * A worklist is this: \"orders awaiting dispatch\" is the default view of the\n * orders, not the only one.\n *\n * Read once, on mount, like {@link initialPageSize}; a later value is ignored.\n */\n initialFiltering?: FilterGroup<TEntity>;\n /**\n * The order the list *opens* in: sent with the first load and handed back as\n * `sorting`, so the header reads as sorted by it exactly as if the user had\n * clicked it — and a click replaces it. What a worklist opens on: the stock\n * that is out first, the oldest order first.\n *\n * Read once, on mount, like {@link initialFiltering}; a later value is\n * ignored.\n */\n initialSorting?: EntitySorting<TEntity>;\n}\n"],"names":[],"mappings":"AA8BA,WAuDC"}
|
|
@@ -21,5 +21,5 @@ import type { EntityLinkSourceConfig, UseEntityLinkSourceOptions } from './use-e
|
|
|
21
21
|
* an id but no instance — a draft restored from storage — because a picked target
|
|
22
22
|
* already knows its own name.
|
|
23
23
|
*/
|
|
24
|
-
export declare function useEntityLinkSource<TTarget extends Entity, TContext>(config: EntityLinkSourceConfig<TTarget, TContext>, { descriptor, selectedId, selectedEntity, }: UseEntityLinkSourceOptions<TTarget>): EntityLinkSource<TTarget>;
|
|
24
|
+
export declare function useEntityLinkSource<TTarget extends Entity, TContext>(config: EntityLinkSourceConfig<TTarget, TContext>, { descriptor, selectedId, selectedEntity, locale, }: UseEntityLinkSourceOptions<TTarget>): EntityLinkSource<TTarget>;
|
|
25
25
|
//# sourceMappingURL=use-entity-link-source.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-entity-link-source.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"use-entity-link-source.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.ts"],"names":[],"mappings":"AAGA,OAAO,EAIL,KAAK,MAAM,EAEX,KAAK,gBAAgB,EAItB,MAAM,eAAe,CAAC;AAWvB,OAAO,KAAK,EACV,sBAAsB,EACtB,0BAA0B,EAC3B,MAAM,mCAAmC,CAAC;AAgC3C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,SAAS,MAAM,EAAE,QAAQ,EAClE,MAAM,EAAE,sBAAsB,CAAC,OAAO,EAAE,QAAQ,CAAC,EACjD,EACE,UAAU,EACV,UAAU,EACV,cAAc,EACd,MAAwB,GACzB,EAAE,0BAA0B,CAAC,OAAO,CAAC,GACrC,gBAAgB,CAAC,OAAO,CAAC,CA8K3B"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
3
3
|
import { EntityIdTag, EntityLoadRequestTag } from '@entifix/business';
|
|
4
|
-
import { describeEntityColumns } from '@entifix/core';
|
|
4
|
+
import { defaultLocale, describeEntityColumns, localizedQueryProperty } from '@entifix/core';
|
|
5
5
|
import { useQuery } from '@tanstack/react-query';
|
|
6
6
|
import { Context, Effect } from 'effect';
|
|
7
7
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
@@ -11,20 +11,17 @@ import { useDataLoading } from '../use-data-loading/index.js';
|
|
|
11
11
|
const DEFAULT_QUICK_PAGE_SIZE = 10;
|
|
12
12
|
const DEFAULT_DEBOUNCE_MS = 250;
|
|
13
13
|
/**
|
|
14
|
-
* Fails loudly on a search property the service will reject
|
|
14
|
+
* Fails loudly on a search property the service will reject, and returns its
|
|
15
|
+
* descriptor.
|
|
15
16
|
*
|
|
16
17
|
* A member's `filterable` metadata is also the server-side filter allowlist, so a
|
|
17
18
|
* quick search over a member that lacks it answers `400` — as an empty
|
|
18
19
|
* suggestion list, which reads to the user as "there are no brands". Better to
|
|
19
|
-
* break the developer's build than to lie to them.
|
|
20
|
+
* break the developer's build than to lie to them. A `localized` member is
|
|
21
|
+
* searchable like any other: it is queried in the reader's locale.
|
|
20
22
|
*/ function assertSearchable(config, searchProperty) {
|
|
21
23
|
const target = describeEntityColumns(config.entityConstructor).find((column)=>column.name === searchProperty);
|
|
22
|
-
if ((target == null ? void 0 : target.
|
|
23
|
-
// Queried one locale at a time, and this hook holds no locale: the bare
|
|
24
|
-
// member is what the service would refuse.
|
|
25
|
-
throw new Error(`Cannot search ${config.entityConstructor.name} by "${searchProperty}": ` + 'the member is localized, and a quick search has no locale to search ' + 'it in. Point the relation at a string member with `linkSearchProperty`.');
|
|
26
|
-
}
|
|
27
|
-
if ((target == null ? void 0 : target.filterable) === true) return;
|
|
24
|
+
if ((target == null ? void 0 : target.filterable) === true) return target;
|
|
28
25
|
throw new Error(`Cannot search ${config.entityConstructor.name} by "${searchProperty}": ` + 'the member is not filterable, so the service would reject the query. ' + 'Declare it `filterable` on the target entity, or point the relation at ' + 'another member with `linkSearchProperty`.');
|
|
29
26
|
}
|
|
30
27
|
/**
|
|
@@ -47,12 +44,17 @@ const DEFAULT_DEBOUNCE_MS = 250;
|
|
|
47
44
|
* all until the dialog opens; and the label is only looked up when the caller has
|
|
48
45
|
* an id but no instance — a draft restored from storage — because a picked target
|
|
49
46
|
* already knows its own name.
|
|
50
|
-
*/ export function useEntityLinkSource(config, { descriptor, selectedId, selectedEntity }) {
|
|
47
|
+
*/ export function useEntityLinkSource(config, { descriptor, selectedId, selectedEntity, locale = defaultLocale() }) {
|
|
51
48
|
var _ref, _quickQuery_error;
|
|
52
49
|
var _quickQuery_data;
|
|
53
50
|
const { entityConstructor, loadUc, getUc, ctx, baseFiltering, quickPageSize = DEFAULT_QUICK_PAGE_SIZE, debounceMs = DEFAULT_DEBOUNCE_MS } = config;
|
|
54
|
-
const
|
|
55
|
-
|
|
51
|
+
const searchDescriptor = assertSearchable(config, descriptor.linkSearchProperty);
|
|
52
|
+
// A localized member is filtered and sorted one locale at a time — the
|
|
53
|
+
// reader's — and its options read in that locale.
|
|
54
|
+
const search = localizedQueryProperty(searchDescriptor, locale);
|
|
55
|
+
const searchProperty = search.property;
|
|
56
|
+
const labelDescriptor = describeEntityColumns(entityConstructor).find((column)=>column.name === descriptor.linkLabelProperty);
|
|
57
|
+
const isLocalizedSearch = searchDescriptor.type === 'localized';
|
|
56
58
|
// Callers rebuild `loadUc`/`getUc`/`ctx` inline on every render, so the latest
|
|
57
59
|
// are held in refs and kept out of the query keys — keying on their identity
|
|
58
60
|
// would refetch on every render. Same reason and same shape as `useDataLoading`.
|
|
@@ -98,10 +100,23 @@ const DEFAULT_DEBOUNCE_MS = 250;
|
|
|
98
100
|
if (filtering) request.filtering = [
|
|
99
101
|
filtering
|
|
100
102
|
];
|
|
103
|
+
// Suggestions in one locale's order, so they do not reshuffle as records
|
|
104
|
+
// are rewritten — the service applies no order of its own.
|
|
105
|
+
if (isLocalizedSearch) {
|
|
106
|
+
request.sorting = [
|
|
107
|
+
{
|
|
108
|
+
0: {
|
|
109
|
+
property: searchProperty,
|
|
110
|
+
type: 'asc'
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
];
|
|
114
|
+
}
|
|
101
115
|
return request;
|
|
102
116
|
}, [
|
|
103
117
|
baseFiltering,
|
|
104
118
|
debouncedTerm,
|
|
119
|
+
isLocalizedSearch,
|
|
105
120
|
quickPageSize,
|
|
106
121
|
searchProperty
|
|
107
122
|
]);
|
|
@@ -136,10 +151,15 @@ const DEFAULT_DEBOUNCE_MS = 250;
|
|
|
136
151
|
// an unnamed row must still be distinguishable from the row above it.
|
|
137
152
|
const labelOf = useCallback((target)=>{
|
|
138
153
|
var _target_id;
|
|
139
|
-
const
|
|
140
|
-
|
|
154
|
+
const raw = target[descriptor.linkLabelProperty];
|
|
155
|
+
// A label member the target does not declare reads as nothing, so the
|
|
156
|
+
// option falls back to its id.
|
|
157
|
+
const label = labelDescriptor && localizedQueryProperty(labelDescriptor, locale).read(raw);
|
|
158
|
+
return label ? label : String((_target_id = target.id) != null ? _target_id : '');
|
|
141
159
|
}, [
|
|
142
|
-
descriptor.linkLabelProperty
|
|
160
|
+
descriptor.linkLabelProperty,
|
|
161
|
+
labelDescriptor,
|
|
162
|
+
locale
|
|
143
163
|
]);
|
|
144
164
|
const resolved = selectedEntity != null ? selectedEntity : labelQuery.data;
|
|
145
165
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.ts"],"sourcesContent":["'use client';\n\nimport { EntityIdTag, EntityLoadRequestTag } from '@entifix/business';\nimport {\n describeEntityColumns,\n type EntifixFailure,\n type Entity,\n type EntityLinkSource,\n type EntityLoadRequest,\n type EntityPage,\n} from '@entifix/core';\nimport { useQuery } from '@tanstack/react-query';\nimport { Context, Effect } from 'effect';\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\n\nimport { combineFilterGroups } from '../../query/combine-filtering.js';\nimport {\n entityQueryKey,\n entityQueryScope,\n} from '../../query/entity-query-key.js';\nimport { useDataLoading } from '../use-data-loading/index.js';\nimport type {\n EntityLinkSourceConfig,\n UseEntityLinkSourceOptions,\n} from './use-entity-link-source.types.js';\n\nconst DEFAULT_QUICK_PAGE_SIZE = 10;\nconst DEFAULT_DEBOUNCE_MS = 250;\n\n/**\n * Fails loudly on a search property the service will reject.\n *\n * A member's `filterable` metadata is also the server-side filter allowlist, so a\n * quick search over a member that lacks it answers `400` — as an empty\n * suggestion list, which reads to the user as \"there are no brands\". Better to\n * break the developer's build than to lie to them.\n */\nfunction assertSearchable<TTarget extends Entity>(\n config: EntityLinkSourceConfig<TTarget, unknown>,\n searchProperty: string,\n): void {\n const target = describeEntityColumns(config.entityConstructor).find(\n column => column.name === searchProperty,\n );\n if (target?.type === 'localized') {\n // Queried one locale at a time, and this hook holds no locale: the bare\n // member is what the service would refuse.\n throw new Error(\n `Cannot search ${config.entityConstructor.name} by \"${searchProperty}\": ` +\n 'the member is localized, and a quick search has no locale to search ' +\n 'it in. Point the relation at a string member with `linkSearchProperty`.',\n );\n }\n if (target?.filterable === true) return;\n\n throw new Error(\n `Cannot search ${config.entityConstructor.name} by \"${searchProperty}\": ` +\n 'the member is not filterable, so the service would reject the query. ' +\n 'Declare it `filterable` on the target entity, or point the relation at ' +\n 'another member with `linkSearchProperty`.',\n );\n}\n\n/**\n * Produces the {@link EntityLinkSource} a link editor runs on: the quick\n * type-ahead, the browse list, and the label of whatever key is currently held.\n *\n * This is the data half of the link mechanism. It exists as a hook — rather than\n * inside the input — because `@entifix/react-controls` may not import this package\n * (both are T3, and the tier register declares no sideways edge between them), so\n * the two meet at the framework-free port declared in `@entifix/core`. A form\n * wrapper calls it once per relation and hands the results to `EntityForm`'s\n * `linkSources`.\n *\n * What may be picked is never decided here: the caller's `loadUc` (and optional\n * `baseFiltering`) is what restricts the set, so a domain rule about assignable\n * targets lives in a use-case, not in the UI.\n *\n * Three requests, each held back until it means something: the quick search runs\n * a small page and re-runs on a debounced term; the browse list does not fetch at\n * all until the dialog opens; and the label is only looked up when the caller has\n * an id but no instance — a draft restored from storage — because a picked target\n * already knows its own name.\n */\nexport function useEntityLinkSource<TTarget extends Entity, TContext>(\n config: EntityLinkSourceConfig<TTarget, TContext>,\n {\n descriptor,\n selectedId,\n selectedEntity,\n }: UseEntityLinkSourceOptions<TTarget>,\n): EntityLinkSource<TTarget> {\n const {\n entityConstructor,\n loadUc,\n getUc,\n ctx,\n baseFiltering,\n quickPageSize = DEFAULT_QUICK_PAGE_SIZE,\n debounceMs = DEFAULT_DEBOUNCE_MS,\n } = config;\n\n const searchProperty = descriptor.linkSearchProperty;\n assertSearchable(\n config as unknown as EntityLinkSourceConfig<TTarget, unknown>,\n searchProperty,\n );\n\n // Callers rebuild `loadUc`/`getUc`/`ctx` inline on every render, so the latest\n // are held in refs and kept out of the query keys — keying on their identity\n // would refetch on every render. Same reason and same shape as `useDataLoading`.\n const effects = useRef({ loadUc, getUc, ctx });\n useEffect(() => {\n effects.current = { loadUc, getUc, ctx };\n });\n\n const [term, setTerm] = useState('');\n const [debouncedTerm, setDebouncedTerm] = useState('');\n const [isOpen, setOpen] = useState(false);\n\n // Typing is not a request per keystroke: the term settles first, and only the\n // settled value reaches the service.\n useEffect(() => {\n const timer = setTimeout(() => setDebouncedTerm(term), debounceMs);\n return () => clearTimeout(timer);\n }, [term, debounceMs]);\n\n const quickRequest = useMemo<EntityLoadRequest<TTarget>>(() => {\n const filtering = combineFilterGroups<TTarget>(\n baseFiltering,\n debouncedTerm === ''\n ? undefined\n : {\n operator: 'and',\n values: [\n {\n property: searchProperty as keyof TTarget,\n operator: 'like',\n value: debouncedTerm,\n },\n ],\n },\n );\n\n const request: EntityLoadRequest<TTarget> = {\n page: 1,\n pageSize: quickPageSize,\n };\n if (filtering) request.filtering = [filtering];\n return request;\n }, [baseFiltering, debouncedTerm, quickPageSize, searchProperty]);\n\n const quickQuery = useQuery<EntityPage<TTarget>, EntifixFailure>({\n queryKey: [\n ...entityQueryKey(entityConstructor, quickRequest),\n 'link-quick',\n ],\n queryFn: () =>\n Effect.runPromise(\n Effect.provide(\n effects.current.loadUc,\n effects.current.ctx.pipe(\n Context.add(\n EntityLoadRequestTag,\n // The tag carries no generic, so the entity-typed request crosses\n // it — the same cast `loadUCFactory` makes reading it back.\n quickRequest as unknown as EntityLoadRequest,\n ),\n ),\n ),\n ),\n });\n\n const browse = useDataLoading<TTarget, TContext>({\n uc: loadUc,\n ctx,\n queryKey: entityQueryScope(entityConstructor),\n baseFiltering,\n enabled: isOpen,\n });\n\n const needsLabel =\n selectedEntity === undefined && selectedId != null && getUc !== undefined;\n\n const labelQuery = useQuery<TTarget, EntifixFailure>({\n queryKey: [\n ...entityQueryScope(entityConstructor),\n 'link-label',\n String(selectedId),\n ],\n enabled: needsLabel,\n queryFn: () =>\n Effect.runPromise(\n Effect.provide(\n // Guarded by `enabled`: the query only runs when both are present.\n effects.current.getUc as NonNullable<typeof getUc>,\n effects.current.ctx.pipe(Context.add(EntityIdTag, selectedId)),\n ),\n ),\n });\n\n // A blank label is as useless as a missing one, so both fall back to the key —\n // an unnamed row must still be distinguishable from the row above it.\n const labelOf = useCallback(\n (target: TTarget) => {\n const label = (target as Record<string, unknown>)[\n descriptor.linkLabelProperty\n ];\n return label == null || label === ''\n ? String(target.id ?? '')\n : String(label);\n },\n [descriptor.linkLabelProperty],\n );\n\n const resolved = selectedEntity ?? labelQuery.data;\n\n return {\n entityConstructor,\n labelOf,\n selected: {\n label: resolved === undefined ? undefined : labelOf(resolved),\n // The whole target, not only its name. The lookup above already fetched\n // it, and a form restoring a draft has nothing else to rebuild an\n // `embedded` relation from — the draft kept the id and lost the instance.\n entity: resolved,\n isLoading: needsLabel && labelQuery.isFetching,\n },\n quick: {\n term,\n setTerm,\n options: quickQuery.data?.items ?? [],\n isLoading: quickQuery.isFetching,\n error: quickQuery.error ?? undefined,\n },\n browse: {\n ...browse,\n isOpen,\n open: () => setOpen(true),\n close: () => setOpen(false),\n },\n };\n}\n"],"names":["EntityIdTag","EntityLoadRequestTag","describeEntityColumns","useQuery","Context","Effect","useCallback","useEffect","useMemo","useRef","useState","combineFilterGroups","entityQueryKey","entityQueryScope","useDataLoading","DEFAULT_QUICK_PAGE_SIZE","DEFAULT_DEBOUNCE_MS","assertSearchable","config","searchProperty","target","entityConstructor","find","column","name","type","Error","filterable","useEntityLinkSource","descriptor","selectedId","selectedEntity","quickQuery","loadUc","getUc","ctx","baseFiltering","quickPageSize","debounceMs","linkSearchProperty","effects","current","term","setTerm","debouncedTerm","setDebouncedTerm","isOpen","setOpen","timer","setTimeout","clearTimeout","quickRequest","filtering","undefined","operator","values","property","value","request","page","pageSize","queryKey","queryFn","runPromise","provide","pipe","add","browse","uc","enabled","needsLabel","labelQuery","String","labelOf","label","linkLabelProperty","id","resolved","data","selected","entity","isLoading","isFetching","quick","options","items","error","open","close"],"mappings":"AAAA;;AAEA,SAASA,WAAW,EAAEC,oBAAoB,QAAQ,oBAAoB;AACtE,SACEC,qBAAqB,QAMhB,gBAAgB;AACvB,SAASC,QAAQ,QAAQ,wBAAwB;AACjD,SAASC,OAAO,EAAEC,MAAM,QAAQ,SAAS;AACzC,SAASC,WAAW,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAQ;AAE1E,SAASC,mBAAmB,QAAQ,mCAAmC;AACvE,SACEC,cAAc,EACdC,gBAAgB,QACX,kCAAkC;AACzC,SAASC,cAAc,QAAQ,+BAA+B;AAM9D,MAAMC,0BAA0B;AAChC,MAAMC,sBAAsB;AAE5B;;;;;;;CAOC,GACD,SAASC,iBACPC,MAAgD,EA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MAAM2C,UAAUnE,YACd,CAACc;YAKYA;QAJX,MAAMsD,QAAQ,AAACtD,MAAkC,CAC/CS,WAAW8C,iBAAiB,CAC7B;QACD,OAAOD,SAAS,QAAQA,UAAU,KAC9BF,QAAOpD,aAAAA,OAAOwD,EAAE,YAATxD,aAAa,MACpBoD,OAAOE;IACb,GACA;QAAC7C,WAAW8C,iBAAiB;KAAC;IAGhC,MAAME,WAAW9C,yBAAAA,iBAAkBwC,WAAWO,IAAI;IAElD,OAAO;QACLzD;QACAoD;QACAM,UAAU;YACRL,OAAOG,aAAaxB,YAAYA,YAAYoB,QAAQI;YACpD,wEAAwE;YACxE,kEAAkE;YAClE,0EAA0E;YAC1EG,QAAQH;YACRI,WAAWX,cAAcC,WAAWW,UAAU;QAChD;QACAC,OAAO;YACLzC;YACAC;YACAyC,OAAO,WAAEpD,mBAAAA,WAAW8C,IAAI,qBAAf9C,iBAAiBqD,KAAK,mBAAI,EAAE;YACrCJ,WAAWjD,WAAWkD,UAAU;YAChCI,KAAK,GAAEtD,oBAAAA,WAAWsD,KAAK,YAAhBtD,oBAAoBqB;QAC7B;QACAc,QAAQ,aACHA;YACHrB;YACAyC,MAAM,IAAMxC,QAAQ;YACpByC,OAAO,IAAMzC,QAAQ;;IAEzB;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.ts"],"sourcesContent":["'use client';\n\nimport { EntityIdTag, EntityLoadRequestTag } from '@entifix/business';\nimport {\n defaultLocale,\n describeEntityColumns,\n type EntifixFailure,\n type Entity,\n type EntityFieldDescriptor,\n type EntityLinkSource,\n type EntityLoadRequest,\n type EntityPage,\n localizedQueryProperty,\n} from '@entifix/core';\nimport { useQuery } from '@tanstack/react-query';\nimport { Context, Effect } from 'effect';\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\n\nimport { combineFilterGroups } from '../../query/combine-filtering.js';\nimport {\n entityQueryKey,\n entityQueryScope,\n} from '../../query/entity-query-key.js';\nimport { useDataLoading } from '../use-data-loading/index.js';\nimport type {\n EntityLinkSourceConfig,\n UseEntityLinkSourceOptions,\n} from './use-entity-link-source.types.js';\n\nconst DEFAULT_QUICK_PAGE_SIZE = 10;\nconst DEFAULT_DEBOUNCE_MS = 250;\n\n/**\n * Fails loudly on a search property the service will reject, and returns its\n * descriptor.\n *\n * A member's `filterable` metadata is also the server-side filter allowlist, so a\n * quick search over a member that lacks it answers `400` — as an empty\n * suggestion list, which reads to the user as \"there are no brands\". Better to\n * break the developer's build than to lie to them. A `localized` member is\n * searchable like any other: it is queried in the reader's locale.\n */\nfunction assertSearchable<TTarget extends Entity>(\n config: EntityLinkSourceConfig<TTarget, unknown>,\n searchProperty: string,\n): EntityFieldDescriptor {\n const target = describeEntityColumns(config.entityConstructor).find(\n column => column.name === searchProperty,\n );\n if (target?.filterable === true) return target;\n\n throw new Error(\n `Cannot search ${config.entityConstructor.name} by \"${searchProperty}\": ` +\n 'the member is not filterable, so the service would reject the query. ' +\n 'Declare it `filterable` on the target entity, or point the relation at ' +\n 'another member with `linkSearchProperty`.',\n );\n}\n\n/**\n * Produces the {@link EntityLinkSource} a link editor runs on: the quick\n * type-ahead, the browse list, and the label of whatever key is currently held.\n *\n * This is the data half of the link mechanism. It exists as a hook — rather than\n * inside the input — because `@entifix/react-controls` may not import this package\n * (both are T3, and the tier register declares no sideways edge between them), so\n * the two meet at the framework-free port declared in `@entifix/core`. A form\n * wrapper calls it once per relation and hands the results to `EntityForm`'s\n * `linkSources`.\n *\n * What may be picked is never decided here: the caller's `loadUc` (and optional\n * `baseFiltering`) is what restricts the set, so a domain rule about assignable\n * targets lives in a use-case, not in the UI.\n *\n * Three requests, each held back until it means something: the quick search runs\n * a small page and re-runs on a debounced term; the browse list does not fetch at\n * all until the dialog opens; and the label is only looked up when the caller has\n * an id but no instance — a draft restored from storage — because a picked target\n * already knows its own name.\n */\nexport function useEntityLinkSource<TTarget extends Entity, TContext>(\n config: EntityLinkSourceConfig<TTarget, TContext>,\n {\n descriptor,\n selectedId,\n selectedEntity,\n locale = defaultLocale(),\n }: UseEntityLinkSourceOptions<TTarget>,\n): EntityLinkSource<TTarget> {\n const {\n entityConstructor,\n loadUc,\n getUc,\n ctx,\n baseFiltering,\n quickPageSize = DEFAULT_QUICK_PAGE_SIZE,\n debounceMs = DEFAULT_DEBOUNCE_MS,\n } = config;\n\n const searchDescriptor = assertSearchable(\n config as unknown as EntityLinkSourceConfig<TTarget, unknown>,\n descriptor.linkSearchProperty,\n );\n // A localized member is filtered and sorted one locale at a time — the\n // reader's — and its options read in that locale.\n const search = localizedQueryProperty(searchDescriptor, locale);\n const searchProperty = search.property;\n const labelDescriptor = describeEntityColumns(entityConstructor).find(\n column => column.name === descriptor.linkLabelProperty,\n );\n const isLocalizedSearch = searchDescriptor.type === 'localized';\n\n // Callers rebuild `loadUc`/`getUc`/`ctx` inline on every render, so the latest\n // are held in refs and kept out of the query keys — keying on their identity\n // would refetch on every render. Same reason and same shape as `useDataLoading`.\n const effects = useRef({ loadUc, getUc, ctx });\n useEffect(() => {\n effects.current = { loadUc, getUc, ctx };\n });\n\n const [term, setTerm] = useState('');\n const [debouncedTerm, setDebouncedTerm] = useState('');\n const [isOpen, setOpen] = useState(false);\n\n // Typing is not a request per keystroke: the term settles first, and only the\n // settled value reaches the service.\n useEffect(() => {\n const timer = setTimeout(() => setDebouncedTerm(term), debounceMs);\n return () => clearTimeout(timer);\n }, [term, debounceMs]);\n\n const quickRequest = useMemo<EntityLoadRequest<TTarget>>(() => {\n const filtering = combineFilterGroups<TTarget>(\n baseFiltering,\n debouncedTerm === ''\n ? undefined\n : {\n operator: 'and',\n values: [\n {\n property: searchProperty as keyof TTarget,\n operator: 'like',\n value: debouncedTerm,\n },\n ],\n },\n );\n\n const request: EntityLoadRequest<TTarget> = {\n page: 1,\n pageSize: quickPageSize,\n };\n if (filtering) request.filtering = [filtering];\n // Suggestions in one locale's order, so they do not reshuffle as records\n // are rewritten — the service applies no order of its own.\n if (isLocalizedSearch) {\n request.sorting = [\n { 0: { property: searchProperty as keyof TTarget, type: 'asc' } },\n ];\n }\n return request;\n }, [\n baseFiltering,\n debouncedTerm,\n isLocalizedSearch,\n quickPageSize,\n searchProperty,\n ]);\n\n const quickQuery = useQuery<EntityPage<TTarget>, EntifixFailure>({\n queryKey: [\n ...entityQueryKey(entityConstructor, quickRequest),\n 'link-quick',\n ],\n queryFn: () =>\n Effect.runPromise(\n Effect.provide(\n effects.current.loadUc,\n effects.current.ctx.pipe(\n Context.add(\n EntityLoadRequestTag,\n // The tag carries no generic, so the entity-typed request crosses\n // it — the same cast `loadUCFactory` makes reading it back.\n quickRequest as unknown as EntityLoadRequest,\n ),\n ),\n ),\n ),\n });\n\n const browse = useDataLoading<TTarget, TContext>({\n uc: loadUc,\n ctx,\n queryKey: entityQueryScope(entityConstructor),\n baseFiltering,\n enabled: isOpen,\n });\n\n const needsLabel =\n selectedEntity === undefined && selectedId != null && getUc !== undefined;\n\n const labelQuery = useQuery<TTarget, EntifixFailure>({\n queryKey: [\n ...entityQueryScope(entityConstructor),\n 'link-label',\n String(selectedId),\n ],\n enabled: needsLabel,\n queryFn: () =>\n Effect.runPromise(\n Effect.provide(\n // Guarded by `enabled`: the query only runs when both are present.\n effects.current.getUc as NonNullable<typeof getUc>,\n effects.current.ctx.pipe(Context.add(EntityIdTag, selectedId)),\n ),\n ),\n });\n\n // A blank label is as useless as a missing one, so both fall back to the key —\n // an unnamed row must still be distinguishable from the row above it.\n const labelOf = useCallback(\n (target: TTarget) => {\n const raw = (target as Record<string, unknown>)[\n descriptor.linkLabelProperty\n ];\n // A label member the target does not declare reads as nothing, so the\n // option falls back to its id.\n const label =\n labelDescriptor &&\n localizedQueryProperty(labelDescriptor, locale).read(raw);\n return label ? label : String(target.id ?? '');\n },\n [descriptor.linkLabelProperty, labelDescriptor, locale],\n );\n\n const resolved = selectedEntity ?? labelQuery.data;\n\n return {\n entityConstructor,\n labelOf,\n selected: {\n label: resolved === undefined ? undefined : labelOf(resolved),\n // The whole target, not only its name. The lookup above already fetched\n // it, and a form restoring a draft has nothing else to rebuild an\n // `embedded` relation from — the draft kept the id and lost the instance.\n entity: resolved,\n isLoading: needsLabel && labelQuery.isFetching,\n },\n quick: {\n term,\n setTerm,\n options: quickQuery.data?.items ?? [],\n isLoading: quickQuery.isFetching,\n error: quickQuery.error ?? undefined,\n },\n browse: {\n ...browse,\n isOpen,\n open: () => setOpen(true),\n close: () => setOpen(false),\n },\n };\n}\n"],"names":["EntityIdTag","EntityLoadRequestTag","defaultLocale","describeEntityColumns","localizedQueryProperty","useQuery","Context","Effect","useCallback","useEffect","useMemo","useRef","useState","combineFilterGroups","entityQueryKey","entityQueryScope","useDataLoading","DEFAULT_QUICK_PAGE_SIZE","DEFAULT_DEBOUNCE_MS","assertSearchable","config","searchProperty","target","entityConstructor","find","column","name","filterable","Error","useEntityLinkSource","descriptor","selectedId","selectedEntity","locale","quickQuery","loadUc","getUc","ctx","baseFiltering","quickPageSize","debounceMs","searchDescriptor","linkSearchProperty","search","property","labelDescriptor","linkLabelProperty","isLocalizedSearch","type","effects","current","term","setTerm","debouncedTerm","setDebouncedTerm","isOpen","setOpen","timer","setTimeout","clearTimeout","quickRequest","filtering","undefined","operator","values","value","request","page","pageSize","sorting","queryKey","queryFn","runPromise","provide","pipe","add","browse","uc","enabled","needsLabel","labelQuery","String","labelOf","raw","label","read","id","resolved","data","selected","entity","isLoading","isFetching","quick","options","items","error","open","close"],"mappings":"AAAA;;AAEA,SAASA,WAAW,EAAEC,oBAAoB,QAAQ,oBAAoB;AACtE,SACEC,aAAa,EACbC,qBAAqB,EAOrBC,sBAAsB,QACjB,gBAAgB;AACvB,SAASC,QAAQ,QAAQ,wBAAwB;AACjD,SAASC,OAAO,EAAEC,MAAM,QAAQ,SAAS;AACzC,SAASC,WAAW,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAQ;AAE1E,SAASC,mBAAmB,QAAQ,mCAAmC;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0C,MAAM,IAAMzC,QAAQ;YACpB0C,OAAO,IAAM1C,QAAQ;;IAEzB;AACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EntityIdTag, EntityLoadRequestTag } from '@entifix/business';
|
|
2
|
-
import type { EntifixFailure, Entity, EntityConstructor, EntityFieldDescriptor, EntityId, EntityPage, FilterGroup } from '@entifix/core';
|
|
2
|
+
import type { EntifixFailure, Entity, EntityConstructor, EntityFieldDescriptor, EntityId, EntityPage, FilterGroup, Locale } from '@entifix/core';
|
|
3
3
|
import type { Context, Effect } from 'effect';
|
|
4
4
|
/**
|
|
5
5
|
* Where a picker's targets come from, and what may be picked.
|
|
@@ -45,5 +45,11 @@ export interface UseEntityLinkSourceOptions<TTarget extends Entity> {
|
|
|
45
45
|
* spares the label request entirely.
|
|
46
46
|
*/
|
|
47
47
|
selectedEntity?: TTarget;
|
|
48
|
+
/**
|
|
49
|
+
* The reader's locale. A `localized` search member is filtered and sorted
|
|
50
|
+
* in it (`name.es`), and a `localized` label reads in it. Defaults to the
|
|
51
|
+
* host's default locale.
|
|
52
|
+
*/
|
|
53
|
+
locale?: Locale;
|
|
48
54
|
}
|
|
49
55
|
//# sourceMappingURL=use-entity-link-source.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-entity-link-source.types.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC3E,OAAO,KAAK,EACV,cAAc,EACd,MAAM,EACN,iBAAiB,EACjB,qBAAqB,EACrB,QAAQ,EACR,UAAU,EACV,WAAW,
|
|
1
|
+
{"version":3,"file":"use-entity-link-source.types.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC3E,OAAO,KAAK,EACV,cAAc,EACd,MAAM,EACN,iBAAiB,EACjB,qBAAqB,EACrB,QAAQ,EACR,UAAU,EACV,WAAW,EACX,MAAM,EACP,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,WAAW,sBAAsB,CAAC,OAAO,SAAS,MAAM,EAAE,QAAQ;IACtE,iBAAiB,EAAE,iBAAiB,CAAC,OAAO,CAAC,CAAC;IAC9C,4EAA4E;IAC5E,MAAM,EAAE,MAAM,CAAC,MAAM,CACnB,UAAU,CAAC,OAAO,CAAC,EACnB,cAAc,EACd,oBAAoB,GAAG,QAAQ,CAChC,CAAC;IACF;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,cAAc,EAAE,WAAW,GAAG,QAAQ,CAAC,CAAC;IACvE,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC/B;;;OAGG;IACH,aAAa,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,sEAAsE;IACtE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,0EAA0E;IAC1E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,0BAA0B,CAAC,OAAO,SAAS,MAAM;IAChE;;;OAGG;IACH,UAAU,EAAE,qBAAqB,CAAC;IAClC,iDAAiD;IACjD,UAAU,CAAC,EAAE,QAAQ,CAAC;IACtB;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.types.ts"],"sourcesContent":["import type { EntityIdTag, EntityLoadRequestTag } from '@entifix/business';\nimport type {\n EntifixFailure,\n Entity,\n EntityConstructor,\n EntityFieldDescriptor,\n EntityId,\n EntityPage,\n FilterGroup,\n} from '@entifix/core';\nimport type { Context, Effect } from 'effect';\n\n/**\n * Where a picker's targets come from, and what may be picked.\n *\n * The use-cases are the parametrization seam: a domain that must restrict what is\n * assignable supplies its own list use-case (only active brands, only leaf\n * categories) instead of the generic `loadUCFactory`, and the picker never learns\n * the rule. {@link baseFiltering} is the lighter version of the same idea for a\n * restriction that needs no use-case of its own.\n */\nexport interface EntityLinkSourceConfig<TTarget extends Entity, TContext> {\n entityConstructor: EntityConstructor<TTarget>;\n /** Lists the targets. Supplies {@link EntityLoadRequestTag} per request. */\n loadUc: Effect.Effect<\n EntityPage<TTarget>,\n EntifixFailure,\n EntityLoadRequestTag | TContext\n >;\n /**\n * Loads one target by id — how a draft restored from storage turns its held\n * foreign key back into a name. Omit it and the input falls back to showing\n * the bare key.\n */\n getUc?: Effect.Effect<TTarget, EntifixFailure, EntityIdTag | TContext>;\n ctx: Context.Context<TContext>;\n /**\n * ANDed into every quick and browse request, so a caller can narrow the\n * selectable set without writing a use-case.\n */\n baseFiltering?: FilterGroup<TTarget>;\n /** How many suggestions the quick search asks for. Defaults to 10. */\n quickPageSize?: number;\n /** Milliseconds of quiet before a typed term is sent. Defaults to 250. */\n debounceMs?: number;\n}\n\nexport interface UseEntityLinkSourceOptions<TTarget extends Entity> {\n /**\n * The relation being edited. Its `linkSearchProperty` is what the quick search\n * filters on and its `linkLabelProperty` is what a target reads as.\n */\n descriptor: EntityFieldDescriptor;\n /** The foreign key the draft currently holds. */\n selectedId?: EntityId;\n /**\n * The instance behind {@link selectedId} when the caller already has it — the\n * one the user just picked, or the one a loaded record carried. Present, it\n * spares the label request entirely.\n */\n selectedEntity?: TTarget;\n}\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/use-entity-link-source/use-entity-link-source.types.ts"],"sourcesContent":["import type { EntityIdTag, EntityLoadRequestTag } from '@entifix/business';\nimport type {\n EntifixFailure,\n Entity,\n EntityConstructor,\n EntityFieldDescriptor,\n EntityId,\n EntityPage,\n FilterGroup,\n Locale,\n} from '@entifix/core';\nimport type { Context, Effect } from 'effect';\n\n/**\n * Where a picker's targets come from, and what may be picked.\n *\n * The use-cases are the parametrization seam: a domain that must restrict what is\n * assignable supplies its own list use-case (only active brands, only leaf\n * categories) instead of the generic `loadUCFactory`, and the picker never learns\n * the rule. {@link baseFiltering} is the lighter version of the same idea for a\n * restriction that needs no use-case of its own.\n */\nexport interface EntityLinkSourceConfig<TTarget extends Entity, TContext> {\n entityConstructor: EntityConstructor<TTarget>;\n /** Lists the targets. Supplies {@link EntityLoadRequestTag} per request. */\n loadUc: Effect.Effect<\n EntityPage<TTarget>,\n EntifixFailure,\n EntityLoadRequestTag | TContext\n >;\n /**\n * Loads one target by id — how a draft restored from storage turns its held\n * foreign key back into a name. Omit it and the input falls back to showing\n * the bare key.\n */\n getUc?: Effect.Effect<TTarget, EntifixFailure, EntityIdTag | TContext>;\n ctx: Context.Context<TContext>;\n /**\n * ANDed into every quick and browse request, so a caller can narrow the\n * selectable set without writing a use-case.\n */\n baseFiltering?: FilterGroup<TTarget>;\n /** How many suggestions the quick search asks for. Defaults to 10. */\n quickPageSize?: number;\n /** Milliseconds of quiet before a typed term is sent. Defaults to 250. */\n debounceMs?: number;\n}\n\nexport interface UseEntityLinkSourceOptions<TTarget extends Entity> {\n /**\n * The relation being edited. Its `linkSearchProperty` is what the quick search\n * filters on and its `linkLabelProperty` is what a target reads as.\n */\n descriptor: EntityFieldDescriptor;\n /** The foreign key the draft currently holds. */\n selectedId?: EntityId;\n /**\n * The instance behind {@link selectedId} when the caller already has it — the\n * one the user just picked, or the one a loaded record carried. Present, it\n * spares the label request entirely.\n */\n selectedEntity?: TTarget;\n /**\n * The reader's locale. A `localized` search member is filtered and sorted\n * in it (`name.es`), and a `localized` label reads in it. Defaults to the\n * host's default locale.\n */\n locale?: Locale;\n}\n"],"names":[],"mappings":"AAgDA,WAoBC"}
|
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"version": "0.
|
|
24
|
+
"version": "0.22.1",
|
|
25
25
|
"description": "React hooks that bind entities to data: TanStack Query and Form over the entity adapters, drafts, mutations and live invalidation.",
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"repository": {
|
|
@@ -65,16 +65,16 @@
|
|
|
65
65
|
"LICENSE"
|
|
66
66
|
],
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@entifix/business": "0.
|
|
69
|
-
"@entifix/core": "0.
|
|
70
|
-
"@entifix/transactions": "0.
|
|
68
|
+
"@entifix/business": "0.22.1",
|
|
69
|
+
"@entifix/core": "0.22.1",
|
|
70
|
+
"@entifix/transactions": "0.22.1"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
73
|
"@tanstack/react-form": "1.33.5",
|
|
74
74
|
"@tanstack/react-query": "^5.107.1",
|
|
75
75
|
"effect": "4.0.7",
|
|
76
76
|
"react": "19.3.1",
|
|
77
|
-
"@entifix/testing-unit": "0.
|
|
77
|
+
"@entifix/testing-unit": "0.22.1"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|
|
80
80
|
"@tanstack/react-form": ">=1.33 <2",
|