tooot/src/components/Message.tsx

146 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-02-28 22:49:55 +01:00
import Icon from '@components/Icon'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2022-02-12 14:51:01 +01:00
import { getColors, Theme } from '@utils/styles/themes'
2021-05-09 21:59:03 +02:00
import React, { RefObject } from 'react'
2021-04-09 21:43:12 +02:00
import { AccessibilityInfo } from 'react-native'
2022-11-20 16:14:08 +01:00
import FlashMessage, { hideMessage, showMessage } from 'react-native-flash-message'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
2021-02-28 22:49:55 +01:00
import haptics from './haptics'
const displayMessage = ({
2021-05-09 21:59:03 +02:00
ref,
2021-03-01 00:28:14 +01:00
duration = 'short',
2021-02-28 22:49:55 +01:00
autoHide = true,
message,
description,
onPress,
2022-02-12 14:51:01 +01:00
theme,
2021-02-28 22:49:55 +01:00
type
}:
| {
2021-05-09 21:59:03 +02:00
ref?: RefObject<FlashMessage>
2021-03-01 00:28:14 +01:00
duration?: 'short' | 'long'
2021-02-28 22:49:55 +01:00
autoHide?: boolean
message: string
description?: string
onPress?: () => void
2022-02-12 14:51:01 +01:00
theme?: undefined
2021-02-28 22:49:55 +01:00
type?: undefined
}
| {
2021-05-09 21:59:03 +02:00
ref?: RefObject<FlashMessage>
2021-03-01 00:28:14 +01:00
duration?: 'short' | 'long'
2021-02-28 22:49:55 +01:00
autoHide?: boolean
message: string
description?: string
onPress?: () => void
2022-02-12 14:51:01 +01:00
theme: Theme
2021-02-28 22:49:55 +01:00
type: 'success' | 'error' | 'warning'
}) => {
2021-04-09 21:43:12 +02:00
AccessibilityInfo.announceForAccessibility(message + '.' + description)
2021-02-28 22:49:55 +01:00
enum iconMapping {
success = 'CheckCircle',
error = 'XCircle',
warning = 'AlertCircle'
}
enum colorMapping {
success = 'blue',
error = 'red',
warning = 'secondary'
}
if (type && type === 'error') {
haptics('Error')
}
2021-05-09 21:59:03 +02:00
if (ref) {
ref.current?.showMessage({
2022-05-05 23:08:29 +02:00
duration: type === 'error' ? 8000 : duration === 'short' ? 3000 : 5000,
2021-05-09 21:59:03 +02:00
autoHide,
message,
description,
onPress,
2022-02-12 14:51:01 +01:00
...(theme &&
2021-05-09 21:59:03 +02:00
type && {
renderFlashMessageIcon: () => {
return (
<Icon
name={iconMapping[type]}
size={StyleConstants.Font.LineHeight.M}
2022-02-12 14:51:01 +01:00
color={getColors(theme)[colorMapping[type]]}
2021-05-09 21:59:03 +02:00
style={{ marginRight: StyleConstants.Spacing.S }}
/>
)
}
})
})
} else {
showMessage({
2022-07-09 11:29:47 +02:00
duration: type === 'error' ? 8000 : duration === 'short' ? 3000 : 5000,
2021-05-09 21:59:03 +02:00
autoHide,
message,
description,
onPress,
2022-02-12 14:51:01 +01:00
...(theme &&
2021-05-09 21:59:03 +02:00
type && {
renderFlashMessageIcon: () => {
return (
<Icon
name={iconMapping[type]}
size={StyleConstants.Font.LineHeight.M}
2022-02-12 14:51:01 +01:00
color={getColors(theme)[colorMapping[type]]}
2021-05-09 21:59:03 +02:00
style={{ marginRight: StyleConstants.Spacing.S }}
/>
)
}
})
})
}
2021-02-28 22:49:55 +01:00
}
2021-03-15 22:30:29 +01:00
const removeMessage = () => {
2021-05-09 21:59:03 +02:00
// if (ref) {
// ref.current?.hideMessage()
// } else {
2021-03-15 22:30:29 +01:00
hideMessage()
2021-05-09 21:59:03 +02:00
// }
2021-03-15 22:30:29 +01:00
}
2021-05-09 21:59:03 +02:00
const Message = React.forwardRef<FlashMessage>((_, ref) => {
2022-02-12 14:51:01 +01:00
const { colors, theme } = useTheme()
2022-11-20 16:14:08 +01:00
const insets = useSafeAreaInsets()
2021-02-28 22:49:55 +01:00
2021-05-09 21:59:03 +02:00
return (
<FlashMessage
ref={ref}
icon='auto'
position='top'
floating
style={{
2022-02-12 14:51:01 +01:00
backgroundColor: colors.backgroundDefault,
shadowColor: colors.primaryDefault,
2021-05-09 21:59:03 +02:00
shadowOffset: { width: 0, height: 0 },
2022-02-12 14:51:01 +01:00
shadowOpacity: theme === 'light' ? 0.16 : 0.24,
2022-07-09 11:29:47 +02:00
shadowRadius: 4,
2022-11-20 16:14:08 +01:00
paddingRight: StyleConstants.Spacing.M * 2,
marginTop: ref ? undefined : insets.top
2021-05-09 21:59:03 +02:00
}}
titleStyle={{
2022-02-12 14:51:01 +01:00
color: colors.primaryDefault,
2021-05-09 21:59:03 +02:00
...StyleConstants.FontStyle.M,
fontWeight: StyleConstants.Font.Weight.Bold
}}
textStyle={{
2022-02-12 14:51:01 +01:00
color: colors.primaryDefault,
2021-05-09 21:59:03 +02:00
...StyleConstants.FontStyle.S
}}
// @ts-ignore
textProps={{ numberOfLines: 2 }}
/>
)
})
2021-02-28 22:49:55 +01:00
2021-03-15 22:30:29 +01:00
export { Message, displayMessage, removeMessage }