@izumisy-tailor/tailor-data-viewer 0.2.4 → 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,7 +1,7 @@
1
1
  {
2
2
  "name": "@izumisy-tailor/tailor-data-viewer",
3
3
  "private": false,
4
- "version": "0.2.4",
4
+ "version": "0.2.5",
5
5
  "type": "module",
6
6
  "description": "Flexible data viewer component for Tailor Platform",
7
7
  "files": [
@@ -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 | null>(
5
- null,
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
  // -----------------------------------------------------------------------------
@@ -27,6 +27,8 @@ export type {
27
27
  DataTableRowProps,
28
28
  DataTableCellProps,
29
29
  FieldName,
30
+ ExtractOrderField,
31
+ MatchingTableName,
30
32
  MetadataFieldOptions,
31
33
  MetadataFieldsOptions,
32
34
  ColumnSelectorProps,
@@ -290,10 +290,16 @@ export interface UseCollectionParamsOptions<
290
290
  * `UseCollectionParamsReturn<string>` (bivariant method check).
291
291
  *
292
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.
293
296
  */
294
- export interface UseCollectionParamsReturn<TFieldName extends string = string> {
297
+ export interface UseCollectionParamsReturn<
298
+ TFieldName extends string = string,
299
+ TVariables = QueryVariables<TFieldName>,
300
+ > {
295
301
  /** Query variables in Tailor Platform format */
296
- variables: QueryVariables<TFieldName>;
302
+ variables: TVariables;
297
303
 
298
304
  // Filter operations
299
305
  /** Current active filters */
@@ -351,7 +357,7 @@ export interface UseDataTableOptions<TRow extends Record<string, unknown>> {
351
357
  /** Error */
352
358
  error?: Error | null;
353
359
  /** Collection params for sort integration */
354
- collectionParams?: UseCollectionParamsReturn;
360
+ collectionParams?: UseCollectionParamsReturn<string, unknown>;
355
361
  }
356
362
 
357
363
  /**
@@ -480,6 +486,47 @@ export type FieldName<
480
486
  : never
481
487
  : never;
482
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
+
483
530
  /**
484
531
  * Options for metadata-based single field definition.
485
532
  */