tooot/src/components/ContextMenu/share.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-06-06 22:49:43 +02:00
import analytics from '@components/analytics'
2022-08-09 00:44:56 +02:00
import { displayMessage } from '@components/Message'
import Clipboard from '@react-native-clipboard/clipboard'
import { useTheme } from '@utils/styles/ThemeManager'
2022-06-06 22:49:43 +02:00
import { useTranslation } from 'react-i18next'
import { Platform, Share } from 'react-native'
import { ContextMenuAction } from 'react-native-context-menu-view'
export interface Props {
2022-08-09 00:44:56 +02:00
copiableContent?: React.MutableRefObject<{
content?: string | undefined
complete: boolean
}>
2022-06-07 20:07:14 +02:00
actions: ContextMenuAction[]
type: 'status' | 'account'
url: string
2022-06-06 22:49:43 +02:00
}
2022-08-09 00:44:56 +02:00
const contextMenuShare = ({ copiableContent, actions, type, url }: Props) => {
const { theme } = useTheme()
2022-06-06 22:49:43 +02:00
const { t } = useTranslation('componentContextMenu')
2022-06-07 20:07:14 +02:00
actions.push({
id: 'share',
title: t(`share.${type}.action`),
systemIcon: 'square.and.arrow.up'
})
2022-08-09 00:44:56 +02:00
Platform.OS !== 'android' &&
actions.push({
id: 'copy',
title: t(`copy.action`),
systemIcon: 'doc.on.doc',
disabled: !copiableContent?.current.content?.length
})
2022-06-06 22:49:43 +02:00
return (index: number) => {
2022-08-09 00:44:56 +02:00
if (actions[index].id === 'copy') {
analytics('timeline_shared_headeractions_copy_press')
Clipboard.setString(copiableContent?.current.content || '')
displayMessage({
theme,
type: 'success',
message: t(`copy.succeed`)
})
}
if (actions[index].id === 'share') {
analytics('timeline_shared_headeractions_share_press')
switch (Platform.OS) {
case 'ios':
Share.share({ url })
break
case 'android':
Share.share({ message: url })
break
}
2022-06-06 22:49:43 +02:00
}
}
}
export default contextMenuShare