|
| 1 | +import { createStyles, useStyles } from '@theme/styles'; |
| 2 | +import { useEffect } from 'react'; |
| 3 | +import { createCallable } from 'react-call'; |
| 4 | +import { Snackbar as BaseSnackbar, Text } from 'react-native-paper'; |
| 5 | +import { Subject } from 'rxjs'; |
| 6 | +import { hapticFeedback } from '~/lib/haptic'; |
| 7 | +import { logEvent, LogEventParams } from '~/util/analytics'; |
| 8 | + |
| 9 | +const HIDE_SNACKBAR = new Subject<true>(); |
| 10 | + |
| 11 | +type SnackbarVariant = 'info' | 'success' | 'warning' | 'error'; |
| 12 | + |
| 13 | +export interface SnackbarProps { |
| 14 | + variant?: SnackbarVariant; |
| 15 | + message: string; |
| 16 | + duration?: number; |
| 17 | + action?: string; |
| 18 | + event?: Partial<LogEventParams> | boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export const Snackbar = createCallable<SnackbarProps, boolean>( |
| 22 | + ({ call, variant = 'info', message, duration = 6000, action, event }) => { |
| 23 | + const { styles } = useStyles(getStylesheet({ variant })); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + const sub = HIDE_SNACKBAR.subscribe(() => call.end(false)); |
| 27 | + return () => sub.unsubscribe(); |
| 28 | + }, [call]); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + if (variant !== 'info') hapticFeedback(variant); |
| 32 | + }, [variant]); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (event && variant !== 'success') { |
| 36 | + logEvent({ |
| 37 | + level: variant, |
| 38 | + message, |
| 39 | + snackbar: true, |
| 40 | + ...(typeof event === 'object' && event), |
| 41 | + }); |
| 42 | + } |
| 43 | + }, [message, variant, event]); |
| 44 | + |
| 45 | + return ( |
| 46 | + <BaseSnackbar |
| 47 | + elevation={2} |
| 48 | + visible |
| 49 | + duration={duration} |
| 50 | + onDismiss={() => call.end(false)} |
| 51 | + style={[styles.snackbarBase, styles.snackbar]} |
| 52 | + {...(action && { |
| 53 | + action: { |
| 54 | + label: action, |
| 55 | + labelStyle: styles.actionLabel, |
| 56 | + onPress: () => call.end(true), |
| 57 | + }, |
| 58 | + })} |
| 59 | + > |
| 60 | + <Text variant="bodyMedium" style={styles.message}> |
| 61 | + {message} |
| 62 | + </Text> |
| 63 | + </BaseSnackbar> |
| 64 | + ); |
| 65 | + }, |
| 66 | +); |
| 67 | + |
| 68 | +const getStylesheet = ({ variant }: { variant: SnackbarVariant }) => |
| 69 | + createStyles(({ colors }) => { |
| 70 | + const s = { |
| 71 | + info: { |
| 72 | + snackbar: { backgroundColor: colors.inverseSurface }, |
| 73 | + message: { color: colors.inverseOnSurface }, |
| 74 | + actionLabel: { color: colors.inversePrimary }, |
| 75 | + }, |
| 76 | + success: { |
| 77 | + snackbar: { backgroundColor: colors.successContainer }, |
| 78 | + message: { color: colors.onSuccessContainer }, |
| 79 | + }, |
| 80 | + warning: { |
| 81 | + snackbar: { backgroundColor: colors.warningContainer }, |
| 82 | + message: { color: colors.onWarningContainer }, |
| 83 | + }, |
| 84 | + error: { |
| 85 | + snackbar: { backgroundColor: colors.errorContainer }, |
| 86 | + message: { color: colors.onErrorContainer }, |
| 87 | + }, |
| 88 | + }[variant]; |
| 89 | + |
| 90 | + return { |
| 91 | + actionLabel: { |
| 92 | + color: colors.primary, |
| 93 | + }, |
| 94 | + snackbarBase: { |
| 95 | + maxWidth: 600, |
| 96 | + }, |
| 97 | + ...s, |
| 98 | + }; |
| 99 | + }); |
| 100 | + |
| 101 | +type ShowOptions = Omit<SnackbarProps, 'variant' | 'message'>; |
| 102 | + |
| 103 | +export const showInfo = (message: string, options?: ShowOptions) => |
| 104 | + Snackbar.call({ variant: 'info', message, ...options }); |
| 105 | + |
| 106 | +export const showSuccess = (message: string, options?: ShowOptions) => |
| 107 | + Snackbar.call({ variant: 'success', message, ...options }); |
| 108 | + |
| 109 | +export const showWarning = (message: string, options?: ShowOptions) => |
| 110 | + Snackbar.call({ variant: 'warning', message, ...options }); |
| 111 | + |
| 112 | +export const showError = (message: string, options?: ShowOptions) => |
| 113 | + Snackbar.call({ variant: 'error', message, ...options }); |
| 114 | + |
| 115 | +export const hideSnackbar = () => HIDE_SNACKBAR.next(true); |
0 commit comments