/
hooks.ts
561 lines (481 loc) · 14.4 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import {
useState,
useEffect,
useCallback,
useMemo,
useRef,
useContext,
createContext,
} from 'react';
import {
Property,
Store,
Resource,
Datatype,
datatypeFromUrl,
urls,
truncateUrl,
JSONValue,
valToBoolean,
valToNumber,
valToDate,
valToArray,
valToString,
FetchOpts,
unknownSubject,
JSONArray,
OptionalClass,
proxyResource,
} from '@tomic/lib';
import { useDebouncedCallback } from './index.js';
/**
* Hook for getting a Resource in a React component. Will try to fetch the
* subject and add its parsed values to the store.
*/
export function useResource<C extends OptionalClass = never>(
subject: string = unknownSubject,
opts?: FetchOpts,
): Resource<C> {
const store = useStore();
const [resource, setResource] = useState<Resource<C>>(() =>
store.getResourceLoading(subject, opts),
);
// If the subject changes, make sure to change the resource!
// When a component mounts, it needs to let the store know that it will subscribe to changes to that resource.
useEffect(() => {
setResource(proxyResource(store.getResourceLoading(subject, opts)));
return store.subscribe(subject, (updated: Resource<C>) => {
setResource(proxyResource(updated));
});
}, [store, subject]);
return resource;
}
/**
* Converts an array of Atomic URL strings to an array of Resources. Could take
* a long time.
*/
export function useResources(
subjects: string[],
opts: FetchOpts = {},
): Map<string, Resource> {
const [resources, setResources] = useState(new Map<string, Resource>());
const store = useStore();
useEffect(() => {
// When a change happens, set the new Resource.
function handleNotify(updated: Resource) {
setResources(prev => {
prev.set(updated.getSubject(), proxyResource(updated));
// We need to create new Maps for react hooks to update - React only checks references, not content
return new Map(prev);
});
}
setResources(prev => {
for (const subject of subjects) {
const resource = store.getResourceLoading(subject, opts);
prev.set(subject, proxyResource(resource));
// Let the store know to call handleNotify when a resource is updated.
store.subscribe(subject, handleNotify);
}
return new Map(prev);
});
return () => {
// When the component is unmounted, unsubscribe from the store.
for (const subject of subjects) {
store.unsubscribe(subject, handleNotify);
}
};
// maybe add resources here
}, [subjects, store]);
return resources;
}
/**
* Hook for using a Property. Will return `undefined` if the Property is not yet
* loaded, and add Error strings to shortname and description if something goes wrong.
*/
export function useProperty(subject: string): Property {
const propertyResource = useResource(subject);
if (propertyResource.loading) {
return {
subject,
datatype: Datatype.UNKNOWN,
shortname: 'loading',
description: `Loading property ${subject}`,
loading: true,
};
}
if (propertyResource.error) {
return {
subject,
datatype: Datatype.UNKNOWN,
shortname: 'error',
description: 'Error getting Property. ' + propertyResource.error.message,
error: propertyResource.error,
};
}
const datatypeUrl = propertyResource.get(urls.properties.datatype) as string;
const datatype = datatypeFromUrl(datatypeUrl);
const shortname = propertyResource.get(urls.properties.shortname) as string;
const description = propertyResource.get(
urls.properties.description,
) as string;
const classType = propertyResource.get(urls.properties.classType) as string;
const isDynamic = !!propertyResource.get(
urls.properties.isDynamic,
) as boolean;
const property: Property = {
subject,
datatype,
shortname,
description,
classType,
isDynamic,
};
return property;
}
export type SetValue<T extends JSONValue = JSONValue> = (
val: T | undefined,
) => Promise<void>;
/** Extra options for useValue hooks, mostly related to commits and validation */
type useValueOptions = {
/**
* Sends a Commit to the server when the value is changed. Disabled by
* default. If this is false, you will need to manually call Resource.save()
* to save changes
*/
commit?: boolean;
/**
* Performs datatype validation. Enabled by default, but this could cause some
* slowdown when the first validation is done as the Property needs to be
* present in the store, and might have to be fetched
*/
validate?: boolean;
/** Amount of milliseconds to wait (debounce) before applying Commit. Defaults to 100. */
commitDebounce?: number;
/**
* A callback function that will be called when the validation fails. For
* example, pass a `setError` function. If you want to remove the Error, return `undefined`.
*/
handleValidationError?: (e: Error | undefined) => unknown;
};
/**
* Similar to React's `useState` hook. Returns a Value and a Setter as an array
* of two items. Value will be `undefined` if the Resource isn't loaded yet. The
* generated Setter function can be called to set the value. Be sure to look at
* the various options for useValueOptions (debounce, commits, error handling).
*
* ```typescript
* // Simple usage:
* const resource = useResource('https://atomicdata.dev/classes/Agent');
* const [shortname, setShortname] = useValue(
* resource,
* 'https://atomicdata.dev/properties/shortname',
* );
* ```
*
* ```typescript
* // With options:
* const resource = useResource('https://atomicdata.dev/classes/Agent');
* const [error, setError] = useState(null);
* const [shortname, setShortname] = useValue(
* resource,
* 'https://atomicdata.dev/properties/shortname',
* {
* commit: true,
* validate: true,
* commitDebounce: 500,
* handleValidationError: setError,
* },
* );
* ```
*/
export function useValue(
resource: Resource,
propertyURL: string,
opts: useValueOptions = {},
): [JSONValue | undefined, SetValue] {
const {
commit = false,
validate = true,
commitDebounce = 100,
handleValidationError,
} = opts;
const [val, set] = useState<JSONValue>(resource.get(propertyURL));
const [prevResourceReference, setPrevResourceReference] = useState(resource);
const store = useStore();
const [saveResource] = useDebouncedCallback(
() => {
if (!commit) {
return;
}
resource.save(store).catch(e => store.notifyError(e));
},
commitDebounce,
[resource, store],
);
/**
* Validates the value. If it fails, it calls the function in the second
* Argument. Pass `undefined` to remove existing value.
*/
const validateAndSet = useCallback(
async (newVal: JSONValue): Promise<void> => {
if (newVal === undefined) {
// remove the value
resource.removePropVal(propertyURL);
set(undefined);
saveResource();
return;
}
set(newVal);
// Validates and sets a property / value combination. Will invoke the
// callback if the value is not valid.
try {
await resource.set(propertyURL, newVal, store, validate);
saveResource();
handleValidationError?.(undefined);
} catch (e) {
if (handleValidationError) {
handleValidationError(e);
} else {
store.notifyError(e);
}
}
},
[resource, handleValidationError, store, validate, saveResource],
);
// Update value when resource changes.
if (resource !== prevResourceReference) {
try {
set(resource.get(propertyURL));
} catch (e) {
store.notifyError(e);
}
setPrevResourceReference(resource);
}
return [val, validateAndSet];
}
/**
* Hook for getting and setting a stringified representation of an Atom in a
* React component. See {@link useValue}
*/
export function useString(
resource: Resource,
propertyURL: string,
opts?: useValueOptions,
): [string | undefined, SetValue<string>] {
const [val, setVal] = useValue(resource, propertyURL, opts);
if (typeof val === 'string') {
return [val, setVal];
}
if (val === undefined) {
return [undefined, setVal];
}
return [valToString(val), setVal];
}
export const noNestedSupport =
'error:no_support_for_editing_nested_resources_yet';
/**
* Hook for getting and setting a Subject. Converts Nested resources into paths.
* See {@link useValue} for more info on using the `set` functionality.
*/
export function useSubject(
resource: Resource,
propertyURL: string,
opts?: useValueOptions,
): [string | undefined, SetValue<string>] {
const [val, setVal] = useValue(resource, propertyURL, opts);
if (!val) {
return [undefined, setVal];
}
if (typeof val === 'string') {
return [val, setVal];
} else {
// It's a nested resource
// TODO: Implement support for this. Get the subject from the Resource, or construct te Path.
return [noNestedSupport, setVal];
}
}
const titleHookOpts: useValueOptions = {
commit: true,
};
/**
* Returns the most fitting title / name for a Resource. This is either the
* Name, Shortname, Filename or truncated Subject URL of that resource.
*/
export function useTitle(
resource: Resource,
truncateLength = 40,
opts: useValueOptions = titleHookOpts,
): [string, SetValue<string>] {
const [name, setName] = useString(resource, urls.properties.name, opts);
const [shortname, setShortname] = useString(
resource,
urls.properties.shortname,
opts,
);
const [filename, setFileName] = useString(
resource,
urls.properties.file.filename,
opts,
);
if (resource.loading) {
return ['...', setName];
}
if (name !== undefined) {
return [name, setName];
}
if (shortname !== undefined) {
return [shortname, setShortname];
}
if (filename !== undefined) {
return [filename, setFileName];
}
const subject = resource?.getSubject();
if (typeof subject === 'string' && subject.length > 0) {
return [truncateUrl(subject, truncateLength), setName];
}
return [subject, setName];
}
/**
* Hook for getting all URLs for some array. Returns the current Array (defaults
* to empty array) and a callback for validation errors. See {@link useValue}
*/
export function useArray(
resource: Resource,
propertyURL: string,
opts?: useValueOptions,
): [string[], SetValue<JSONArray>, (vals: string[]) => void] {
const [value, set] = useValue(resource, propertyURL, opts);
const stableEmptyArray = useRef<JSONArray>([]);
const store = useStore();
const values = useMemo(() => {
if (value === undefined) {
return stableEmptyArray.current;
}
try {
// This cast isn't entirely correct - we should add a `useSubjects` hook.
// https://github.com/atomicdata-dev/atomic-data-browser/issues/219
return valToArray(value);
} catch (e) {
console.error(e, value, propertyURL, resource.getSubject());
// If .toArray() errors, return an empty array. Useful in forms when datatypes haves changed!
// https://github.com/atomicdata-dev/atomic-data-browser/issues/85
return stableEmptyArray.current;
}
}, [value, resource, propertyURL]);
const push = useCallback(
(val: string[]) => {
resource.pushPropVal(propertyURL, val);
if (opts?.commit) {
resource.save(store);
}
},
[resource, propertyURL, store],
);
return [values as string[], set, push];
}
/** See {@link useValue} */
export function useNumber(
resource: Resource,
propertyURL: string,
opts?: useValueOptions,
): [number | undefined, SetValue<number>] {
const [value, set] = useValue(resource, propertyURL, opts);
if (value === undefined) {
return [undefined, set];
}
return [valToNumber(value), set];
}
/** Returns false if there is no value for this propertyURL. See {@link useValue} */
export function useBoolean(
resource: Resource,
propertyURL: string,
opts?: useValueOptions,
): [boolean, SetValue<boolean>] {
const [value, set] = useValue(resource, propertyURL, opts);
if (value === undefined) {
return [false, set];
}
return [valToBoolean(value), set];
}
/**
* Hook for getting a stringified representation of an Atom in a React
* component. See {@link useValue}
*/
export function useDate(
resource: Resource,
propertyURL: string,
opts?: useValueOptions,
): Date | undefined {
const store = useStore();
const [value] = useValue(resource, propertyURL, opts);
if (value === undefined) {
return undefined;
}
try {
return valToDate(value);
} catch (e) {
store.notifyError(e);
return;
}
}
/** Preferred way of using the store in a Component or Hook */
export function useStore(): Store {
const store = useContext(StoreContext);
if (store === undefined) {
throw new Error(
'Store is not found in react context. Have you wrapped your application in `<StoreContext.Provider value={new Store}>`?',
);
}
return store;
}
/**
* Checks if the Agent has the appropriate rights to edit this resource. If you
* don't explicitly pass an Agent URL, it will select the current Agent set by the store.
*/
export function useCanWrite(
resource: Resource,
agent?: string,
): [canWrite: boolean, message: string | undefined] {
const store = useStore();
const [canWrite, setCanWrite] = useState<boolean>(false);
const [msg, setMsg] = useState<string | undefined>(undefined);
const agentStore = store.getAgent();
// If the subject changes, make sure to change the resource!
useEffect(() => {
if (agent === undefined) {
agent = agentStore?.subject;
}
if (agent === undefined) {
setMsg('No Agent set');
setCanWrite(false);
return;
}
if (resource.new) {
setCanWrite(true);
return;
}
setMsg('Checking write rights...');
async function tryCanWrite() {
const [canWriteAsync, canWriteMsg] = await resource.canWrite(
store,
agent,
);
setCanWrite(canWriteAsync);
if (canWriteAsync) {
setMsg(undefined);
} else {
setMsg(
("You don't have write rights in this resource or its parents: " +
canWriteMsg) as string,
);
}
}
tryCanWrite();
}, [resource, agent, agentStore?.subject]);
return [canWrite, msg];
}
/**
* The context must be provided by wrapping a high level React element in
* `<StoreContext.Provider value={new Store}>My App</StoreContext.Provider>`
*/
export const StoreContext = createContext<Store>(new Store());