@izumisy-tailor/tailor-data-viewer 0.2.3 → 0.2.5
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/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createContext, useContext, type ReactNode } from "react";
|
|
2
2
|
import type { UseCollectionParamsReturn } from "../types";
|
|
3
3
|
|
|
4
|
-
const CollectionParamsContext = createContext<UseCollectionParamsReturn
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const CollectionParamsContext = createContext<UseCollectionParamsReturn<
|
|
5
|
+
string,
|
|
6
|
+
unknown
|
|
7
|
+
> | null>(null);
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Provider that shares collection query parameters via React Context.
|
|
@@ -23,7 +24,7 @@ export function CollectionParamsProvider({
|
|
|
23
24
|
value,
|
|
24
25
|
children,
|
|
25
26
|
}: {
|
|
26
|
-
value: UseCollectionParamsReturn
|
|
27
|
+
value: UseCollectionParamsReturn<string, unknown>;
|
|
27
28
|
children: ReactNode;
|
|
28
29
|
}) {
|
|
29
30
|
return (
|
|
@@ -10,6 +10,8 @@ import type {
|
|
|
10
10
|
SortState,
|
|
11
11
|
UseCollectionParamsOptions,
|
|
12
12
|
UseCollectionParamsReturn,
|
|
13
|
+
ExtractOrderField,
|
|
14
|
+
MatchingTableName,
|
|
13
15
|
} from "../types";
|
|
14
16
|
import { fieldTypeToFilterConfig } from "../types";
|
|
15
17
|
import type { FieldName } from "../types";
|
|
@@ -62,6 +64,41 @@ export function useCollectionParams(
|
|
|
62
64
|
options?: UseCollectionParamsOptions,
|
|
63
65
|
): UseCollectionParamsReturn;
|
|
64
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Hook for managing collection query parameters (filters, sort, pagination)
|
|
69
|
+
* with an explicit `TVariables` type for the output `variables`.
|
|
70
|
+
*
|
|
71
|
+
* The `order[].field` union is extracted from `TVariables` and used as
|
|
72
|
+
* `TFieldName` for `addFilter`, `setSort`, `initialSort`, etc.
|
|
73
|
+
* When `metadata` is provided, `tableName` is constrained to tables
|
|
74
|
+
* whose fields are a superset of the extracted field names.
|
|
75
|
+
*
|
|
76
|
+
* @typeParam TVariables - The exact type for the output `variables` property.
|
|
77
|
+
* Typically `VariablesOf<typeof YOUR_QUERY>` from gql-tada.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```tsx
|
|
81
|
+
* import type { VariablesOf } from "gql.tada";
|
|
82
|
+
*
|
|
83
|
+
* const params = useCollectionParams<VariablesOf<typeof GET_ORDERS>>({
|
|
84
|
+
* metadata: tableMetadata,
|
|
85
|
+
* tableName: "order", // ← constrained to tables matching order[].field
|
|
86
|
+
* pageSize: 20,
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* params.addFilter("status", "ACTIVE"); // ← field name auto-completed
|
|
90
|
+
* const [result] = useQuery({ query: GET_ORDERS, variables: params.variables });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function useCollectionParams<TVariables>(
|
|
94
|
+
options?: UseCollectionParamsOptions<ExtractOrderField<TVariables>> & {
|
|
95
|
+
metadata?: TableMetadataMap;
|
|
96
|
+
tableName?: TableMetadataMap extends never
|
|
97
|
+
? string
|
|
98
|
+
: MatchingTableName<TableMetadataMap, ExtractOrderField<TVariables>>;
|
|
99
|
+
},
|
|
100
|
+
): UseCollectionParamsReturn<ExtractOrderField<TVariables>, TVariables>;
|
|
101
|
+
|
|
65
102
|
// -----------------------------------------------------------------------------
|
|
66
103
|
// Implementation
|
|
67
104
|
// -----------------------------------------------------------------------------
|
package/src/component/index.ts
CHANGED
package/src/component/types.ts
CHANGED
|
@@ -110,10 +110,16 @@ export interface PageInfo {
|
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
112
|
* GraphQL query variables in Tailor Platform format.
|
|
113
|
+
*
|
|
114
|
+
* @typeParam TFieldName - Union of allowed field name strings (default: `string`).
|
|
115
|
+
* When metadata is provided to `useCollectionParams`, this
|
|
116
|
+
* narrows `order[].field` to match the table's field names,
|
|
117
|
+
* making the output directly compatible with gql-tada's
|
|
118
|
+
* `VariablesOf<>` types.
|
|
113
119
|
*/
|
|
114
|
-
export interface QueryVariables {
|
|
120
|
+
export interface QueryVariables<TFieldName extends string = string> {
|
|
115
121
|
query?: Record<string, unknown>;
|
|
116
|
-
order?: { field:
|
|
122
|
+
order?: { field: TFieldName; direction: "Asc" | "Desc" }[];
|
|
117
123
|
first: number;
|
|
118
124
|
after?: string | null;
|
|
119
125
|
}
|
|
@@ -284,10 +290,16 @@ export interface UseCollectionParamsOptions<
|
|
|
284
290
|
* `UseCollectionParamsReturn<string>` (bivariant method check).
|
|
285
291
|
*
|
|
286
292
|
* @typeParam TFieldName - Union of allowed field name strings (default: `string`).
|
|
293
|
+
* @typeParam TVariables - Type of the `variables` output (default: `QueryVariables<TFieldName>`).
|
|
294
|
+
* Pass `VariablesOf<typeof YOUR_QUERY>` (gql-tada) to get
|
|
295
|
+
* exact type compatibility with your GraphQL client.
|
|
287
296
|
*/
|
|
288
|
-
export interface UseCollectionParamsReturn<
|
|
297
|
+
export interface UseCollectionParamsReturn<
|
|
298
|
+
TFieldName extends string = string,
|
|
299
|
+
TVariables = QueryVariables<TFieldName>,
|
|
300
|
+
> {
|
|
289
301
|
/** Query variables in Tailor Platform format */
|
|
290
|
-
variables:
|
|
302
|
+
variables: TVariables;
|
|
291
303
|
|
|
292
304
|
// Filter operations
|
|
293
305
|
/** Current active filters */
|
|
@@ -345,7 +357,7 @@ export interface UseDataTableOptions<TRow extends Record<string, unknown>> {
|
|
|
345
357
|
/** Error */
|
|
346
358
|
error?: Error | null;
|
|
347
359
|
/** Collection params for sort integration */
|
|
348
|
-
collectionParams?: UseCollectionParamsReturn
|
|
360
|
+
collectionParams?: UseCollectionParamsReturn<string, unknown>;
|
|
349
361
|
}
|
|
350
362
|
|
|
351
363
|
/**
|
|
@@ -474,6 +486,47 @@ export type FieldName<
|
|
|
474
486
|
: never
|
|
475
487
|
: never;
|
|
476
488
|
|
|
489
|
+
/**
|
|
490
|
+
* Extract the `order[].field` union type from a GraphQL variables type.
|
|
491
|
+
*
|
|
492
|
+
* For gql-tada's `VariablesOf<typeof QUERY>`, this extracts the allowed
|
|
493
|
+
* field names from the `order` parameter.
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```ts
|
|
497
|
+
* type Fields = ExtractOrderField<VariablesOf<typeof GET_ORDERS>>;
|
|
498
|
+
* // → "name" | "amount" | "status" | "createdAt"
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
export type ExtractOrderField<T> = T extends {
|
|
502
|
+
order?: readonly (infer O | null | undefined)[] | null | undefined;
|
|
503
|
+
}
|
|
504
|
+
? O extends { field?: infer F | null }
|
|
505
|
+
? NonNullable<F> & string
|
|
506
|
+
: string
|
|
507
|
+
: string;
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Find table names in metadata whose fields are a superset of `TFieldName`.
|
|
511
|
+
*
|
|
512
|
+
* Used to constrain `tableName` so that it matches the `order[].field`
|
|
513
|
+
* union extracted from `VariablesOf<>`.
|
|
514
|
+
*
|
|
515
|
+
* @example
|
|
516
|
+
* ```ts
|
|
517
|
+
* type T = MatchingTableName<typeof tableMetadata, "name" | "email">;
|
|
518
|
+
* // → "buyerContact" (if buyerContact's fields include name and email)
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
521
|
+
export type MatchingTableName<
|
|
522
|
+
TMetadata extends TableMetadataMap,
|
|
523
|
+
TFieldName extends string,
|
|
524
|
+
> = {
|
|
525
|
+
[K in string & keyof TMetadata]: TFieldName extends FieldName<TMetadata, K>
|
|
526
|
+
? K
|
|
527
|
+
: never;
|
|
528
|
+
}[string & keyof TMetadata];
|
|
529
|
+
|
|
477
530
|
/**
|
|
478
531
|
* Options for metadata-based single field definition.
|
|
479
532
|
*/
|