@swan-io/lake 4.5.2 → 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.5.2",
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
@@ -280,11 +280,14 @@ const styles = StyleSheet.create({
280
280
  left: 0,
281
281
  right: 0,
282
282
  bottom: 0,
283
+ backgroundColor: backgroundColor.default,
284
+ },
285
+ emptyListContentContainer: {
283
286
  flexDirection: "column",
284
287
  alignItems: "center",
285
288
  justifyContent: "center",
286
289
  padding: spacings[48],
287
- backgroundColor: backgroundColor.default,
290
+ minHeight: "100%",
288
291
  },
289
292
  emptyList: {
290
293
  flexDirection: "column",
@@ -632,7 +635,7 @@ export const FixedListView = ({ data: originalData, mode = "tile", keyExtractor,
632
635
  }
633
636
  }
634
637
  }, [viewId]);
635
- useEffect(() => {
638
+ useLayoutEffect(() => {
636
639
  const renderedRangeStartIndex = Math.max(0, Math.floor((currentScrollY.current - renderThreshold) / totalRowHeight));
637
640
  const renderedRangeEndIndex = Math.min(originalData.length, renderedRangeStartIndex +
638
641
  Math.ceil((lastKnownHeight.current + renderThreshold * 2) / totalRowHeight));
@@ -808,7 +811,7 @@ export const FixedListView = ({ data: originalData, mode = "tile", keyExtractor,
808
811
  ], children: [_jsx(View, { style: [
809
812
  styles.stickyColumnEndOverflow,
810
813
  { width: horizontalPadding, backgroundColor: headerBackgroundColor },
811
- ] }), _jsx(HeaderSegment, { columns: stickedToEndColumns, extraInfo: extraInfo, viewId: viewId, width: stickedToEndColumnsWidth }), _jsx(View, { style: [styles.topGradient, isScrolled && styles.visibleTopGradient] })] }), _jsx(View, { style: { height: rowsHeight }, children: endRows })] })) : null] })] }), data.length === 0 && isNotNullish(renderEmptyList) && !isLoading ? (_jsx(View, { style: styles.emptyListContainer, children: renderEmptyList() })) : null, _jsx(View, { ref: endFocusAnchorRef, tabIndex: 0 })] }));
814
+ ] }), _jsx(HeaderSegment, { columns: stickedToEndColumns, extraInfo: extraInfo, viewId: viewId, width: stickedToEndColumnsWidth }), _jsx(View, { style: [styles.topGradient, isScrolled && styles.visibleTopGradient] })] }), _jsx(View, { style: { height: rowsHeight }, children: endRows })] })) : null] })] }), data.length === 0 && isNotNullish(renderEmptyList) && !isLoading ? (_jsx(ScrollView, { style: styles.emptyListContainer, contentContainerStyle: styles.emptyListContentContainer, children: renderEmptyList() })) : null, _jsx(View, { ref: endFocusAnchorRef, tabIndex: 0 })] }));
812
815
  };
813
816
  export const FixedListViewPlaceholder = ({ count, rowHeight, rowVerticalSpacing, groupHeaderHeight, headerHeight, paddingHorizontal = HORIZONTAL_SAFE_AREA, }) => {
814
817
  const totalRowHeight = rowHeight + rowVerticalSpacing;
@@ -7,6 +7,7 @@ export type LakeTagInputProps = Merge<TextInputProps, {
7
7
  disabled?: boolean;
8
8
  valid?: boolean;
9
9
  hideErrors?: boolean;
10
+ validateOnBlur?: boolean;
10
11
  help?: string;
11
12
  validator?: (value: string) => boolean;
12
13
  values: string[];
@@ -211,6 +212,7 @@ export declare const LakeTagInput: import("react").ForwardRefExoticComponent<{
211
212
  disabled?: boolean | undefined;
212
213
  valid?: boolean | undefined;
213
214
  hideErrors?: boolean | undefined;
215
+ validateOnBlur?: boolean | undefined;
214
216
  help?: string | undefined;
215
217
  validator?: ((value: string) => boolean) | undefined;
216
218
  placeholder?: string | undefined;
@@ -77,7 +77,7 @@ const styles = StyleSheet.create({
77
77
  },
78
78
  });
79
79
  const SEPARATORS_REGEX = /,| /;
80
- export const LakeTagInput = forwardRef(({ validator = () => true, onFocus: originalOnFocus, onBlur: originalOnBlur, values, onValuesChanged, readOnly = false, disabled = false, valid = false, hideErrors = false, placeholder, help, error, }, forwardRef) => {
80
+ export const LakeTagInput = forwardRef(({ validator = () => true, onFocus: originalOnFocus, onBlur: originalOnBlur, validateOnBlur = true, values, onValuesChanged, readOnly = false, disabled = false, valid = false, hideErrors = false, placeholder, help, error, }, forwardRef) => {
81
81
  const inputRef = useRef(null);
82
82
  const containerRef = useRef(null);
83
83
  const [isFocused, setIsFocused] = useState(false);
@@ -97,6 +97,9 @@ export const LakeTagInput = forwardRef(({ validator = () => true, onFocus: origi
97
97
  }
98
98
  }, [pushNewValues]);
99
99
  const onTextInputKeyPress = useCallback(({ nativeEvent }) => {
100
+ if (disabled || readOnly) {
101
+ return;
102
+ }
100
103
  match({ key: nativeEvent.key, input: inputRef.current })
101
104
  .with({ key: "Backspace", input: P.instanceOf(HTMLInputElement) }, ({ input }) => {
102
105
  if (isNullishOrEmpty(input.value)) {
@@ -108,7 +111,7 @@ export const LakeTagInput = forwardRef(({ validator = () => true, onFocus: origi
108
111
  pushNewValues([input.value]);
109
112
  }
110
113
  });
111
- }, [onValuesChanged, pushNewValues, values]);
114
+ }, [onValuesChanged, pushNewValues, values, disabled, readOnly]);
112
115
  const autoFocus = useCallback(() => {
113
116
  inputRef.current?.focus();
114
117
  }, []);
@@ -117,9 +120,15 @@ export const LakeTagInput = forwardRef(({ validator = () => true, onFocus: origi
117
120
  originalOnFocus?.(event);
118
121
  }, [originalOnFocus]);
119
122
  const onBlur = useCallback((event) => {
123
+ const input = inputRef.current;
124
+ if (input instanceof HTMLInputElement &&
125
+ isNotNullishOrEmpty(input.value) &&
126
+ validateOnBlur) {
127
+ pushNewValues([input.value]);
128
+ }
120
129
  setIsFocused(false);
121
130
  originalOnBlur?.(event);
122
- }, [originalOnBlur]);
131
+ }, [pushNewValues, originalOnBlur, validateOnBlur]);
123
132
  const mergedRef = useMergeRefs(inputRef, forwardRef);
124
133
  const hasError = isNotNullishOrEmpty(error);
125
134
  return (_jsxs(View, { children: [_jsxs(Pressable, { style: [
@@ -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) => {