@swan-io/lake 4.6.0 → 4.6.1

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-io/lake",
3
- "version": "4.6.0",
3
+ "version": "4.6.1",
4
4
  "engines": {
5
5
  "node": ">=18.0.0",
6
6
  "yarn": "^1.22.0"
@@ -182,7 +182,7 @@ function FilterInput({ label, initialValue = "", noValueText, submitText, autoOp
182
182
  }
183
183
  });
184
184
  };
185
- return (_jsxs(View, { style: styles.container, children: [_jsx(FilterTag, { label: label, onPress: toggle, ref: inputRef, onPressRemove: onPressRemove, isActive: visible, value: value === "" ? noValueText : value }), _jsx(Popover, { role: "listbox", matchReferenceWidth: false, onDismiss: close, referenceRef: inputRef, returnFocus: false, visible: visible, children: _jsxs(View, { style: [styles.dropdown, styles.inputContent], children: [_jsx(Field, { name: "input", children: ({ error, value, onChange }) => (_jsx(LakeLabel, { label: label, render: id => (_jsx(LakeTextInput, { nativeID: id, error: error, style: styles.input, placeholder: placeholder, value: value, onChangeText: onChange })) })) }), _jsx(LakeButton, { size: "small", color: "current", onPress: onSubmit, children: submitText })] }) })] }));
185
+ return (_jsxs(View, { style: styles.container, children: [_jsx(FilterTag, { label: label, onPress: toggle, ref: inputRef, onPressRemove: onPressRemove, isActive: visible, value: value === "" ? noValueText : value }), _jsx(Popover, { role: "listbox", matchReferenceWidth: false, onDismiss: close, referenceRef: inputRef, returnFocus: false, visible: visible, children: _jsxs(View, { style: [styles.dropdown, styles.inputContent], children: [_jsx(Field, { name: "input", children: ({ error, value, onChange }) => (_jsx(LakeLabel, { label: label, render: id => (_jsx(LakeTextInput, { id: id, error: error, style: styles.input, placeholder: placeholder, value: value, onChangeText: onChange, onSubmitEditing: onSubmit })) })) }), _jsx(LakeButton, { size: "small", color: "current", onPress: onSubmit, children: submitText })] }) })] }));
186
186
  }
187
187
  function FilterBooleanTag({ children, onAdd, onPressRemove }) {
188
188
  useEffect(onAdd, []); // eslint-disable-line react-hooks/exhaustive-deps
@@ -635,7 +635,7 @@ export const FixedListView = ({ data: originalData, mode = "tile", keyExtractor,
635
635
  }
636
636
  }
637
637
  }, [viewId]);
