@izumisy-tailor/tailor-data-viewer 0.2.11 → 0.2.13

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.11",
4
+ "version": "0.2.13",
5
5
  "type": "module",
6
6
  "description": "Flexible data viewer component for Tailor Platform",
7
7
  "files": [
@@ -3,7 +3,6 @@ import type { UseCollectionReturn } from "../types";
3
3
 
4
4
  const CollectionContext = createContext<UseCollectionReturn<
5
5
  string,
6
- unknown,
7
6
  unknown
8
7
  > | null>(null);
9
8
 
@@ -25,7 +24,7 @@ export function CollectionProvider({
25
24
  value,
26
25
  children,
27
26
  }: {
28
- value: UseCollectionReturn<string, unknown, unknown>;
27
+ value: UseCollectionReturn<string, unknown>;
29
28
  children: ReactNode;
30
29
  }) {
31
30
  return (
@@ -35,8 +34,6 @@ export function CollectionProvider({
35
34
  );
36
35
  }
37
36
 
38
-
39
-
40
37
  /**
41
38
  * Hook to access collection state from the nearest `Collection.Provider`.
42
39
  *
@@ -71,8 +68,6 @@ export function useCollectionContext<
71
68
  return ctx as UseCollectionReturn<TFieldName>;
72
69
  }
73
70
 
74
-
75
-
76
71
  /**
77
72
  * `Collection` namespace object providing the Provider component.
78
73
  *
@@ -86,5 +81,3 @@ export function useCollectionContext<
86
81
  export const Collection = {
87
82
  Provider: CollectionProvider,
88
83
  } as const;
89
-
90
-
@@ -8,20 +8,9 @@ import type {
8
8
  SortState,
9
9
  UseCollectionOptions,
10
10
  UseCollectionReturn,
11
- ExtractQueryVariables,
12
11
  ValidateCollectionQuery,
13
12
  } from "../types";
14
- import type { FieldName } from "../types";
15
-
16
- /**
17
- * Resolves query variables type: uses `ExtractQueryVariables<TQuery>` when
18
- * the query document carries a `__variablesType` brand (e.g. gql-tada),
19
- * otherwise falls back to `QueryVariables<TFieldName>`.
20
- */
21
- type ResolveVariables<TQuery, TFieldName extends string = string> =
22
- ExtractQueryVariables<TQuery> extends never
23
- ? QueryVariables<TFieldName>
24
- : ExtractQueryVariables<TQuery>;
13
+ import type { FieldName, OrderableFieldName } from "../types";
25
14
 
26
15
  // -----------------------------------------------------------------------------
27
16
  // Overload signatures
@@ -58,14 +47,17 @@ export function useCollection<
58
47
  > & {
59
48
  metadata: TMetadata;
60
49
  tableName: TTableName;
61
- query: ValidateCollectionQuery<TQuery, FieldName<TMetadata, TTableName>>;
50
+ query: ValidateCollectionQuery<
51
+ TQuery,
52
+ FieldName<TMetadata, TTableName>,
53
+ OrderableFieldName<TMetadata, TTableName>
54
+ >;
62
55
  },
63
56
  ): UseCollectionReturn<
64
57
  FieldName<TMetadata, TTableName>,
65
- ResolveVariables<TQuery, FieldName<TMetadata, TTableName>>,
66
58
  {
67
59
  query: TQuery;
68
- variables: ResolveVariables<TQuery, FieldName<TMetadata, TTableName>>;
60
+ variables: QueryVariables;
69
61
  },
70
62
  MetadataFilter<TMetadata, TTableName>
71
63
  >;
@@ -91,11 +83,7 @@ export function useCollection<TQuery>(
91
83
  metadata?: never;
92
84
  tableName?: never;
93
85
  },
94
- ): UseCollectionReturn<
95
- string,
96
- ResolveVariables<TQuery>,
97
- { query: TQuery; variables: ResolveVariables<TQuery> }
98
- >;
86
+ ): UseCollectionReturn<string, { query: TQuery; variables: QueryVariables }>;
99
87
 
100
88
  // -----------------------------------------------------------------------------
101
89
  // Implementation
@@ -106,11 +94,7 @@ export function useCollection(
106
94
  tableName?: string;
107
95
  query: unknown;
108
96
  },
