Skip to content

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

3.2 KB
Raw Blame
import { useCallback, useEffect } from 'react'
import { View } from 'react-native'
import { Button, ErrorBanner } from '@altersend/components'
import { SendIcon } from '@altersend/components/icons'
import { useTranslation } from '@altersend/locales'
import { Layout } from '@/src/components'
import { SelectFilesView } from '@/src/transfer/send'
import { usePathname, useRouter } from 'expo-router'
import {
  formatSendButtonLabel,
  getSendPageCopy,
  getSendStep,
  isShareStep,
  useTransferStore
} from '@altersend/domain'
import { continueShare, exceedsFileCountLimit } from '@altersend/domain'

const SEND_FLOW_PATHS = new Set(['/send', '/send/preparing', '/send/share'])

function NavigationController() {
  const router = useRouter()
  const pathname = usePathname()
  const connectionState = useTransferStore((s) => s.connectionState)
  const draftPhase = useTransferStore((s) => s.draftPhase)
  const step = getSendStep({ draftPhase, isPeerConnected: connectionState === 'peer-connected' })

  useEffect(() => {
    const target =
      step === 'preparing' ? '/send/preparing' : isShareStep(step) ? '/send/share' : '/send'

    if (!SEND_FLOW_PATHS.has(pathname)) return
    if (pathname === target) return

    if (target === '/send/preparing') {
      if (pathname === '/send') router.push('/send/preparing')
      else router.replace('/send/preparing')
    } else if (target === '/send/share') {
      if (pathname === '/send') router.push('/send/share')
      else router.replace('/send/share')
    } else if (
      (pathname === '/send/preparing' || pathname === '/send/share') &&
      router.canDismiss()
    ) {
      router.dismissAll()
    }
  }, [step, pathname, router])

  return null
}

export default function SendSelectScreen() {
  const { t } = useTranslation(['send'])
  const selectedFiles = useTransferStore((s) => s.selectedFiles)
  const draftPhase = useTransferStore((s) => s.draftPhase)
  const connectionState = useTransferStore((s) => s.connectionState)
  const router = useRouter()

  const step = getSendStep({ draftPhase, isPeerConnected: connectionState === 'peer-connected' })
  const copy = getSendPageCopy(t, step)
  const hasSelectedFiles = selectedFiles.length > 0
  const fileCount = selectedFiles.filter((file) => file.kind !== 'text').length
  const textCount = selectedFiles.length - fileCount
  const tooManyFiles = exceedsFileCountLimit(fileCount)

  const openMenu = useCallback(() => router.push('/settings'), [router])

  return (
    <View style={{ flex: 1 }}>
      <NavigationController />
      <Layout
        title={copy.title}
        onMenuPress={openMenu}
        footer={
          hasSelectedFiles ? (
            <View style={{ gap: 8 }}>
              {tooManyFiles ? <ErrorBanner message={t('send:files.tooMany')} /> : null}
              <Button
                disabled={tooManyFiles}
                onClick={() => void continueShare(selectedFiles)}
                size='lg'
                variant='primary'
                width='full'
                icon={<SendIcon size={18} />}
              >
                {formatSendButtonLabel(fileCount, textCount, t)}
              </Button>
            </View>
          ) : undefined
        }
      >
        <SelectFilesView />
      </Layout>
    </View>
  )
}