@timefoldai/viz-platform 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -1
- package/dist/analytics/use-analytics-bridge.d.mts +6 -0
- package/dist/analytics/use-analytics-bridge.mjs +21 -0
- package/dist/assignability/assignability.d.mts +97 -0
- package/dist/assignability/assignability.mjs +51 -0
- package/dist/assignability/use-assignability.d.mts +32 -0
- package/dist/assignability/use-assignability.mjs +151 -0
- package/dist/config/credential-channel.d.mts +13 -0
- package/dist/config/credential-channel.mjs +33 -0
- package/dist/config/embedding-params.d.mts +30 -0
- package/dist/config/embedding-params.mjs +54 -0
- package/dist/config/security.d.mts +36 -0
- package/dist/config/security.mjs +64 -0
- package/dist/dataset-context/DataSetContext.d.mts +215 -0
- package/dist/dataset-context/DataSetContext.mjs +170 -0
- package/dist/dataset-context/DataSetContextProvider.d.mts +19 -0
- package/dist/dataset-context/DataSetContextProvider.mjs +627 -0
- package/dist/dataset-context/DataSetLoader.d.mts +68 -0
- package/dist/dataset-context/DataSetLoader.mjs +130 -0
- package/dist/dataset-context/data-set-key.d.mts +2 -0
- package/dist/dataset-context/data-set-key.mjs +14 -0
- package/dist/dataset-context/model-api-url.d.mts +13 -0
- package/dist/dataset-context/model-api-url.mjs +27 -0
- package/dist/dataset-context/use-data-set-load-state.d.mts +26 -0
- package/dist/dataset-context/use-data-set-load-state.mjs +29 -0
- package/dist/dataset-context/use-fetch-dataset-resource.d.mts +8 -0
- package/dist/dataset-context/use-fetch-dataset-resource.mjs +20 -0
- package/dist/dataset-context/use-fetch-waypoints.d.mts +8 -0
- package/dist/dataset-context/use-fetch-waypoints.mjs +20 -0
- package/dist/dataset-context/use-run-events.d.mts +34 -0
- package/dist/dataset-context/use-run-events.mjs +138 -0
- package/dist/demo-data/use-demo-data.d.mts +42 -0
- package/dist/demo-data/use-demo-data.mjs +187 -0
- package/dist/env.d.mts +2 -0
- package/dist/env.mjs +4 -0
- package/dist/feature-flags/feature-flags.d.mts +34 -0
- package/dist/feature-flags/feature-flags.mjs +51 -0
- package/dist/feature-flags/use-feature-flag.d.mts +11 -0
- package/dist/feature-flags/use-feature-flag.mjs +17 -0
- package/dist/http/describe-response.d.mts +9 -0
- package/dist/http/describe-response.mjs +13 -0
- package/dist/http/models-api-request.d.mts +3 -0
- package/dist/http/models-api-request.mjs +20 -0
- package/dist/iframe-bridge/iframe-bridge.d.mts +10 -0
- package/dist/iframe-bridge/iframe-bridge.mjs +24 -0
- package/dist/iframe-bridge/use-iframe-bridge.d.mts +32 -0
- package/dist/iframe-bridge/use-iframe-bridge.mjs +200 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.mjs +14 -0
- package/dist/solve-control/use-solve-control.d.mts +16 -0
- package/dist/solve-control/use-solve-control.mjs +46 -0
- package/dist/theme/prefers-dark.d.mts +11 -0
- package/dist/theme/prefers-dark.mjs +43 -0
- package/dist/view-channel/view-channel.d.mts +17 -0
- package/dist/view-channel/view-channel.mjs +67 -0
- package/dist/visualization-provider/VisualizationProvider.d.mts +20 -0
- package/dist/visualization-provider/VisualizationProvider.mjs +26 -0
- package/package.json +36 -5
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { DistanceUnitSystem } from "@timefoldai/viz-core";
|
|
2
|
+
import { Dispatch } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* A loadable item of the `GET {demoDataSetPath}` list: its `id` is both the picker's option id and its label.
|
|
5
|
+
* The list drops an item without an `id`, because nothing can load it.
|
|
6
|
+
*/
|
|
7
|
+
export type DemoDataSetSummary = {
|
|
8
|
+
id: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The loaded/imported dataset that gets scheduled, POSTed wholesale as the
|
|
12
|
+
* solve request body.
|
|
13
|
+
*/
|
|
14
|
+
export type ActiveDataSet<InputType> = {
|
|
15
|
+
id?: string;
|
|
16
|
+
modelInput: InputType;
|
|
17
|
+
};
|
|
18
|
+
/** Embedding params a dev build uses where the query string has none: a local run and its credentials. */
|
|
19
|
+
export type DevEmbeddingParams = {
|
|
20
|
+
apiUrl?: string;
|
|
21
|
+
runId?: string;
|
|
22
|
+
tenantId?: string;
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
featureFlags?: string[];
|
|
25
|
+
};
|
|
26
|
+
/** The `config` of a `VisualizationProvider`: which model API it talks to and how it reads and merges data. */
|
|
27
|
+
export type VisualizationConfig<InputType, OutputType, MergedType = InputType> = {
|
|
28
|
+
apiVersion?: string;
|
|
29
|
+
dataSetPath: string;
|
|
30
|
+
demoDataSetPath: string;
|
|
31
|
+
/** @remarks Receives the API key; `allowedApiHosts` does not gate it, as it gates only the `apiUrl` parameter. */
|
|
32
|
+
host?: string;
|
|
33
|
+
/**
|
|
34
|
+
* @remarks Bare host names that may receive the API key and post to the embed, such as `"api.example.com"`: no
|
|
35
|
+
* scheme, port or path. An entry also trusts its subdomains, so `"example.com"` trusts `api.example.com`.
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* allowedApiHosts: ["example.com", "localhost"]
|
|
39
|
+
* ```
|
|
40
|
+
* @defaultValue `[]`, so only the page's own origin is trusted
|
|
41
|
+
*/
|
|
42
|
+
allowedApiHosts?: readonly string[];
|
|
43
|
+
/** @defaultValue {} */
|
|
44
|
+
devDefaults?: DevEmbeddingParams;
|
|
45
|
+
/** @remarks Called with `undefined` output before a solve has output; without it the input is the merged view. */
|
|
46
|
+
mergeData?: (inputData: InputType, outputData: OutputType | undefined) => MergedType;
|
|
47
|
+
validateInputData?: (inputData: InputType) => string | null;
|
|
48
|
+
};
|
|
49
|
+
/** The solver run lifecycle, as reported by the Models API `solverStatus` field. */
|
|
50
|
+
export type SolvingStatus = "DATASET_CREATED" | "DATASET_VALIDATED" | "DATASET_INVALID" | "DATASET_COMPUTED" | "SOLVING_SCHEDULED" | "SOLVING_STARTED" | "SOLVING_ACTIVE" | "SOLVING_COMPLETED" | "SOLVING_INCOMPLETE" | "SOLVING_FAILED" | "SOLVING_STOPPED";
|
|
51
|
+
/**
|
|
52
|
+
* True while the run is still in progress (any non-terminal status).
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* The pre-solving states (`DATASET_CREATED`/`DATASET_COMPUTED`) count as in progress.
|
|
56
|
+
*/
|
|
57
|
+
export declare function isActivelySolving(solverStatus: SolvingStatus): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* True while the app should show a solve at work: the solver runs, or a solve the reader started here is warming up.
|
|
60
|
+
* A pre-built run (`DATASET_COMPUTED`) stays non-terminal so its events are still read, but nothing solves it.
|
|
61
|
+
*/
|
|
62
|
+
export declare function isSolveAtWork(status: DataSetStatus, inAppSolveActive: boolean): boolean;
|
|
63
|
+
/** True for the terminal statuses SolveButton renders as "Solve failed". */
|
|
64
|
+
export declare function isSolveFailure(solverStatus: SolvingStatus): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Unified lifecycle status for the active dataset — the single source of
|
|
67
|
+
* truth for loading a dataset by `runId` and for an in-app solve.
|
|
68
|
+
*/
|
|
69
|
+
export type DataSetStatus = {
|
|
70
|
+
phase: "idle";
|
|
71
|
+
} | {
|
|
72
|
+
phase: "loading";
|
|
73
|
+
} | {
|
|
74
|
+
phase: "solving";
|
|
75
|
+
solveId: string;
|
|
76
|
+
solverStatus: SolvingStatus;
|
|
77
|
+
} | {
|
|
78
|
+
phase: "solve-failed";
|
|
79
|
+
solverStatus: SolvingStatus;
|
|
80
|
+
validationErrors?: string[];
|
|
81
|
+
} | {
|
|
82
|
+
phase: "error";
|
|
83
|
+
error: string;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Maps a freshly observed `solverStatus` to the next {@link DataSetStatus}.
|
|
87
|
+
*
|
|
88
|
+
* @remarks
|
|
89
|
+
* A missing/null `solverStatus` degrades to `idle` rather than an error.
|
|
90
|
+
*/
|
|
91
|
+
export declare function resolveSolveStatus(solverStatus: SolvingStatus | null | undefined, solveId: string, validationErrors?: string[]): DataSetStatus;
|
|
92
|
+
/** What a page reads of the loaded dataset: its input, output, KPIs, score and run status. */
|
|
93
|
+
export type DataSetState<InputType, OutputType, MergedType = InputType, KpisType = unknown> = {
|
|
94
|
+
readonly isOnPlatform: boolean;
|
|
95
|
+
readonly dataSetId?: string;
|
|
96
|
+
readonly inputData?: InputType;
|
|
97
|
+
readonly outputData?: OutputType;
|
|
98
|
+
readonly mergedData?: MergedType;
|
|
99
|
+
readonly kpis?: KpisType;
|
|
100
|
+
readonly score: string;
|
|
101
|
+
/** @remarks The run lifecycle. To show a solve at work, read `useDataSetLoadState().isSolving`. */
|
|
102
|
+
readonly status: DataSetStatus;
|
|
103
|
+
};
|
|
104
|
+
export type DataSetProviderState<InputType, OutputType, MergedType = InputType, KpisType = unknown> = DataSetState<InputType, OutputType, MergedType, KpisType> & {
|
|
105
|
+
config: VisualizationConfig<InputType, OutputType, MergedType>;
|
|
106
|
+
allowedApiHosts: readonly string[];
|
|
107
|
+
isDevelopment: boolean;
|
|
108
|
+
apiKey?: string;
|
|
109
|
+
tenantId?: string;
|
|
110
|
+
featureFlags: Set<string>;
|
|
111
|
+
platformChrome: boolean;
|
|
112
|
+
embeddingError?: string;
|
|
113
|
+
dataSetName?: string;
|
|
114
|
+
activeDataSet: ActiveDataSet<InputType> | null;
|
|
115
|
+
inAppSolveActive: boolean;
|
|
116
|
+
};
|
|
117
|
+
/** The fixture data a provider starts from, for a test that mounts a page. `inputData` passes `validateInputData`. */
|
|
118
|
+
export type DataSetInitialState<InputType, OutputType, MergedType = InputType> = Partial<Pick<DataSetState<InputType, OutputType, MergedType>, "inputData" | "outputData" | "mergedData" | "kpis" | "score" | "status">>;
|
|
119
|
+
/** An action the dataset reducer handles to change the state of the loaded dataset. */
|
|
120
|
+
export type DataSetAction<InputType, OutputType, KpisType = unknown> = {
|
|
121
|
+
type: "SET_INPUT";
|
|
122
|
+
inputData: InputType;
|
|
123
|
+
source?: "run" | "local";
|
|
124
|
+
} | {
|
|
125
|
+
type: "REJECT_INPUT";
|
|
126
|
+
error: string;
|
|
127
|
+
} | {
|
|
128
|
+
type: "SET_OUTPUT";
|
|
129
|
+
outputData: OutputType;
|
|
130
|
+
kpis?: KpisType;
|
|
131
|
+
} | {
|
|
132
|
+
type: "SET_SCORE";
|
|
133
|
+
score: string;
|
|
134
|
+
} | {
|
|
135
|
+
type: "SET_STATUS";
|
|
136
|
+
status: DataSetStatus;
|
|
137
|
+
} | {
|
|
138
|
+
type: "START_SOLVE";
|
|
139
|
+
} | {
|
|
140
|
+
type: "SET_ACTIVE_DATASET";
|
|
141
|
+
activeDataSet: ActiveDataSet<InputType> | null;
|
|
142
|
+
} | {
|
|
143
|
+
type: "SET_IN_APP_SOLVE_ACTIVE";
|
|
144
|
+
active: boolean;
|
|
145
|
+
} | {
|
|
146
|
+
type: "SET_DATASET_ID";
|
|
147
|
+
dataSetId: string;
|
|
148
|
+
} | {
|
|
149
|
+
type: "SET_DATASET_NAME";
|
|
150
|
+
name: string;
|
|
151
|
+
} | {
|
|
152
|
+
type: "SET_API_KEY";
|
|
153
|
+
apiKey: string;
|
|
154
|
+
} | {
|
|
155
|
+
type: "SET_EMBEDDING_PARAMS";
|
|
156
|
+
tenantId: string;
|
|
157
|
+
dataSetId?: string;
|
|
158
|
+
apiKey: string;
|
|
159
|
+
host: string;
|
|
160
|
+
apiVersion?: string;
|
|
161
|
+
embeddingError?: string;
|
|
162
|
+
featureFlags: Set<string>;
|
|
163
|
+
};
|
|
164
|
+
export declare const DataSetStateContext: import("react").Context<DataSetProviderState<unknown, unknown, unknown, unknown> | null>;
|
|
165
|
+
export declare const DataSetDispatchContext: import("react").Context<Dispatch<DataSetAction<unknown, unknown, unknown>> | null>;
|
|
166
|
+
/** Holds the embedding host's unit system for {@link useDistanceUnitSystem}. */
|
|
167
|
+
export declare const DistanceUnitSystemContext: import("react").Context<DistanceUnitSystem>;
|
|
168
|
+
/**
|
|
169
|
+
* The unit system every distance renders in: the embedding host's choice, or metric when no host sent one.
|
|
170
|
+
*
|
|
171
|
+
* @category Hooks
|
|
172
|
+
*/
|
|
173
|
+
export declare function useDistanceUnitSystem(): DistanceUnitSystem;
|
|
174
|
+
/** Holds the free height the embedding host reported, for {@link useViewportHeight}. */
|
|
175
|
+
export declare const HostViewportHeightContext: import("react").Context<number | undefined>;
|
|
176
|
+
/**
|
|
177
|
+
* The height a page may fill: the window standalone; on the platform the host's free height below the frame, or
|
|
178
|
+
* undefined until it reports one, because the frame itself is sized to the content.
|
|
179
|
+
*
|
|
180
|
+
* @category Hooks
|
|
181
|
+
*/
|
|
182
|
+
export declare function useViewportHeight(): number | undefined;
|
|
183
|
+
/** The provider's solve controls and input writers, in their own context so a writer reads no state. */
|
|
184
|
+
export type DataSetControls = DataSetWriters<unknown> & {
|
|
185
|
+
startSolve: () => Promise<void>;
|
|
186
|
+
stopSolve: () => void;
|
|
187
|
+
};
|
|
188
|
+
export declare const DataSetControlsContext: import("react").Context<DataSetControls | null>;
|
|
189
|
+
/**
|
|
190
|
+
* The value of `useDataSet()`: the dataset state, the active dataset, and the functions that write them.
|
|
191
|
+
* A page fetches a dataset sub-resource through `useFetchDataSetResource()`, which keeps the key inside the library.
|
|
192
|
+
*/
|
|
193
|
+
export type DataSet<InputType, OutputType, MergedType = InputType, KpisType = unknown> = DataSetState<InputType, OutputType, MergedType, KpisType> & {
|
|
194
|
+
isDevelopment: boolean;
|
|
195
|
+
activeDataSet: ActiveDataSet<InputType> | null;
|
|
196
|
+
updateOutputData: (outputData: OutputType, kpis?: KpisType) => void;
|
|
197
|
+
updateInputData: (inputData: InputType, source?: "run" | "local") => void;
|
|
198
|
+
updateActiveDataSet: (activeDataSet: ActiveDataSet<InputType> | null) => void;
|
|
199
|
+
};
|
|
200
|
+
export declare function useDataSetProviderState<InputType, OutputType, MergedType = InputType, KpisType = unknown>(): DataSetProviderState<InputType, OutputType, MergedType, KpisType>;
|
|
201
|
+
/** The `REJECT_INPUT` action naming what `validateInputData` finds wrong with the input, or `null` for valid input. */
|
|
202
|
+
export declare function rejectInvalidInput<InputType, OutputType, MergedType>(config: VisualizationConfig<InputType, OutputType, MergedType>, inputData: InputType): {
|
|
203
|
+
type: "REJECT_INPUT";
|
|
204
|
+
error: string;
|
|
205
|
+
} | null;
|
|
206
|
+
type DataSetWriters<InputType, OutputType = unknown> = Pick<DataSet<InputType, OutputType>, "updateInputData" | "updateActiveDataSet">;
|
|
207
|
+
/** The input writers of a dataset, which reject input that `validateInputData` refuses. */
|
|
208
|
+
export declare function createDataSetWriters<InputType, OutputType, MergedType, KpisType>(config: VisualizationConfig<InputType, OutputType, MergedType>, dispatch: Dispatch<DataSetAction<InputType, OutputType, KpisType>>): DataSetWriters<InputType, OutputType>;
|
|
209
|
+
export declare function useDataSetActions<InputType, OutputType = unknown>(): DataSetWriters<InputType, OutputType> & Pick<DataSetControls, "startSolve" | "stopSolve">;
|
|
210
|
+
/**
|
|
211
|
+
* Reads the loaded dataset from the nearest `VisualizationProvider`, as one object per state, with the
|
|
212
|
+
* `updateOutputData`, `updateInputData` and `updateActiveDataSet` writers a page uses to update it.
|
|
213
|
+
*/
|
|
214
|
+
export declare function useDataSet<InputType, OutputType, MergedType = InputType, KpisType = unknown>(): DataSet<InputType, OutputType, MergedType, KpisType>;
|
|
215
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import "@timefoldai/viz-core";
|
|
3
|
+
import { createContext, use, useCallback, useMemo, useSyncExternalStore } from "react";
|
|
4
|
+
// src/dataset-context/DataSetContext.ts
|
|
5
|
+
const TERMINAL_FAILURE_STATUSES = [
|
|
6
|
+
"SOLVING_FAILED",
|
|
7
|
+
"SOLVING_INCOMPLETE",
|
|
8
|
+
"DATASET_INVALID"
|
|
9
|
+
];
|
|
10
|
+
const TERMINAL_SUCCESS_STATUSES = ["SOLVING_COMPLETED", "SOLVING_STOPPED"];
|
|
11
|
+
function isTerminalStatus(solverStatus) {
|
|
12
|
+
return TERMINAL_FAILURE_STATUSES.includes(solverStatus) || TERMINAL_SUCCESS_STATUSES.includes(solverStatus);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* True while the run is still in progress (any non-terminal status).
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* The pre-solving states (`DATASET_CREATED`/`DATASET_COMPUTED`) count as in progress.
|
|
19
|
+
*/
|
|
20
|
+
function isActivelySolving(solverStatus) {
|
|
21
|
+
return !isTerminalStatus(solverStatus);
|
|
22
|
+
}
|
|
23
|
+
const SOLVER_RUNNING_STATUSES = [
|
|
24
|
+
"SOLVING_SCHEDULED",
|
|
25
|
+
"SOLVING_STARTED",
|
|
26
|
+
"SOLVING_ACTIVE"
|
|
27
|
+
];
|
|
28
|
+
/**
|
|
29
|
+
* True while the app should show a solve at work: the solver runs, or a solve the reader started here is warming up.
|
|
30
|
+
* A pre-built run (`DATASET_COMPUTED`) stays non-terminal so its events are still read, but nothing solves it.
|
|
31
|
+
*/
|
|
32
|
+
function isSolveAtWork(status, inAppSolveActive) {
|
|
33
|
+
return status.phase === "solving" && (inAppSolveActive || SOLVER_RUNNING_STATUSES.includes(status.solverStatus));
|
|
34
|
+
}
|
|
35
|
+
/** True for the terminal statuses SolveButton renders as "Solve failed". */
|
|
36
|
+
function isSolveFailure(solverStatus) {
|
|
37
|
+
return TERMINAL_FAILURE_STATUSES.includes(solverStatus);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Maps a freshly observed `solverStatus` to the next {@link DataSetStatus}.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* A missing/null `solverStatus` degrades to `idle` rather than an error.
|
|
44
|
+
*/
|
|
45
|
+
function resolveSolveStatus(solverStatus, solveId, validationErrors) {
|
|
46
|
+
if (solverStatus && isActivelySolving(solverStatus)) return {
|
|
47
|
+
phase: "solving",
|
|
48
|
+
solveId,
|
|
49
|
+
solverStatus
|
|
50
|
+
};
|
|
51
|
+
if (solverStatus && isSolveFailure(solverStatus)) return {
|
|
52
|
+
phase: "solve-failed",
|
|
53
|
+
solverStatus,
|
|
54
|
+
...validationErrors && validationErrors.length > 0 ? { validationErrors } : {}
|
|
55
|
+
};
|
|
56
|
+
return { phase: "idle" };
|
|
57
|
+
}
|
|
58
|
+
const DataSetStateContext = createContext(null);
|
|
59
|
+
const DataSetDispatchContext = createContext(null);
|
|
60
|
+
/** Holds the embedding host's unit system for {@link useDistanceUnitSystem}. */
|
|
61
|
+
const DistanceUnitSystemContext = createContext("metric");
|
|
62
|
+
/**
|
|
63
|
+
* The unit system every distance renders in: the embedding host's choice, or metric when no host sent one.
|
|
64
|
+
*
|
|
65
|
+
* @category Hooks
|
|
66
|
+
*/
|
|
67
|
+
function useDistanceUnitSystem() {
|
|
68
|
+
return use(DistanceUnitSystemContext);
|
|
69
|
+
}
|
|
70
|
+
/** Holds the free height the embedding host reported, for {@link useViewportHeight}. */
|
|
71
|
+
const HostViewportHeightContext = createContext(void 0);
|
|
72
|
+
function subscribeToWindowResize(onResize) {
|
|
73
|
+
window.addEventListener("resize", onResize);
|
|
74
|
+
return () => window.removeEventListener("resize", onResize);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The height a page may fill: the window standalone; on the platform the host's free height below the frame, or
|
|
78
|
+
* undefined until it reports one, because the frame itself is sized to the content.
|
|
79
|
+
*
|
|
80
|
+
* @category Hooks
|
|
81
|
+
*/
|
|
82
|
+
function useViewportHeight() {
|
|
83
|
+
const { isOnPlatform } = useDataSetProviderState();
|
|
84
|
+
const hostHeight = use(HostViewportHeightContext);
|
|
85
|
+
const windowHeight = useSyncExternalStore(subscribeToWindowResize, () => window.innerHeight);
|
|
86
|
+
return isOnPlatform ? hostHeight : windowHeight;
|
|
87
|
+
}
|
|
88
|
+
const DataSetControlsContext = createContext(null);
|
|
89
|
+
const OUTSIDE_PROVIDER = "The dataset context is missing: call this hook inside a VisualizationProvider.";
|
|
90
|
+
function resolveMergedView(state) {
|
|
91
|
+
if (state.mergedData !== void 0) return state.mergedData;
|
|
92
|
+
if (state.inputData === void 0) return void 0;
|
|
93
|
+
return state.inputData;
|
|
94
|
+
}
|
|
95
|
+
function useDataSetProviderState() {
|
|
96
|
+
const state = use(DataSetStateContext);
|
|
97
|
+
const stateWithMergedView = useMemo(() => state === null ? null : {
|
|
98
|
+
...state,
|
|
99
|
+
mergedData: resolveMergedView(state)
|
|
100
|
+
}, [state]);
|
|
101
|
+
if (stateWithMergedView === null) throw new Error(OUTSIDE_PROVIDER);
|
|
102
|
+
return stateWithMergedView;
|
|
103
|
+
}
|
|
104
|
+
function useDataSetDispatch() {
|
|
105
|
+
const dispatch = use(DataSetDispatchContext);
|
|
106
|
+
if (dispatch === null) throw new Error(OUTSIDE_PROVIDER);
|
|
107
|
+
return dispatch;
|
|
108
|
+
}
|
|
109
|
+
/** The `REJECT_INPUT` action naming what `validateInputData` finds wrong with the input, or `null` for valid input. */
|
|
110
|
+
function rejectInvalidInput(config, inputData) {
|
|
111
|
+
const problem = config.validateInputData?.(inputData);
|
|
112
|
+
return problem ? {
|
|
113
|
+
type: "REJECT_INPUT",
|
|
114
|
+
error: `Could not load the dataset: ${problem}.`
|
|
115
|
+
} : null;
|
|
116
|
+
}
|
|
117
|
+
/** The input writers of a dataset, which reject input that `validateInputData` refuses. */
|
|
118
|
+
function createDataSetWriters(config, dispatch) {
|
|
119
|
+
return {
|
|
120
|
+
updateInputData: (inputData, source) => dispatch(rejectInvalidInput(config, inputData) ?? {
|
|
121
|
+
type: "SET_INPUT",
|
|
122
|
+
inputData,
|
|
123
|
+
source
|
|
124
|
+
}),
|
|
125
|
+
updateActiveDataSet: (activeDataSet) => dispatch({
|
|
126
|
+
type: "SET_ACTIVE_DATASET",
|
|
127
|
+
activeDataSet
|
|
128
|
+
})
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function useDataSetActions() {
|
|
132
|
+
const controls = use(DataSetControlsContext);
|
|
133
|
+
if (controls === null) throw new Error(OUTSIDE_PROVIDER);
|
|
134
|
+
return controls;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Reads the loaded dataset from the nearest `VisualizationProvider`, as one object per state, with the
|
|
138
|
+
* `updateOutputData`, `updateInputData` and `updateActiveDataSet` writers a page uses to update it.
|
|
139
|
+
*/
|
|
140
|
+
function useDataSet() {
|
|
141
|
+
const state = useDataSetProviderState();
|
|
142
|
+
const dispatch = useDataSetDispatch();
|
|
143
|
+
const updateOutputData = useCallback((outputData, kpis) => dispatch({
|
|
144
|
+
type: "SET_OUTPUT",
|
|
145
|
+
outputData,
|
|
146
|
+
kpis
|
|
147
|
+
}), [dispatch]);
|
|
148
|
+
const { updateInputData, updateActiveDataSet } = useMemo(() => createDataSetWriters(state.config, dispatch), [state.config, dispatch]);
|
|
149
|
+
return useMemo(() => ({
|
|
150
|
+
isOnPlatform: state.isOnPlatform,
|
|
151
|
+
dataSetId: state.dataSetId,
|
|
152
|
+
inputData: state.inputData,
|
|
153
|
+
outputData: state.outputData,
|
|
154
|
+
mergedData: state.mergedData,
|
|
155
|
+
kpis: state.kpis,
|
|
156
|
+
score: state.score,
|
|
157
|
+
status: state.status,
|
|
158
|
+
isDevelopment: state.isDevelopment,
|
|
159
|
+
activeDataSet: state.activeDataSet,
|
|
160
|
+
updateOutputData,
|
|
161
|
+
updateInputData,
|
|
162
|
+
updateActiveDataSet
|
|
163
|
+
}), [
|
|
164
|
+
state,
|
|
165
|
+
updateOutputData,
|
|
166
|
+
updateInputData,
|
|
167
|
+
updateActiveDataSet
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
export { DataSetControlsContext, DataSetDispatchContext, DataSetStateContext, DistanceUnitSystemContext, HostViewportHeightContext, createDataSetWriters, isActivelySolving, isSolveAtWork, isSolveFailure, rejectInvalidInput, resolveSolveStatus, useDataSet, useDataSetActions, useDataSetProviderState, useDistanceUnitSystem, useViewportHeight };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DataSetAction, DataSetInitialState, DataSetProviderState, VisualizationConfig } from "./DataSetContext.mjs";
|
|
2
|
+
import { DistanceUnitSystem } from "@timefoldai/viz-core";
|
|
3
|
+
import { ReactNode } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* The single owner of dataset load and solve state; exported for direct unit
|
|
6
|
+
* testing without mounting the provider.
|
|
7
|
+
*/
|
|
8
|
+
export declare function dataSetReducer<InputType, OutputType, MergedType, KpisType>(state: DataSetProviderState<InputType, OutputType, MergedType, KpisType>, action: DataSetAction<InputType, OutputType, KpisType>): DataSetProviderState<InputType, OutputType, MergedType, KpisType>;
|
|
9
|
+
/**
|
|
10
|
+
* Provides the active dataset to descendant visualisation components.
|
|
11
|
+
*
|
|
12
|
+
* @category Providers
|
|
13
|
+
*/
|
|
14
|
+
export declare function DataSetContextProvider<InputType, OutputType, MergedType = InputType>({ config, initialState, unitSystem, children }: {
|
|
15
|
+
config: VisualizationConfig<InputType, OutputType, MergedType>;
|
|
16
|
+
initialState?: DataSetInitialState<InputType, OutputType, MergedType>;
|
|
17
|
+
unitSystem?: DistanceUnitSystem;
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
}): import("react").JSX.Element;
|