638
- useEffect(() => {
638
+ useLayoutEffect(() => {
639
639
  const renderedRangeStartIndex = Math.max(0, Math.floor((currentScrollY.current - renderThreshold) / totalRowHeight));
640
640
  const renderedRangeEndIndex = Math.min(originalData.length, renderedRangeStartIndex +
641
641
  Math.ceil((lastKnownHeight.current + renderThreshold * 2) / totalRowHeight));
@@ -1,3 +1,3 @@
1
1
  import { AsyncData, Future, Result } from "@swan-io/boxed";
2
2
  import { AnyVariables, CombinedError, OperationContext, TypedDocumentNode } from "urql";
3
- export declare const useUrqlMutation: <Data, Variables extends AnyVariables>(query: TypedDocumentNode<Data, Variables>) => readonly [AsyncData<Result<Data, Error>>, (input: Variables, context?: Partial<OperationContext>) => Future<Result<Data, Error | CombinedError>>];
3
+ export declare const useUrqlMutation: <Data, Variables extends AnyVariables>(query: TypedDocumentNode<Data, Variables>) => readonly [AsyncData<Result<Data, CombinedError>>, (variables: Variables, context?: Partial<OperationContext>) => Future<Result<Data, CombinedError>>];
@@ -23,8 +23,8 @@ export const useUrqlMutation = (query) => {
23
23
  }
24
24
  return AsyncData.Done(toResult({ data, error }));
25
25
  }, [fetching, data, error]),
26
- useCallback((input, context) => Future.fromPromise(execute(input, context))
27
- .mapError(error => error) // Only used to cast error
26
+ useCallback((variables, context) => Future.fromPromise(execute(variables, context))
27
+ .mapError(error => error)
28
28
  .mapOkToResult(toResult), [execute]),
29
29
  ];
30
30
  };
@@ -1,19 +1,19 @@
1
1
  import { AsyncData, Result } from "@swan-io/boxed";
2
2
  import { DependencyList } from "react";
3
- import { AnyVariables, UseQueryArgs } from "urql";
3
+ import { AnyVariables, CombinedError, UseQueryArgs } from "urql";
4
4
  type Query<Data> = {
5
- data: AsyncData<Result<Data, Error>>;
6
- nextData: AsyncData<Result<Data, Error>>;
7
5
  isForceReloading: boolean;
6
+ data: AsyncData<Result<Data, CombinedError>>;
7
+ nextData: AsyncData<Result<Data, CombinedError>>;
8
8
  reload: () => void;
9
9
  };
10
- export declare const useUrqlQuery: <Data, Variables extends AnyVariables>(args: UseQueryArgs<Variables, Data>, dependencyList: DependencyList) => Query<Data>;
10
+ export declare const useUrqlQuery: <Data, Variables extends AnyVariables>(args: UseQueryArgs<Variables, Data>, dependencyList?: DependencyList) => Query<Data>;
11
11
  type PaginatedQuery<Data> = {
12
- data: AsyncData<Result<Data, Error>>;
13
- nextData: AsyncData<Result<Data, Error>>;
14
12
  isForceReloading: boolean;
13
+ data: AsyncData<Result<Data, CombinedError>>;
14
+ nextData: AsyncData<Result<Data, CombinedError>>;
15
15
  reload: () => void;
16
16
  setAfter: (cursor: string | undefined) => void;
17
17
  };
18
- export declare const useUrqlPaginatedQuery: <Data, Variables extends AnyVariables>(args: UseQueryArgs<Variables, Data>, dependencyList: DependencyList) => PaginatedQuery<Data>;
18
+ export declare const useUrqlPaginatedQuery: <Data, Variables extends AnyVariables>(args: UseQueryArgs<Variables, Data>, dependencyList?: DependencyList) => PaginatedQuery<Data>;
19
19
  export {};
@@ -2,72 +2,82 @@ import { AsyncData, Result } from "@swan-io/boxed";
2
2
  import { useCallback, useEffect, useMemo, useState } from "react";
3
3
  import { useQuery } from "urql";
4
4
  import { isNotNullish, isNullish } from "../utils/nullish";
5
- const NO_SUSPENSE_CONTEXT = { suspense: false };
6
- export const useUrqlQuery = (args, dependencyList) => {
7
- const [haveParamsChanged, setHaveParamsChanged] = useState(false);
5
+ const EMPTY_DEPENDENCY_LIST = [];
6
+ export const useUrqlQuery = (args, dependencyList = EMPTY_DEPENDENCY_LIST) => {
7
+ const hasDependencyList = dependencyList !== EMPTY_DEPENDENCY_LIST;
8
+ const [isDepsListUpdate, setIsDepsListUpdate] = useState(hasDependencyList);
8
9
  const [isForceReloading, setIsForceReloading] = useState(false);
9
10
  const [{ data, fetching, error }, reexecute] = useQuery({
10
11
  ...args,
11
- context: NO_SUSPENSE_CONTEXT,
12
+ context: useMemo(() => ({ ...args.context, suspense: false }), [args.context]),
12
13
  });
13
- const reload = useCallback(() => {
14
- setIsForceReloading(true);
15
- reexecute({ requestPolicy: "network-only" });
16
- }, [reexecute]);
17
14
  useEffect(() => {
18
- setHaveParamsChanged(true);
15
+ if (hasDependencyList) {
16
+ setIsDepsListUpdate(true);
17
+ }
19
18
  // eslint-disable-next-line react-hooks/exhaustive-deps
20
19
  }, dependencyList);
21
20
  useEffect(() => {
22
21
  if (!fetching) {
23
- setHaveParamsChanged(false);
22
+ setIsDepsListUpdate(false);
24
23
  setIsForceReloading(false);
25
24
  }
26
25
  }, [fetching]);
27
- const okResult = useMemo(() => AsyncData.Done(Result.Ok(data)), [data]);
28
- const errorResult = useMemo(() => AsyncData.Done(Result.Error(error)), [error]);
29
- const shouldResetState = isForceReloading || haveParamsChanged;
30
- if (((isNullish(data) && isNullish(error)) || shouldResetState) && fetching) {
26
+ const reload = useCallback(() => {
27
+ setIsForceReloading(true);
28
+ reexecute({ requestPolicy: "network-only" });
29
+ }, [reexecute]);
30
+ const okResult = useMemo(() => (isNullish(data) ? null : AsyncData.Done(Result.Ok(data))), [data]);
31
+ const errorResult = useMemo(() => (isNullish(error) ? null : AsyncData.Done(Result.Error(error))), [error]);
32
+ const initialFetching = isNullish(okResult) && isNullish(errorResult);
33
+ const shouldResetState = isDepsListUpdate || isForceReloading;
34
+ if (fetching && (initialFetching || shouldResetState)) {
31
35
  return {
36
+ isForceReloading,
32
37
  data: AsyncData.Loading(),
33
38
  nextData: AsyncData.Loading(),
34
- isForceReloading,
35
39
  reload,
36
40
  };
37
41
  }
38
- if (isNotNullish(error)) {
42
+ if (isNotNullish(errorResult)) {
39
43
  return {
44
+ isForceReloading,
40
45
  data: errorResult,
41
46
  nextData: errorResult,
42
- isForceReloading,
43
47
  reload,
44
48
  };
45
49
  }
46
- if (isNotNullish(data)) {
50
+ if (isNotNullish(okResult)) {
47
51
  return {
52
+ isForceReloading,
48
53
  data: okResult,
49
54
  nextData: fetching ? AsyncData.Loading() : okResult,
50
- isForceReloading,
51
55
  reload,
52
56
  };
53
57
  }
54
58
  return {
59
+ isForceReloading,
55
60
  data: AsyncData.NotAsked(),
56
61
  nextData: AsyncData.NotAsked(),
57
- isForceReloading,
58
62
  reload,
59
63
  };
60
64
  };
61
- export const useUrqlPaginatedQuery = (args, dependencyList) => {
62
- const [after, setAfter] = useState(undefined);
63
- const { data, nextData, isForceReloading, reload: originalReload, } = useUrqlQuery({ ...args, variables: { ...args.variables, after } }, dependencyList);
65
+ export const useUrqlPaginatedQuery = (args, dependencyList = EMPTY_DEPENDENCY_LIST) => {
66
+ const [after, setAfter] = useState();
67
+ const { isForceReloading, data, nextData, reload: baseReload, } = useUrqlQuery({ ...args, variables: { ...args.variables, after } }, dependencyList);
64
68
  useEffect(() => {
65
69
  setAfter(undefined);
66
70
  // eslint-disable-next-line react-hooks/exhaustive-deps
67
71
  }, dependencyList);
68
72
  const reload = useCallback(() => {
69
73
  setAfter(undefined);
70
- originalReload();
71
- }, [originalReload]);
72
- return { data, nextData, isForceReloading, reload, setAfter };
74
+ baseReload();
75
+ }, [baseReload]);
76
+ return {
77
+ isForceReloading,
78
+ data,
79
+ nextData,
80
+ reload,
81
+ setAfter,
82
+ };
73
83
  };
@@ -3,4 +3,5 @@ export declare const noop: () => void;
3
3
  export declare const stubFalse: () => false;
4
4
  export declare const stubTrue: () => true;
5
5
  export declare const unionToArray: <T extends PropertyKey>(object: Record<T, true>) => T[];
6
+ export declare const getUnionGuard: <T extends PropertyKey>(object: Record<T, true>) => (value: unknown) => value is T;
6
7
  export declare const memoize: <Input extends unknown[], Output>(func: (...input: Input) => Output, getCacheKey: (...input: Input) => string) => (...input: Input) => Output;
@@ -4,6 +4,10 @@ export const noop = () => { };
4
4
  export const stubFalse = () => false;
5
5
  export const stubTrue = () => true;
6
6
  export const unionToArray = (object) => Dict.keys(object);
7
+ export const getUnionGuard = (object) => {
8
+ const set = new Set(unionToArray(object));
9
+ return (value) => set.has(value);
10
+ };
7
11
  export const memoize = (func, getCacheKey) => {
8
12
  const cache = new Map();
9
13
  return (...input) => {