@swan-io/lake 10.0.1 → 11.0.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.
@@ -1,59 +0,0 @@
1
- import { DatePickerDate } from "@swan-io/shared-business/src/components/DatePicker";
2
- import { DateFormat } from "@swan-io/shared-business/src/utils/i18n";
3
- import { ValidatorResult } from "@swan-io/use-form";
4
- import { Simplify } from "type-fest";
5
- type Item<T> = {
6
- label: string;
7
- value: T;
8
- };
9
- export type FilterCheckboxDef<T> = {
10
- type: "checkbox";
11
- label: string;
12
- items: Item<T>[];
13
- width?: number;
14
- checkAllLabel?: string;
15
- };
16
- export type FilterRadioDef<T> = {
17
- type: "radio";
18
- label: string;
19
- items: Item<T>[];
20
- width?: number;
21
- };
22
- export type FilterDateDef<Values = unknown> = {
23
- type: "date";
24
- label: string;
25
- cancelText: string;
26
- submitText: string;
27
- noValueText: string;
28
- dateFormat: DateFormat;
29
- isSelectable?: (date: DatePickerDate, filters: Values) => boolean;
30
- validate?: (value: string, filters: Values) => ValidatorResult;
31
- };
32
- export type FilterInputDef = {
33
- type: "input";
34
- label: string;
35
- noValueText: string;
36
- placeholder?: string;
37
- validate?: (value: string) => ValidatorResult;
38
- };
39
- type Filter<T> = FilterCheckboxDef<T> | FilterRadioDef<T> | FilterDateDef | FilterInputDef;
40
- type ExtractFilterValue<T extends Filter<unknown>> = T extends {
41
- type: "checkbox";
42
- } ? T["items"][number]["value"][] | undefined : T extends {
43
- type: "radio";
44
- } ? T["items"][number]["value"] | undefined : T extends {
45
- type: "boolean";
46
- } ? boolean | undefined : string | undefined;
47
- type FiltersDefinition = Record<string, Filter<unknown>>;
48
- export type FiltersState<T extends FiltersDefinition> = Simplify<{
49
- [K in keyof T]: Simplify<ExtractFilterValue<T[K]>>;
50
- }>;
51
- type FiltersStackProps<Definition extends FiltersDefinition, State extends FiltersState<Definition> = FiltersState<Definition>> = {
52
- definition: Definition;
53
- filters: State;
54
- openedFilters: (keyof Definition)[];
55
- onChangeOpened: (value: (keyof Definition)[]) => void;
56
- onChangeFilters: (value: State) => void;
57
- };
58
- export declare const FiltersStack: <T extends FiltersDefinition>({ filters, openedFilters, definition, onChangeOpened, onChangeFilters, }: FiltersStackProps<T>) => import("react/jsx-runtime").JSX.Element | null;
59
- export {};
@@ -1,208 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { DatePickerModal, } from "@swan-io/shared-business/src/components/DatePicker";
3
- import dayjs from "dayjs";
4
- import { forwardRef, useCallback, useMemo, useRef, useState } from "react";
5
- import { Pressable, StyleSheet, Text, View } from "react-native";
6
- import { P, match } from "ts-pattern";
7
- import { colors, shadows } from "../constants/design";
8
- import { useDisclosure } from "../hooks/useDisclosure";
9
- import { useMergeRefs } from "../hooks/useMergeRefs";
10
- import { usePreviousValue } from "../hooks/usePreviousValue";
11
- import { isNotNullish, isNullish } from "../utils/nullish";
12
- import { Box } from "./Box";
13
- import { FlatList } from "./FlatList";
14
- import { Icon } from "./Icon";
15
- import { LakeCheckbox } from "./LakeCheckbox";
16
- import { LakeLabel } from "./LakeLabel";
17
- import { LakeRadio } from "./LakeRadio";
18
- import { LakeTextInput } from "./LakeTextInput";
19
- import { Popover } from "./Popover";
20
- import { Space } from "./Space";
21
- import { Stack } from "./Stack";
22
- import { Tag } from "./Tag";
23
- const styles = StyleSheet.create({
24
- container: {
25
- paddingRight: 12,
26
- paddingBottom: 8,
27
- },
28
- shadowed: {
29
- position: "absolute",
30
- opacity: 0,
31
- width: "100%",
32
- height: "100%",
33
- borderRadius: 4,
34
- boxShadow: shadows.tile,
35
- transitionDuration: "150ms",
36
- transitionProperty: "opacity",
37
- },
38
- hovered: {
39
- opacity: 1,
40
- },
41
- dropdown: {
42
- maxHeight: 400,
43
- minWidth: 200,
44
- },
45
- itemHovered: {
46
- backgroundColor: colors.gray[50],
47
- },
48
- content: {
49
- paddingVertical: 12,
50
- },
51
- inputContent: {
52
- paddingHorizontal: 24,
53
- paddingTop: 24,
54
- paddingBottom: 16,
55
- },
56
- radio: {
57
- display: "flex",
58
- flexDirection: "row",
59
- alignItems: "center",
60
- height: 32,
61
- paddingHorizontal: 24,
62
- },
63
- itemLabel: {
64
- textOverflow: "ellipsis",
65
- overflow: "hidden",
66
- whiteSpace: "nowrap",
67
- },
68
- input: {
69
- width: 250,
70
- },
71
- value: {
72
- maxWidth: 130,
73
- whiteSpace: "nowrap",
74
- },
75
- });
76
- const FilterTag = forwardRef(({ onPress, onPressRemove, label, value = "", isActive }, forwardRef) => {
77
- const ref = useRef(null);
78
- const mergedRef = useMergeRefs(ref, forwardRef);
79
- return (_jsx(Pressable, { ref: mergedRef, onPress: onPress, children: ({ hovered }) => (_jsxs(_Fragment, { children: [_jsx(View, { style: [styles.shadowed, hovered && styles.hovered] }), _jsx(Tag, { label: label, color: "current", onPressRemove: onPressRemove, children: _jsxs(Box, { direction: "row", alignItems: "center", children: [_jsx(Text, { numberOfLines: 1, style: styles.value, children: value }), _jsx(Space, { width: 4 }), _jsx(Icon, { color: colors.current.primary, name: isActive ? "chevron-up-filled" : "chevron-down-filled", size: 16 })] }) })] })) }));
80
- });
81
- function FilterRadio({ label, items, width, value, onValueChange, onPressRemove, autoOpen = false, }) {
82
- const inputRef = useRef(null);
83
- const [visible, { close, toggle }] = useDisclosure(autoOpen);
84
- const currentValue = useMemo(() => items.find(i => i.value === value), [items, value]);
85
- return (_jsxs(View, { style: styles.container, children: [_jsx(FilterTag, { label: label, onPress: toggle, ref: inputRef, onPressRemove: onPressRemove, isActive: visible, value: currentValue === null || currentValue === void 0 ? void 0 : currentValue.label }), _jsx(Popover, { role: "listbox", matchReferenceWidth: false, onDismiss: close, referenceRef: inputRef, returnFocus: false, visible: visible, children: _jsx(FlatList, { role: "list", data: items, style: [styles.dropdown, { width }], contentContainerStyle: styles.content, keyExtractor: (_, index) => `filter-item-${index}`, renderItem: ({ item }) => {
86
- const isSelected = value === item.value;
87
- return (_jsxs(Pressable, { role: "radio", "aria-checked": isSelected, style: ({ hovered }) => [styles.radio, hovered && styles.itemHovered], onPress: () => {
88
- onValueChange(item.value);
89
- close();
90
- }, children: [_jsx(LakeRadio, { value: isSelected }), _jsx(Space, { width: 12 }), _jsx(Text, { style: styles.itemLabel, children: item.label })] }));
91
- } }) })] }));
92
- }
93
- function FilterCheckbox({ label, items, width, checkAllLabel, value, onValueChange, onPressRemove, autoOpen = false, }) {
94
- const inputRef = useRef(null);
95
- const [visible, { close, toggle }] = useDisclosure(autoOpen);
96
- const valueSet = useMemo(() => new Set(value), [value]);
97
- const currentValue = useMemo(() => items.filter(item => valueSet.has(item.value)), [items, valueSet]);
98
- const allChecked = checkAllLabel != null && valueSet.size === items.length;
99
- const listItems = useMemo(() => {
100
- if (checkAllLabel == null) {
101
- return items;
102
- }
103
- const checked = valueSet.size === 0 ? false : valueSet.size === items.length ? true : "mixed";
104
- const checkAllItem = {
105
- label: checkAllLabel,
106
- checked,
107
- };
108
- return [checkAllItem, ...items];
109
- }, [items, checkAllLabel, valueSet]);
110
- return (_jsxs(View, { style: styles.container, children: [_jsx(FilterTag, { label: label, onPress: toggle, ref: inputRef, onPressRemove: onPressRemove, isActive: visible, value: allChecked ? checkAllLabel : currentValue.map(item => item.label).join(", ") }), _jsx(Popover, { role: "listbox", matchReferenceWidth: false, onDismiss: close, referenceRef: inputRef, returnFocus: false, visible: visible, children: _jsx(FlatList, { role: "list", data: listItems, style: [styles.dropdown, { width }], contentContainerStyle: styles.content, keyExtractor: (_, index) => `filter-item-${index}`, renderItem: ({ item }) => {
111
- const isSelected = match(item)
112
- .with({ checked: P.any }, ({ checked }) => checked)
113
- .with({ value: P.any }, ({ value }) => valueSet.has(value))
114
- .exhaustive();
115
- const onPress = match(item)
116
- // Check all item
117
- .with({ checked: P.any }, ({ checked }) => () => {
118
- if (checked === true) {
119
- onValueChange(undefined);
120
- }
121
- else {
122
- onValueChange(items.map(item => item.value));
123
- }
124
- })
125
- // Regular item
126
- .with({ value: P.any }, ({ value }) => () => {
127
- const nextValues = new Set([...valueSet]);
128
- if (isSelected === true) {
129
- nextValues.delete(value);
130
- }
131
- else {
132
- nextValues.add(value);
133
- }
134
- if (nextValues.size === 0) {
135
- onValueChange(undefined);
136
- }
137
- else {
138
- onValueChange([...nextValues]);
139
- }
140
- })
141
- .exhaustive();
142
- return (_jsxs(Pressable, { role: "radio", "aria-checked": isSelected, style: ({ hovered }) => [styles.radio, hovered && styles.itemHovered], onPress: onPress, children: [_jsx(LakeCheckbox, { value: isSelected }), _jsx(Space, { width: 12 }), _jsx(Text, { style: styles.itemLabel, children: item.label })] }));
143
- } }) })] }));
144
- }
145
- function FilterDate({ label, initialValue, noValueText, cancelText, submitText, dateFormat, isSelectable, validate, onValueChange, onPressRemove, autoOpen = false, }) {
146
- const inputRef = useRef(null);
147
- const [visible, { close, toggle }] = useDisclosure(autoOpen);
148
- const value = useMemo(() => (isNotNullish(initialValue) ? dayjs(initialValue).format(dateFormat) : ""), [initialValue, dateFormat]);
149
- return (_jsxs(View, { style: styles.container, children: [_jsx(FilterTag, { label: label, onPress: toggle, ref: inputRef, onPressRemove: onPressRemove, isActive: visible, value: isNotNullish(initialValue) ? dayjs(initialValue).format(dateFormat) : noValueText }), _jsx(DatePickerModal, { visible: visible, format: dateFormat, firstWeekDay: "monday", label: label, cancelLabel: cancelText, confirmLabel: submitText, value: value, isSelectable: isSelectable, validate: validate, onChange: value => {
150
- const formattedValue = dayjs(value, dateFormat, true).toJSON();
151
- onValueChange(formattedValue);
152
- }, onDismiss: close })] }));
153
- }
154
- function FilterInput({ label, initialValue = "", noValueText, autoOpen = false, placeholder, validate, onValueChange, onPressRemove, }) {
155
- const [visible, { close, toggle }] = useDisclosure(autoOpen);
156
- const tagRef = useRef(null);
157
- const getValueState = useCallback((inputValue, isInitialValue) => {
158
- var _a;
159
- const trimmed = inputValue.trim();
160
- const error = (_a = validate === null || validate === void 0 ? void 0 : validate(trimmed)) !== null && _a !== void 0 ? _a : undefined;
161
- const validValue = isNullish(error) ? trimmed : undefined;
162
- if (!isInitialValue) {
163
- onValueChange(validValue);
164
- }
165
- return {
166
- inputValue,
167
- validValue,
168
- error: isInitialValue ? undefined : error,
169
- };
170
- }, [validate, onValueChange]);
171
- const [{ inputValue, validValue, error }, setState] = useState(() => getValueState(initialValue, true));
172
- const onChangeText = useCallback((value) => setState(getValueState(value, false)), [getValueState]);
173
- return (_jsxs(View, { style: styles.container, children: [_jsx(FilterTag, { label: label, onPress: toggle, ref: tagRef, onPressRemove: onPressRemove, isActive: visible, value: validValue !== null && validValue !== void 0 ? validValue : noValueText }), _jsx(Popover, { role: "listbox", matchReferenceWidth: false, onDismiss: close, referenceRef: tagRef, returnFocus: false, visible: visible, children: _jsx(View, { style: [styles.dropdown, styles.inputContent], children: _jsx(LakeLabel, { label: label, render: id => (_jsx(LakeTextInput, { id: id, value: inputValue, error: error, style: styles.input, placeholder: placeholder, onChangeText: onChangeText })) }) }) })] }));
174
- }
175
- const getFilterValue = (_type, filters, name) => filters[name];
176
- export const FiltersStack = ({ filters, openedFilters, definition, onChangeOpened, onChangeFilters, }) => {
177
- const previousOpened = usePreviousValue(openedFilters);
178
- const lastOpenedFilter = openedFilters.length > previousOpened.length
179
- ? openedFilters[openedFilters.length - 1]
180
- : undefined;
181
- if (openedFilters.length === 0) {
182
- return null;
183
- }
184
- return (_jsx(Stack, { direction: "row", wrap: "wrap", children: openedFilters.map(filterName => {
185
- const filterDefinition = definition[filterName];
186
- if (typeof filterName !== "string" || filterDefinition == null) {
187
- return null;
188
- }
189
- return (_jsx(View, { children: match(filterDefinition)
190
- .with({ type: "radio" }, ({ type, label, items, width }) => (_jsx(FilterRadio, { label: label, items: items, width: width, autoOpen: lastOpenedFilter === filterName, onPressRemove: () => {
191
- onChangeFilters({ ...filters, [filterName]: undefined });
192
- onChangeOpened(openedFilters.filter(f => f !== filterName));
193
- }, value: getFilterValue(type, filters, filterName), onValueChange: value => onChangeFilters({ ...filters, [filterName]: value }) })))
194
- .with({ type: "checkbox" }, ({ type, label, items, width, checkAllLabel }) => (_jsx(FilterCheckbox, { label: label, items: items, width: width, checkAllLabel: checkAllLabel, autoOpen: lastOpenedFilter === filterName, value: getFilterValue(type, filters, filterName), onValueChange: value => onChangeFilters({ ...filters, [filterName]: value }), onPressRemove: () => {
195
- onChangeFilters({ ...filters, [filterName]: undefined });
196
- onChangeOpened(openedFilters.filter(f => f !== filterName));
197
- } })))
198
- .with({ type: "date" }, ({ type, label, noValueText, cancelText, submitText, dateFormat, isSelectable, validate, }) => (_jsx(FilterDate, { label: label, noValueText: noValueText, cancelText: cancelText, submitText: submitText, dateFormat: dateFormat, autoOpen: lastOpenedFilter === filterName, isSelectable: isSelectable ? date => isSelectable(date, filters) : undefined, validate: validate ? value => validate(value, filters) : undefined, initialValue: getFilterValue(type, filters, filterName), onValueChange: value => onChangeFilters({ ...filters, [filterName]: value }), onPressRemove: () => {
199
- onChangeFilters({ ...filters, [filterName]: undefined });
200
- onChangeOpened(openedFilters.filter(f => f !== filterName));
201
- } })))
202
- .with({ type: "input" }, ({ type, label, placeholder, noValueText, validate }) => (_jsx(FilterInput, { label: label, placeholder: placeholder, noValueText: noValueText, autoOpen: lastOpenedFilter === filterName, validate: validate, initialValue: getFilterValue(type, filters, filterName), onValueChange: value => onChangeFilters({ ...filters, [filterName]: value }), onPressRemove: () => {
203
- onChangeFilters({ ...filters, [filterName]: undefined });
204
- onChangeOpened(openedFilters.filter(f => f !== filterName));
205
- } })))
206
- .exhaustive() }, filterName));
207
- }) }));
208
- };
@@ -1,8 +0,0 @@
1
- import { CountryCCA2 } from "@swan-io/shared-business/src/constants/countries";
2
- export type FlagCode = CountryCCA2 | "EU";
3
- type Props = {
4
- code: FlagCode;
5
- width?: number;
6
- };
7
- export declare const Flag: (props: Props) => import("react/jsx-runtime").JSX.Element;
8
- export {};
@@ -1,32 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { Lazy } from "@swan-io/boxed";
3
- import { useEffect, useMemo, useState } from "react";
4
- import { match } from "ts-pattern";
5
- import { getFlagGlyphName } from "../utils/string";
6
- import { Svg, Use } from "./Svg";
7
- const UNICODE_OFFSET = 127462 - 65;
8
- let svgUrl;
9
- const svgUrlGetter = Lazy(async () => {
10
- const { default: value } = await import("../assets/images/flags.svg");
11
- svgUrl = value;
12
- return value;
13
- });
14
- export const Flag = (props) => {
15
- var _a;
16
- const { code } = props;
17
- const width = (_a = props.width) !== null && _a !== void 0 ? _a : 18;
18
- const [url, setUrl] = useState(svgUrl);
19
- useEffect(() => {
20
- if (svgUrl == null) {
21
- void svgUrlGetter.get().then(setUrl);
22
- }
23
- }, []);
24
- const flag = useMemo(() => {
25
- return match(code)
26
- .with("EU", () => getFlagGlyphName("🇪🇺"))
27
- .otherwise(() => {
28
- return `${(UNICODE_OFFSET + code.charCodeAt(0)).toString(16)}-${(UNICODE_OFFSET + code.charCodeAt(1)).toString(16)}`;
29
- });
30
- }, [code]);
31
- return (_jsx(Svg, { viewBox: "0 0 18 18", style: { height: width, width }, children: url != null && flag != null ? _jsx(Use, { xlinkHref: `${url}#${flag}` }) : null }));
32
- };
@@ -1 +0,0 @@
1
- export declare const ToastStack: () => import("react/jsx-runtime").JSX.Element | null;
@@ -1,142 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { Array, Option } from "@swan-io/boxed";
3
- import { t } from "@swan-io/shared-business/src/utils/i18n";
4
- import { memo, useEffect, useRef, useState } from "react";
5
- import { StyleSheet, View } from "react-native";
6
- import { P, match } from "ts-pattern";
7
- import { animations, colors, shadows } from "../constants/design";
8
- import { getErrorToRequestId, hideToast, useToasts, } from "../state/toasts";
9
- import { setClipboardText } from "../utils/clipboard";
10
- import { isNotNullishOrEmpty, isNullish } from "../utils/nullish";
11
- import { Box } from "./Box";
12
- import { Icon } from "./Icon";
13
- import { LakeText } from "./LakeText";
14
- import { LakeTooltip } from "./LakeTooltip";
15
- import { Portal } from "./Portal";
16
- import { Pressable } from "./Pressable";
17
- import { Space } from "./Space";
18
- import { TransitionGroupView } from "./TransitionGroupView";
19
- const styles = StyleSheet.create({
20
- list: {
21
- position: "fixed",
22
- right: 0,
23
- bottom: 0,
24
- maxHeight: "100%",
25
- maxWidth: 400,
26
- paddingVertical: 8,
27
- width: "100%",
28
- zIndex: 10,
29
- pointerEvents: "none",
30
- },
31
- toastWrapper: {
32
- paddingHorizontal: 16,
33
- paddingVertical: 8,
34
- pointerEvents: "auto",
35
- },
36
- toast: {
37
- padding: 24,
38
- borderRadius: 4,
39
- borderWidth: 1,
40
- borderLeftWidth: 4,
41
- overflow: "hidden",
42
- boxShadow: shadows.modal,
43
- },
44
- contentContainer: {
45
- paddingRight: 36, // 24 for close button + 12 for spacing
46
- },
47
- closeButton: {
48
- zIndex: 1,
49
- position: "absolute",
50
- width: 24,
51
- height: 24,
52
- right: 24,
53
- top: 0,
54
- bottom: 0,
55
- margin: "auto",
56
- },
57
- progressBar: {
58
- height: 2,
59
- transformOrigin: "left",
60
- },
61
- copyTooltip: {
62
- alignSelf: "flex-start",
63
- },
64
- copyButton: {
65
- alignItems: "center",
66
- flexDirection: "row",
67
- flexGrow: 1,
68
- flexShrink: 1,
69
- },
70
- });
71
- const errorsToArray = (errors) => {
72
- const asArray = Array.isArray(errors) ? errors : [errors];
73
- return Array.filterMap(asArray, error => error instanceof Error ? Option.Some(error) : Option.None());
74
- };
75
- const Toast = memo(({ variant, uid, title, description, error, progress, onClose }) => {
76
- const progressBarRef = useRef(null);
77
- const [visibleState, setVisibleState] = useState("copy");
78
- const hasDescription = isNotNullishOrEmpty(description);
79
- const [requestId] = useState(() => {
80
- if (error == undefined) {
81
- return Option.None();
82
- }
83
- return Array.findMap(errorsToArray(error), error => Option.fromNullable(getErrorToRequestId().get(error)));
84
- });
85
- const colorVariation = match(variant)
86
- .returnType()
87
- .with("success", () => "positive")
88
- .with("error", () => "negative")
89
- .with("info", () => "shakespear")
90
- .with("warning", () => "warning")
91
- .exhaustive();
92
- useEffect(() => {
93
- if (isNullish(progress)) {
94
- return;
95
- }
96
- return progress.subscribe(value => {
97
- if (progressBarRef.current instanceof HTMLElement) {
98
- progressBarRef.current.style.transform = `scaleX(${value})`;
99
- }
100
- });
101
- }, [progress]);
102
- return (_jsx(View, { style: styles.toastWrapper, children: _jsxs(View, { style: [
103
- styles.toast,
104
- {
105
- borderColor: colors[colorVariation][200],
106
- borderLeftColor: colors[colorVariation][500],
107
- backgroundColor: colors[colorVariation][0],
108
- },
109
- ], children: [_jsxs(Box, { style: styles.contentContainer, children: [_jsxs(Box, { direction: "row", alignItems: "center", children: [match(variant)
110
- .with("success", () => (_jsx(Icon, { name: "checkmark-circle-regular", size: 20, color: colors[colorVariation][700] })))
111
- .with("error", () => (_jsx(Icon, { name: "dismiss-circle-regular", size: 20, color: colors[colorVariation][700] })))
112
- .with("info", () => (_jsx(Icon, { name: "info-regular", size: 20, color: colors[colorVariation][700] })))
113
- .with("warning", () => (_jsx(Icon, { name: "warning-regular", size: 20, color: colors[colorVariation][700] })))
114
- .exhaustive(), _jsx(Space, { width: 12 }), _jsx(LakeText, { variant: "regular", color: colors[colorVariation][700], children: title })] }), hasDescription && (_jsxs(_Fragment, { children: [_jsx(Space, { height: 8 }), _jsx(LakeText, { variant: "smallRegular", color: colors.gray[700], children: description })] })), match(requestId)
115
- .with(Option.P.None, () => null)
116
- .with(Option.P.Some(P.select()), requestId => (_jsxs(_Fragment, { children: [_jsx(Space, { height: hasDescription ? 4 : 8 }), _jsx(LakeTooltip, { describedBy: "copy", onHide: () => setVisibleState("copy"), togglableOnFocus: true, placement: "center", containerStyle: styles.copyTooltip, content: visibleState === "copy"
117
- ? t("copyButton.copyTooltip")
118
- : t("copyButton.copiedTooltip"), children: _jsxs(Pressable, { style: styles.copyButton, onPress: event => {
119
- event.stopPropagation();
120
- event.preventDefault();
121
- setClipboardText(requestId !== null && requestId !== void 0 ? requestId : "");
122
- setVisibleState("copied");
123
- }, children: [_jsx(Icon, { color: colors.gray[700], size: 14, name: "copy-regular" }), _jsx(Space, { width: 4 }), _jsxs(LakeText, { numberOfLines: 1, variant: "smallRegular", color: colors.gray[700], children: ["ID: ", requestId] })] }) })] })))
124
- .exhaustive()] }), _jsx(Pressable, { onPress: () => onClose(uid), style: styles.closeButton, children: _jsx(Icon, { name: "lake-close", size: 24, color: colors.gray[500] }) }), progress != null && (_jsxs(_Fragment, { children: [_jsx(Space, { height: 24 }), _jsx(View, { ref: progressBarRef, role: "progressbar", style: [styles.progressBar, { backgroundColor: colors[colorVariation][500] }] })] }))] }) }));
125
- });
126
- export const ToastStack = () => {
127
- const toasts = useToasts();
128
- const [rootElement, setRootElement] = useState(() => undefined);
129
- useEffect(() => {
130
- const rootElement = document.createElement("div");
131
- document.body.append(rootElement);
132
- setRootElement(rootElement);
133
- return () => {
134
- rootElement.remove();
135
- setRootElement(undefined);
136
- };
137
- }, []);
138
- if (rootElement == null) {
139
- return null;
140
- }
141
- return (_jsx(Portal, { container: rootElement, children: _jsx(TransitionGroupView, { style: styles.list, enter: animations.fadeAndSlideInFromRight.enter, leave: animations.fadeAndSlideInFromRight.leave, children: toasts.map(toast => (_jsx(Toast, { uid: toast.uid, variant: toast.variant, title: toast.title, description: toast.description, error: toast.error, progress: toast.progress, onClose: hideToast }, toast.uid))) }) }));
142
- };
@@ -1,31 +0,0 @@
1
- declare const createProgress: ({ duration, onEnd }: {
2
- duration: number;
3
- onEnd: () => void;
4
- }) => {
5
- clear: () => void;
6
- reset: () => void;
7
- subscribe: (callback: (value: number) => void) => () => void;
8
- };
9
- export type ToastProgress = ReturnType<typeof createProgress>;
10
- export type ToastVariant = "success" | "info" | "warning" | "error";
11
- type ToastContent = {
12
- variant: ToastVariant;
13
- title: string;
14
- description?: string;
15
- error?: unknown;
16
- autoClose?: boolean;
17
- };
18
- type Toast = {
19
- uid: string;
20
- variant: ToastVariant;
21
- title: string;
22
- description?: string;
23
- error?: unknown;
24
- progress?: ToastProgress;
25
- };
26
- export declare const useToasts: () => Toast[];
27
- export declare const hideToast: (uid: string) => void;
28
- export declare const registerErrorToRequestId: (value: WeakMap<WeakKey, string>) => void;
29
- export declare const getErrorToRequestId: () => WeakMap<WeakKey, string>;
30
- export declare const showToast: ({ variant, title, description, error, autoClose }: ToastContent) => string;
31
- export {};
@@ -1,88 +0,0 @@
1
- import { atom, useAtom } from "react-atomic-state";
2
- // based on https://gist.github.com/ncou/3a0a1f89c8e22416d0d607f621a948a9
3
- const createProgress = ({ duration, onEnd }) => {
4
- const callbacks = new Set();
5
- let endDate = 0;
6
- let timerId = 0;
7
- const step = () => {
8
- const value = (endDate - Date.now()) / duration;
9
- callbacks.forEach(callback => callback(value));
10
- animationRequest = window.requestAnimationFrame(step);
11
- };
12
- let animationRequest = window.requestAnimationFrame(step);
13
- const start = () => {
14
- endDate = Date.now() + duration;
15
- timerId = window.setTimeout(() => {
16
- clear();
17
- onEnd();
18
- }, duration);
19
- };
20
- const reset = () => {
21
- window.clearTimeout(timerId);
22
- start();
23
- };
24
- const subscribe = (callback) => {
25
- callbacks.add(callback);
26
- return () => {
27
- callbacks.delete(callback);
28
- };
29
- };
30
- const clear = () => {
31
- document.removeEventListener("visibilitychange", onDocumentVisible);
32
- window.clearTimeout(timerId);
33
- window.cancelAnimationFrame(animationRequest);
34
- };
35
- const onDocumentVisible = () => {
36
- document.removeEventListener("visibilitychange", onDocumentVisible);
37
- reset();
38
- };
39
- if (document.hidden) {
40
- document.addEventListener("visibilitychange", onDocumentVisible);
41
- }
42
- else {
43
- reset();
44
- }
45
- return {
46
- clear,
47
- reset,
48
- subscribe,
49
- };
50
- };
51
- const toasts = atom([]);
52
- export const useToasts = () => useAtom(toasts);
53
- export const hideToast = (uid) => {
54
- var _a;
55
- const toast = toasts.get().find(toast => toast.uid === uid);
56
- if (toast != null) {
57
- (_a = toast.progress) === null || _a === void 0 ? void 0 : _a.clear();
58
- toasts.set(toasts => toasts.filter(toast => toast.uid !== uid));
59
- }
60
- };
61
- let errorToRequestId = new WeakMap();
62
- export const registerErrorToRequestId = (value) => {
63
- errorToRequestId = value;
64
- };
65
- export const getErrorToRequestId = () => {
66
- return errorToRequestId;
67
- };
68
- export const showToast = ({ variant, title, description, error, autoClose }) => {
69
- var _a;
70
- const uid = `${variant} - ${title} - ${description !== null && description !== void 0 ? description : ""}`;
71
- const toast = toasts.get().find(toast => toast.uid === uid);
72
- if (toast != null) {
73
- (_a = toast.progress) === null || _a === void 0 ? void 0 : _a.reset();
74
- return uid;
75
- }
76
- // by default, only info and success toasts are auto-closing
77
- const shouldAutoClose = autoClose !== null && autoClose !== void 0 ? autoClose : (variant === "info" || variant === "success");
78
- const progress = shouldAutoClose
79
- ? createProgress({
80
- duration: 10000,
81
- onEnd: () => {
82
- hideToast(uid);
83
- },
84
- })
85
- : undefined;
86
- toasts.set(toasts => [{ uid, variant, title, description, error, progress }, ...toasts]);
87
- return uid;
88
- };