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

83 lines
1.9 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'
2022-06-07 20:07:14 +02:00
import ContextMenu, {
ContextMenuAction,
ContextMenuProps
} from 'react-native-context-menu-view'
2022-06-06 22:49:43 +02:00
export interface Props {
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,
status,
queryKey,
2022-06-07 20:07:14 +02:00
rootQueryKey,
...props
2022-06-06 22:49:43 +02:00
}) => {
2022-06-07 20:07:14 +02:00
if (!status || !queryKey) {
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({
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
})
const accountOnPress = contextMenuAccount({
2022-06-07 20:07:14 +02:00
actions,
2022-06-06 22:49:43 +02:00
queryKey,
2022-06-07 20:07:14 +02:00
rootQueryKey,
id: status.account.id
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}
2022-06-06 22:49:43 +02:00
onPress={({ nativeEvent: { id } }) => {
2022-06-07 20:07:14 +02:00
for (const on of [
shareOnPress,
statusOnPress,
accountOnPress,
instanceOnPress
]) {
on && on(id)
}
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