@swan-io/lake 11.1.7 → 11.1.8

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": "11.1.7",
3
+ "version": "11.1.8",
4
4
  "engines": {
5
5
  "node": ">=20.9.0",
6
6
  "pnpm": "^9.13.0"
@@ -196,7 +196,7 @@ export const MultiSelect = ({ color = "gray", disabled = false, emptyResultText,
196
196
  }, [filteredItems]);
197
197
  const ListHeaderComponent = useMemo(() => (_jsxs(Box, { direction: "row", alignItems: "center", style: styles.filterContainer, children: [_jsx(TextInput, { autoComplete: "off", inputMode: "search", multiline: false, rows: 1, onChangeText: filterValue => setFilter(filterValue), placeholder: filterPlaceholder, value: filter, onFocus: setFilterFocused.on, onBlur: setFilterFocused.off, style: [styles.filterInput, filterFocused && styles.filterFocused] }), _jsx(Icon, { name: "search-filled", color: colors[color].primary, size: 20, style: styles.searchIcon })] })), [filter, filterFocused, setFilterFocused, filterPlaceholder, color]);
198
198
  const ListEmptyComponent = useMemo(() => (_jsxs(Box, { justifyContent: "center", alignItems: "center", style: styles.emptyList, children: [_jsx(Icon, { name: "clipboard-search-regular", size: 24, color: colors.gray.primary }), isNotNullishOrEmpty(emptyResultText) && (_jsxs(_Fragment, { children: [_jsx(Space, { height: 8 }), _jsx(Text, { style: styles.emptyListText, children: emptyResultText })] }))] })), [emptyResultText]);
199
- return (_jsxs(View, { style: style, children: [_jsxs(Pressable, { id: id, ref: inputRef, "aria-haspopup": "listbox", role: "button", "aria-expanded": visible, disabled: disabled, onPress: open, style: ({ hovered, focused }) => [
199
+ return (_jsxs(View, { style: style, children: [_jsxs(Pressable, { id: id, ref: inputRef, "aria-haspopup": "listbox", "aria-expanded": visible, disabled: disabled, onPress: open, style: ({ hovered, focused }) => [
200
200
  styles.base,
201
201
  hovered && styles.hovered,
202
202
  (focused || visible) && styles.focused,
@@ -1 +1 @@
1
- export declare const usePersistedState: <T>(key: string, defaultValue: T) => readonly [T, (state: T) => void];
1
+ export declare const usePersistedState: <T>(key: string, defaultValue: T) => readonly [T, (value: T | null) => void];
@@ -1,21 +1,55 @@
1
1
  import { Option, Result } from "@swan-io/boxed";
2
- import { useCallback, useState } from "react";
2
+ import { useCallback, useEffect, useState } from "react";
3
+ const getItem = (key) => Result.fromExecution(() => localStorage.getItem(key)).getOr(null);
4
+ const parseRawValue = (rawValue, defaultValue) => Result.fromExecution(() => (rawValue != null ? JSON.parse(rawValue) : rawValue))
5
+ .toOption()
6
+ .flatMap(Option.fromNullable)
7
+ .getOr(defaultValue);
3
8
  export const usePersistedState = (key, defaultValue) => {
4
9
  const [state, setState] = useState(() => {
5
- return Result.fromExecution(() => localStorage.getItem(key))
6
- .toOption()
7
- .flatMap(Option.fromNullable)
8
- .map(value => JSON.parse(value))
9
- .getOr(defaultValue);
10
+ const rawValue = getItem(key);
11
+ const value = parseRawValue(rawValue, defaultValue);
12
+ return { defaultValue, value, rawValue };
10
13
  });
11
- const setPersistedState = useCallback((state) => {
12
- setState(state);
14
+ const updateRawValue = useCallback((rawValue) => {
15
+ setState(prevState => {
16
+ if (rawValue === prevState.rawValue) {
17
+ return prevState; // skip update if rawValue didn't changed
18
+ }
19
+ else {
20
+ const { defaultValue } = prevState;
21
+ const value = parseRawValue(rawValue, defaultValue);
22
+ return { defaultValue, value, rawValue };
23
+ }
24
+ });
25
+ }, []);
26
+ const setPersistedState = useCallback((value) => {
13
27
  try {
14
- localStorage.setItem(key, JSON.stringify(state));
28
+ if (value != null) {
29
+ const rawValue = JSON.stringify(value);
30
+ updateRawValue(rawValue);
31
+ localStorage.setItem(key, rawValue);
32
+ }
33
+ else {
34
+ updateRawValue(null);
35
+ localStorage.removeItem(key);
36
+ }
15
37
  }
16
38
  catch {
17
39
  // ignore
18
40
  }
19
- }, [key]);
20
- return [state, setPersistedState];
41
+ }, [key, updateRawValue]);
42
+ useEffect(() => {
43
+ const listener = (event) => {
44
+ if (event.storageArea === localStorage && (event.key === key || event.key === null)) {
45
+ const rawValue = getItem(key);
46
+ updateRawValue(rawValue);
47
+ }
48
+ };
49
+ window.addEventListener("storage", listener);
50
+ return () => {
51
+ window.removeEventListener("storage", listener);
52
+ };
53
+ }, [key, updateRawValue]);
54
+ return [state.value, setPersistedState];
21
55
  };