tooot/src/components/Timeline/Shared/ContextMenu.tsx

85 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-06-07 20:07:14 +02:00
import contextMenuAccount from '@components/ContextMenu/account'
import contextMenuInstance from '@components/ContextMenu/instance'
import contextMenuShare from '@components/ContextMenu/share'
import contextMenuStatus from '@components/ContextMenu/status'
2022-06-06 22:49:43 +02:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
2022-06-07 20:07:14 +02:00
import React from 'react'
2022-06-06 22:49:43 +02:00
import { createContext } from 'react'
import { Platform } from 'react-native'
2022-10-27 22:51:02 +02:00
import ContextMenu, { ContextMenuAction, ContextMenuProps } from 'react-native-context-menu-view'
2022-06-06 22:49:43 +02:00
export interface Props {
2022-08-09 00:44:56 +02:00
copiableContent: React.MutableRefObject<{
content: string
complete: boolean
}>
2022-06-07 20:07:14 +02:00
status?: Mastodon.Status
2022-06-06 22:49:43 +02:00
queryKey?: QueryKeyTimeline
rootQueryKey?: QueryKeyTimeline
}
export const ContextMenuContext = createContext<ContextMenuAction[]>([])
2022-06-07 20:07:14 +02:00
const TimelineContextMenu: React.FC<Props & ContextMenuProps> = ({
2022-06-06 22:49:43 +02:00
children,
2022-08-09 00:44:56 +02:00
copiableContent,
2022-06-06 22:49:43 +02:00
status,
queryKey,
2022-06-07 20:07:14 +02:00
rootQueryKey,
...props
2022-06-06 22:49:43 +02:00
}) => {
2022-08-09 00:44:56 +02:00
if (!status || !queryKey || Platform.OS === 'android') {
2022-06-06 22:49:43 +02:00
return <>{children}</>
}
2022-06-07 20:07:14 +02:00
const actions: ContextMenuAction[] = []
2022-06-06 22:49:43 +02:00
2022-06-07 20:07:14 +02:00
const shareOnPress =
status.visibility !== 'direct'
? contextMenuShare({
2022-08-09 00:44:56 +02:00
copiableContent,
2022-06-07 20:07:14 +02:00
actions,
type: 'status',
url: status.url || status.uri
})
: null
2022-06-06 22:49:43 +02:00
const statusOnPress = contextMenuStatus({
2022-06-07 20:07:14 +02:00
actions,
2022-06-06 22:49:43 +02:00
status,
queryKey,
rootQueryKey
})
2022-10-27 22:51:02 +02:00
const accountOnPress = status?.account?.id
? contextMenuAccount({
actions,
type: 'status',
queryKey,
rootQueryKey,
id: status.account.id
})
: null
2022-06-06 22:49:43 +02:00
const instanceOnPress = contextMenuInstance({
2022-06-07 20:07:14 +02:00
actions,
2022-06-06 22:49:43 +02:00
status,
queryKey,
rootQueryKey
})
return (
2022-06-07 20:07:14 +02:00
<ContextMenuContext.Provider value={actions}>
2022-06-06 22:49:43 +02:00
<ContextMenu
2022-06-07 20:07:14 +02:00
actions={actions}
onPress={({ nativeEvent: { index } }) => {
2022-10-27 22:51:02 +02:00
for (const on of [shareOnPress, statusOnPress, accountOnPress, instanceOnPress]) {
on && on(index)
2022-06-07 20:07:14 +02:00
}
2022-06-06 22:49:43 +02:00
}}
children={children}
2022-06-07 20:07:14 +02:00
{...props}
2022-06-06 22:49:43 +02:00
/>
</ContextMenuContext.Provider>
)
}
export default TimelineContextMenu