tooot/src/components/Message.tsx

104 lines
2.4 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'
import { getTheme } from '@utils/styles/themes'
import React from 'react'
import FlashMessage, { showMessage } from 'react-native-flash-message'
import haptics from './haptics'
const displayMessage = ({
2021-03-01 00:28:14 +01:00
duration = 'short',
2021-02-28 22:49:55 +01:00
autoHide = true,
message,
description,
onPress,
mode,
type
}:
| {
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
mode?: undefined
type?: undefined
}
| {
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
mode: 'light' | 'dark'
type: 'success' | 'error' | 'warning'
}) => {
enum iconMapping {
success = 'CheckCircle',
error = 'XCircle',
warning = 'AlertCircle'
}
enum colorMapping {
success = 'blue',
error = 'red',
warning = 'secondary'
}
if (type && type === 'error') {
haptics('Error')
}
showMessage({
2021-03-04 14:11:08 +01:00
duration: type === 'error' ? 5000 : duration === 'short' ? 1500 : 3000,
2021-02-28 22:49:55 +01:00
autoHide,
message,
description,
onPress,
...(mode &&
type && {
2021-03-04 01:03:53 +01:00
renderFlashMessageIcon: () => {
2021-02-28 22:49:55 +01:00
return (
<Icon
name={iconMapping[type]}
size={StyleConstants.Font.LineHeight.M}
color={getTheme(mode)[colorMapping[type]]}
style={{ marginRight: StyleConstants.Spacing.S }}
/>
)
}
})
})
}
const Message = React.memo(
() => {
const { mode, theme } = useTheme()
return (
<FlashMessage
icon='auto'
position='top'
floating
style={{
backgroundColor: theme.background,
shadowColor: theme.primary,
shadowOffset: { width: 0, height: 0 },
2021-03-01 00:28:14 +01:00
shadowOpacity: mode === 'light' ? 0.16 : 0.24,
2021-02-28 22:49:55 +01:00
shadowRadius: 4
}}
titleStyle={{
color: theme.primary,
...StyleConstants.FontStyle.M,
fontWeight: StyleConstants.Font.Weight.Bold
}}
2021-03-04 01:03:53 +01:00
textStyle={{ color: theme.primary, ...StyleConstants.FontStyle.S }}
2021-02-28 22:49:55 +01:00
// @ts-ignore
textProps={{ numberOfLines: 2 }}
/>
)
},
() => true
)
export { Message, displayMessage }