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

205 lines
7.7 KiB
TypeScript
Raw Normal View History

2022-12-12 23:57:07 +01:00
import Button from '@components/Button'
2022-12-03 01:08:38 +01:00
import menuAccount from '@components/contextMenu/account'
import menuInstance from '@components/contextMenu/instance'
import menuShare from '@components/contextMenu/share'
import menuStatus from '@components/contextMenu/status'
import Icon from '@components/Icon'
import { RelationshipIncoming, RelationshipOutgoing } from '@components/Relationship'
import browserPackage from '@utils/helpers/browserPackage'
import { getAccountStorage } from '@utils/storage/actions'
2022-12-03 01:08:38 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2022-12-12 23:57:07 +01:00
import * as WebBrowser from 'expo-web-browser'
2022-12-25 17:40:53 +01:00
import React, { Fragment, useContext, useState } from 'react'
2022-12-12 23:57:07 +01:00
import { useTranslation } from 'react-i18next'
2022-12-03 01:08:38 +01:00
import { Platform, Pressable, View } from 'react-native'
import * as DropdownMenu from 'zeego/dropdown-menu'
2022-12-03 20:47:11 +01:00
import StatusContext from './Context'
2022-12-03 01:08:38 +01:00
import HeaderSharedAccount from './HeaderShared/Account'
import HeaderSharedApplication from './HeaderShared/Application'
import HeaderSharedCreated from './HeaderShared/Created'
import HeaderSharedMuted from './HeaderShared/Muted'
import HeaderSharedVisibility from './HeaderShared/Visibility'
2022-12-03 20:47:11 +01:00
export type Props = {
2022-12-03 01:08:38 +01:00
notification: Mastodon.Notification
}
2022-12-03 20:47:11 +01:00
const TimelineHeaderNotification: React.FC<Props> = ({ notification }) => {
2022-12-12 23:57:07 +01:00
const { t } = useTranslation('componentTimeline')
2022-12-03 20:47:11 +01:00
const { queryKey, status } = useContext(StatusContext)
2022-12-03 01:08:38 +01:00
const { colors } = useTheme()
const [openChange, setOpenChange] = useState(false)
const mShare = menuShare({
2022-12-03 20:47:11 +01:00
visibility: status?.visibility,
2022-12-03 01:08:38 +01:00
type: 'status',
2022-12-03 20:47:11 +01:00
url: status?.url || status?.uri
2022-12-03 01:08:38 +01:00
})
const mAccount = menuAccount({
2022-12-03 15:50:15 +01:00
type: 'status',
2022-12-03 01:08:38 +01:00
openChange,
2022-12-03 20:47:11 +01:00
account: status?.account,
2023-01-04 22:39:29 +01:00
...(status && { status })
2022-12-03 01:08:38 +01:00
})
2022-12-03 20:47:11 +01:00
const mStatus = menuStatus({ status, queryKey })
const mInstance = menuInstance({ status, queryKey })
2022-12-03 01:08:38 +01:00
const actions = () => {
switch (notification.type) {
case 'follow':
return <RelationshipOutgoing id={notification.account.id} />
case 'follow_request':
return <RelationshipIncoming id={notification.account.id} />
2022-12-12 23:57:07 +01:00
case 'admin.report':
return (
<Button
type='text'
content={t('shared.actions.openReport')}
onPress={async () =>
WebBrowser.openAuthSessionAsync(
`https://${getAccountStorage.string('auth.domain')}/admin/reports/${
notification.report.id
}`,
2022-12-12 23:57:07 +01:00
'tooot://tooot',
{
2022-12-16 22:00:22 +01:00
...(await browserPackage()),
2022-12-12 23:57:07 +01:00
dismissButtonStyle: 'done',
readerMode: false
}
)
}
/>
)
2022-12-03 01:08:38 +01:00
default:
2022-12-24 02:13:24 +01:00
if (status && Platform.OS !== 'android') {
2022-12-03 01:08:38 +01:00
return (
<Pressable
style={{ flex: 1, alignItems: 'center' }}
children={
<DropdownMenu.Root onOpenChange={setOpenChange}>
<DropdownMenu.Trigger>
<Icon
name='MoreHorizontal'
color={colors.secondary}
size={StyleConstants.Font.Size.L}
/>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
2023-01-08 16:59:35 +01:00
{[mShare, mStatus, mAccount, mInstance].map((menu, i) => (
2022-12-25 17:40:53 +01:00
<Fragment key={i}>
2023-01-08 16:59:35 +01:00
{menu.map((group, index) => (
2022-12-24 01:59:18 +01:00
<DropdownMenu.Group key={index}>
2023-01-08 16:59:35 +01:00
{group.map(item => {
switch (item.type) {
case 'item':
return (
<DropdownMenu.Item key={item.key} {...item.props}>
<DropdownMenu.ItemTitle children={item.title} />
{item.icon ? (
<DropdownMenu.ItemIcon ios={{ name: item.icon }} />
) : null}
</DropdownMenu.Item>
)
case 'sub':
return (
// @ts-ignore
<DropdownMenu.Sub key={item.key}>
<DropdownMenu.SubTrigger
key={item.trigger.key}
{...item.trigger.props}
>
<DropdownMenu.ItemTitle children={item.trigger.title} />
{item.trigger.icon ? (
<DropdownMenu.ItemIcon
ios={{ name: item.trigger.icon }}
/>
) : null}
</DropdownMenu.SubTrigger>
<DropdownMenu.SubContent>
{item.items.map(sub => (
<DropdownMenu.Item key={sub.key} {...sub.props}>
<DropdownMenu.ItemTitle children={sub.title} />
{sub.icon ? (
<DropdownMenu.ItemIcon ios={{ name: sub.icon }} />
) : null}
</DropdownMenu.Item>
))}
</DropdownMenu.SubContent>
</DropdownMenu.Sub>
)
}
})}
2022-12-24 01:59:18 +01:00
</DropdownMenu.Group>
2022-12-03 01:08:38 +01:00
))}
2022-12-25 17:40:53 +01:00
</Fragment>
2022-12-03 01:08:38 +01:00
))}
</DropdownMenu.Content>
</DropdownMenu.Root>
}
/>
)
}
}
}
return (
<View style={{ flex: 1, flexDirection: 'row' }}>
<View
style={{
2022-12-12 23:57:07 +01:00
flex:
notification.type === 'follow' ||
notification.type === 'follow_request' ||
notification.type === 'admin.report'
? 1
: 4
2022-12-03 01:08:38 +01:00
}}
>
<HeaderSharedAccount
2022-12-12 23:57:07 +01:00
account={
notification.type === 'admin.report'
? notification.report.target_account
: notification.status
? notification.status.account
: notification.account
}
{...((notification.type === 'follow' ||
notification.type === 'follow_request' ||
notification.type === 'admin.report') && {
2022-12-03 01:08:38 +01:00
withoutName: true
})}
/>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
marginTop: StyleConstants.Spacing.XS,
marginBottom: StyleConstants.Spacing.S
}}
>
2023-01-03 23:57:23 +01:00
<HeaderSharedCreated />
{notification.status?.visibility ? <HeaderSharedVisibility /> : null}
<HeaderSharedMuted />
<HeaderSharedApplication />
2022-12-03 01:08:38 +01:00
</View>
</View>
2022-12-24 02:13:24 +01:00
<View
style={[
{ marginLeft: StyleConstants.Spacing.M },
notification.type === 'follow' ||
notification.type === 'follow_request' ||
notification.type === 'admin.report'
? { flexShrink: 1 }
: { flex: 1 }
]}
children={actions()}
/>
2022-12-03 01:08:38 +01:00
</View>
)
}
export default TimelineHeaderNotification