Skip to content

Send files directly between devices over the internet - no cloud storage, no size limits.

3.8 KB
Raw Blame
import { Modal, StyleSheet, View } from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { Button, useTheme } from '@altersend/components'
import { CheckIcon, CloseIcon, deviceIcon } from '@altersend/components/icons'
import {
  acceptInvite,
  formatFileSize,
  formatItemsCount,
  useIncomingInvite,
  type IncomingInvite
} from '@altersend/domain'
import { useTranslation } from '@altersend/locales'
import { Text } from '@/src/components/ThemedText'
import { autoAcceptStoragePort } from '@/src/lifecycle/autoAcceptStorage'
import { useRouter } from 'expo-router'

export function InviteBanner() {
  const { t } = useTranslation(['common'])
  const { theme } = useTheme()
  const c = theme.colors
  const insets = useSafeAreaInsets()
  const router = useRouter()

  const joinInvite = (incoming: IncomingInvite) => {
    acceptInvite(incoming).catch((err) => console.warn('InviteBanner: acceptInvite failed', err))
    router.navigate('/receive')
  }

  const { invite, accept, decline } = useIncomingInvite({
    storage: autoAcceptStoragePort,
    onAccept: joinInvite
  })

  const visible = invite !== null

  const Icon = invite ? deviceIcon(invite.deviceType) : null

  const fileCount = invite?.fileCount ?? 0
  const textCount = invite?.textCount ?? 0
  const hasCounts = fileCount > 0 || textCount > 0

  const fileLabel = hasCounts
    ? formatItemsCount(fileCount, textCount, t)
    : t('common:files.filesGeneric')
  const sizeLabel =
    fileCount > 0 && invite?.totalSize != null ? ` · ${formatFileSize(invite.totalSize)}` : ''

  return (
    <Modal
      visible={visible}
      animationType='slide'
      presentationStyle='pageSheet'
      onRequestClose={decline}
    >
      {invite && Icon ? (
        <View style={[styles.root, { backgroundColor: c.colorBackground }]}>
          <View style={styles.body}>
            <View style={[styles.iconWrap, { backgroundColor: c.colorSurfaceSecondary }]}>
              <Icon size={48} color={c.colorTextPrimary} />
            </View>

            <Text style={[styles.deviceName, { color: c.colorTextPrimary }]}>
              {invite.displayName}
            </Text>

            <Text style={[styles.subtitle, { color: c.colorTextSecondary }]}>
              {t('common:status.wantsToSend', { label: fileLabel, size: sizeLabel })}
            </Text>
          </View>

          <View style={[styles.footer, { paddingBottom: Math.max(insets.bottom, 36) }]}>
            <View style={styles.actionWrap}>
              <Button
                variant='danger'
                size='lg'
                pill
                width='full'
                icon={<CloseIcon size={18} color={c.colorDanger} />}
                onClick={decline}
              >
                Decline
              </Button>
            </View>
            <View style={styles.actionWrap}>
              <Button
                variant='success'
                size='lg'
                pill
                width='full'
                icon={<CheckIcon size={18} color={c.colorSuccess} />}
                onClick={accept}
              >
                Accept
              </Button>
            </View>
          </View>
        </View>
      ) : null}
    </Modal>
  )
}

const styles = StyleSheet.create({
  root: {
    flex: 1,
    justifyContent: 'space-between'
  },
  body: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    gap: 14,
    paddingHorizontal: 32
  },
  deviceName: {
    fontSize: 20,
    fontWeight: '700',
    textAlign: 'center',
    marginTop: 8
  },
  subtitle: {
    fontSize: 16,
    textAlign: 'center',
    marginTop: 4
  },
  footer: {
    flexDirection: 'row',
    gap: 12,
    paddingHorizontal: 20
  },
  iconWrap: {
    width: 96,
    height: 96,
    borderRadius: 24,
    alignItems: 'center',
    justifyContent: 'center'
  },
  actionWrap: {
    flex: 1
  }
})