tooot/src/components/contextMenu/share.ts

68 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-08-09 00:44:56 +02:00
import { displayMessage } from '@components/Message'
import Clipboard from '@react-native-clipboard/clipboard'
2022-06-06 22:49:43 +02:00
import { useTranslation } from 'react-i18next'
import { Platform, Share } from 'react-native'
2022-12-03 01:08:38 +01:00
const menuShare = (
params:
| {
visibility?: Mastodon.Status['visibility']
2022-12-18 17:25:18 +01:00
rawContent?: React.MutableRefObject<string[]>
2022-12-03 01:08:38 +01:00
type: 'status'
url?: string
}
| {
type: 'account'
url: string
}
): ContextMenu[][] => {
if (params.type === 'status' && params.visibility === 'direct') return []
2022-06-06 22:49:43 +02:00
const { t } = useTranslation('componentContextMenu')
2022-12-03 01:08:38 +01:00
const menus: ContextMenu[][] = [[]]
2022-06-06 22:49:43 +02:00
2022-12-03 01:08:38 +01:00
if (params.url) {
const url = params.url
menus[0].push({
key: 'share',
item: {
onSelect: () => {
switch (Platform.OS) {
case 'ios':
Share.share({ url })
break
case 'android':
Share.share({ message: url })
break
}
},
disabled: false,
destructive: false,
hidden: false
},
title: t(`share.${params.type}.action`),
icon: 'square.and.arrow.up'
})
2022-06-06 22:49:43 +02:00
}
2022-12-18 17:25:18 +01:00
if (params.type === 'status')
2022-12-03 01:08:38 +01:00
menus[0].push({
key: 'copy',
item: {
onSelect: () => {
2022-12-18 17:25:18 +01:00
Clipboard.setString(params.rawContent?.current.join(`\n\n`) || '')
displayMessage({ type: 'success', message: t(`copy.succeed`) })
2022-12-03 01:08:38 +01:00
},
disabled: false,
destructive: false,
2022-12-18 17:25:18 +01:00
hidden: !params.rawContent?.current.length
2022-12-03 01:08:38 +01:00
},
title: t('copy.action'),
icon: 'doc.on.doc'
})
return menus
2022-06-06 22:49:43 +02:00
}
2022-12-03 01:08:38 +01:00
export default menuShare