tooot/src/components/Message.tsx

115 lines
2.8 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'
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'
import FlashMessage, { MessageType, showMessage } from 'react-native-flash-message'
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,
type
}: {
ref?: RefObject<FlashMessage>
duration?: 'short' | 'long'
autoHide?: boolean
message: string
description?: string
onPress?: () => void
type?: MessageType
}) => {
2021-04-09 21:43:12 +02:00
AccessibilityInfo.announceForAccessibility(message + '.' + description)
if (type && type === 'danger') {
2021-02-28 22:49:55 +01:00
haptics('Error')
}
2021-05-09 21:59:03 +02:00
if (ref) {
ref.current?.showMessage({
duration: type === 'danger' ? 8000 : duration === 'short' ? 3000 : 5000,
2021-05-09 21:59:03 +02:00
autoHide,
message,
description,
onPress,
type
2021-05-09 21:59:03 +02:00
})
} else {
showMessage({
duration: type === 'danger' ? 8000 : duration === 'short' ? 3000 : 5000,
2021-05-09 21:59:03 +02:00
autoHide,
message,
description,
onPress,
type
2021-05-09 21:59:03 +02:00
})
}
2021-02-28 22:49:55 +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()
2021-02-28 22:49:55 +01:00
enum iconMapping {
success = 'check-circle',
danger = 'x-circle',
warning = 'alert-circle',
none = 'x',
default = 'x',
info = 'x',
auto = 'x'
}
enum colorMapping {
success = 'blue',
danger = 'red',
warning = 'secondary',
none = 'secondary',
default = 'secondary',
info = 'secondary',
auto = 'secondary'
}
2021-05-09 21:59:03 +02:00
return (
<FlashMessage
ref={ref}
icon='auto'
renderFlashMessageIcon={type => {
return typeof type === 'string' && ['success', 'danger', 'warning'].includes(type) ? (
<Icon
name={iconMapping[type]}
size={StyleConstants.Font.LineHeight.M}
color={colors[colorMapping[type]]}
style={{ marginRight: StyleConstants.Spacing.S }}
/>
) : null
}}
2021-05-09 21:59:03 +02:00
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,
2023-01-04 00:06:37 +01:00
paddingRight: StyleConstants.Spacing.M * 2
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
2023-01-08 16:59:35 +01:00
textProps={{ numberOfLines: 3 }}
2021-05-09 21:59:03 +02:00
/>
)
})
2021-02-28 22:49:55 +01:00
export { Message, displayMessage }