109
- ): UseCollectionReturn<
110
- string,
111
- QueryVariables,
112
- { query: unknown; variables: QueryVariables }
113
- > {
97
+ ): UseCollectionReturn<string, { query: unknown; variables: QueryVariables }> {
114
98
  const { params = {}, query: queryDocument } = options;
115
99
  const {
116
100
  initialFilters = [],
@@ -28,6 +28,7 @@ export type {
28
28
  DataTableRowProps,
29
29
  DataTableCellProps,
30
30
  FieldName,
31
+ OrderableFieldName,
31
32
  ExtractOrderField,
32
33
  ExtractQueryVariables,
33
34
  ExtractQueryInputKeys,
@@ -186,15 +186,31 @@ export interface PageInfo {
186
186
  /**
187
187
  * GraphQL query variables in Tailor Platform format.
188
188
  *
189
- * @typeParam TFieldName - Union of allowed field name strings (default: `string`).
190
- * When metadata is provided to `useCollection`, this
191
- * narrows `order[].field` to match the table's field names,
192
- * making the output directly compatible with gql-tada's
193
- * `VariablesOf<>` types.
189
+ * Field-level type safety for `query` and `order` is not enforced here.
190
+ * Instead, `ValidateCollectionQuery` performs compile-time checks
191
+ * (e.g. `CheckQueryInput` for filter field names, `CheckOrderField`
192
+ * for orderable fields) when `useCollection` is called with metadata.
194
193
  */
195
- export interface QueryVariables<TFieldName extends string = string> {
194
+ export interface QueryVariables {
195
+ /**
196
+ * Filter object built at runtime by `useCollection`.
197
+ *
198
+ * Typed as `Record<string, unknown>` because the concrete shape depends on
199
+ * which filters the user applies at runtime (e.g. `{ name: { eq: "foo" } }`).
200
+ *
201
+ * Compile-time safety for field names is enforced separately by
202
+ * `ValidateCollectionQuery` (specifically `CheckQueryInput`), which verifies
203
+ * that metadata field names are a subset of the GraphQL `QueryInput` type's keys.
204
+ */
196
205
  query?: Record<string, unknown>;
197
- order?: { field: TFieldName; direction: "Asc" | "Desc" }[];
206
+ /**
207
+ * Sort order built at runtime by `useCollection`.
208
+ *
209
+ * `field` is typed as `string` here. Compile-time validation that
210
+ * field names are assignable to the GraphQL `OrderInput` enum is
211
+ * performed by `ValidateCollectionQuery` (specifically `CheckOrderField`).
212
+ */
213
+ order?: { field: string; direction: "Asc" | "Desc" }[];
198
214
  first: number;
199
215
  after?: string | null;
200
216
  }
@@ -369,16 +385,12 @@ export interface UseCollectionOptions<
369
385
  * `UseCollectionReturn<string>` (bivariant method check).
370
386
  *
371
387
  * @typeParam TFieldName - Union of allowed field name strings (default: `string`).
372
- * @typeParam TVariables - Type of the `variables` output (default: `QueryVariables<TFieldName>`).
373
- * Pass `VariablesOf<typeof YOUR_QUERY>` (gql-tada) to get
374
- * exact type compatibility with your GraphQL client.
375
388
  * @typeParam TQueryArgs - Type returned by `toQueryArgs()`. Contains both
376
389
  * `query` and `variables`.
377
390
  */
378
391
  export interface UseCollectionReturn<
379
392
  TFieldName extends string = string,
380
- TVariables = QueryVariables<TFieldName>,
381
- TQueryArgs = { query: unknown; variables: TVariables },
393
+ TQueryArgs = { query: unknown; variables: QueryVariables },
382
394
  TFilter = Filter<TFieldName>,
383
395
  > {
384
396
  /**
@@ -453,7 +465,7 @@ export interface UseDataTableOptions<TRow extends Record<string, unknown>> {
453
465
  /** Error */
454
466
  error?: Error | null;
455
467
  /** Collection state for sort/pagination integration */
456
- collection?: UseCollectionReturn<string, unknown, unknown>;
468
+ collection?: UseCollectionReturn<string, unknown>;
457
469
  }
458
470
 
459
471
  /**
@@ -582,6 +594,41 @@ export type FieldName<
582
594
  : never
583
595
  : never;
584
596
 
597
+ /**
598
+ * Field types that support ordering.
599
+ * Mirrors the runtime `fieldTypeToSortConfig` logic — types that return
600
+ * a `SortConfig` (not `undefined`) are considered orderable.
601
+ */
602
+ type OrderableFieldType =
603
+ | "string"
604
+ | "number"
605
+ | "boolean"
606
+ | "datetime"
607
+ | "date"
608
+ | "time"
609
+ | "enum";
610
+
611
+ /**
612
+ * Extract only orderable field names from table metadata.
613
+ *
614
+ * Fields whose `type` is not in `OrderableFieldType` (e.g. `uuid`, `array`,
615
+ * `nested`, `file`) are excluded. This allows `CheckOrderField` to compare
616
+ * only the fields that Tailor Platform actually exposes in the
617
+ * `OrderFieldEnum`, avoiding false positives for fields like `id`.
618
+ */
619
+ export type OrderableFieldName<
620
+ TMetadata extends TableMetadataMap,
621
+ TTableName extends keyof TMetadata,
622
+ > =
623
+ Extract<
624
+ TMetadata[TTableName]["fields"][number],
625
+ { readonly type: OrderableFieldType }
626
+ > extends { readonly name: infer N }
627
+ ? N extends string
628
+ ? N
629
+ : never
630
+ : never;
631
+
585
632
  /**
586
633
  * Extract the `order[].field` union type from a GraphQL variables type.
587
634
  *
@@ -726,6 +773,7 @@ type CheckQueryInput<
726
773
  export type ValidateCollectionQuery<
727
774
  TQuery,
728
775
  TFieldName extends string = string,
776
+ TOrderableFieldName extends string = TFieldName,
729
777
  > =
730
778
  ExtractQueryVariables<TQuery> extends never
731
779
  ? TQuery // No gql-tada type info → skip validation
@@ -733,7 +781,7 @@ export type ValidateCollectionQuery<
733
781
  ? TQuery & CheckFirstVariable<TQuery> // No metadata → only check $first
734
782
  : TQuery &
735
783
  CheckFirstVariable<TQuery> &
736
- CheckOrderField<TQuery, TFieldName> &
784
+ CheckOrderField<TQuery, TOrderableFieldName> &
737
785
  CheckQueryInput<TQuery, TFieldName>;
738
786
 
739
787
  /**