Send files directly between devices over the internet - no cloud storage, no size limits.
TypeScript cross-platformdata-transferdecentralizedfile-sharingfile-transferhole-punchingopen-sourcep2ppeer-to-peerprivacyprivacy-tools
import { useState } from 'react'
import {
buildJoinUrl,
buildWebReceiveUrl,
exceedsWebLinkLimit,
useSubscriptionStore,
formatFileSize,
formatItemsCount,
formatTextSnippetPreview,
groupSelectedFiles,
useCopiedFlag,
useShareViewModel
} from '@altersend/domain'
import { Button, LinkCard, LinkRow, WaitingRadar, useTheme } from '@altersend/components'
import {
ChevronsUpDownIcon,
deviceIcon,
FolderIcon,
MessageSquareIcon,
QrCodeIcon,
ShareIcon
} from '@altersend/components/icons'
import { useTranslation } from '@altersend/locales'
import { Popover, QRCode, QRModal, useToast } from '../../components'
import { CopyLinkButton } from './CopyLinkButton'
import { TopicCopyButton } from './TopicCopyButton'
export function ShareView() {
const { t } = useTranslation(['send', 'common'])
const { theme } = useTheme()
const c = theme.colors
const toast = useToast()
const vm = useShareViewModel(t, {
onPeerJoined: (peer) =>
toast.show({
title: peer.isKnown
? t('send:status.deviceConnected', { name: peer.name })
: t('send:status.peerConnected'),
durationMs: 2500
}),
onPeerPaired: (peer) =>
toast.show({ title: t('send:status.pairedToast', { name: peer.name }), durationMs: 2500 }),
onInviteFailed: (peer) =>
toast.show({
title: t('send:status.inviteFailedToast', { name: peer.name }),
hint: t('send:status.inviteFailedHint'),
variant: 'error',
durationMs: 3500
}),
onPeerOutdated: () =>
toast.show({ title: t('send:status.peerOutdated'), variant: 'error', durationMs: 4000 })
})
const [isQrOpen, setIsQrOpen] = useState(false)
const { copiedId, flashCopied } = useCopiedFlag()
const hasConnectedDevices = vm.connectedCount > 0
const pro = useSubscriptionStore((state) => state.active)
const webLinkTooLarge = exceedsWebLinkLimit(vm.totalSize, pro)
const fileRows = groupSelectedFiles(vm.files)
const singleFolder = fileRows.length === 1 && fileRows[0].kind === 'folder' ? fileRows[0] : null
const copyTopic = async () => {
if (!vm.topic) return
await navigator.clipboard.writeText(vm.topic)
vm.markCopied()
}
const copyLink = async () => {
if (!vm.topic) return
const base = import.meta.env.VITE_WEB_URL as string | undefined
await navigator.clipboard.writeText(buildWebReceiveUrl(vm.topic, base))
flashCopied('link')
}
const hasTexts = vm.texts.length > 0
const filesCard =
vm.files.length === 0 && !hasTexts ? null : (
<Popover
variant='plain'
align='left'
trigger={
<LinkRow
standalone
compact
icon={<FolderIcon size={16} color={c.colorTextSecondary} />}
label={
hasTexts
? formatItemsCount(vm.files.length, vm.texts.length, t)
: singleFolder
? singleFolder.name
: t('common:files.count', { count: vm.files.length })
}
subtitle={vm.totalSize > 0 ? formatFileSize(vm.totalSize) : undefined}
trailing={<ChevronsUpDownIcon size={16} color={c.colorTextMuted} />}
/>
}
>
{() => (
<div style={{ maxHeight: 280, overflowY: 'auto' }}>
<LinkCard>
{fileRows.map((row, index) =>
row.kind === 'folder' ? (
<LinkRow
key={`folder:${row.name}`}
compact
icon={<FolderIcon size={16} color={c.colorTextSecondary} />}
label={row.name}
size={row.totalSize}
subtitle={t('common:files.count', { count: row.files.length })}
isLast={!hasTexts && index === fileRows.length - 1}
/>
) : (
<LinkRow
key={row.file.path}
compact
file
label={row.file.name}
size={row.file.size}
isLast={!hasTexts && index === fileRows.length - 1}
/>
)
)}
{vm.texts.map((text, index) => (
<LinkRow
key={text.path}
compact
icon={<MessageSquareIcon size={16} color={c.colorInfo} />}
iconBackground={c.colorInfoSubtle}
label={formatTextSnippetPreview(text.content)}
subtitle={t('common:files.text')}
subtitleTone='faint'
isLast={index === vm.texts.length - 1}
/>
))}
</LinkCard>
</div>
)}
</Popover>
)
return (
<>
<div className='flex flex-col gap-6 pt-5'>
{hasConnectedDevices ? (
<div className='flex w-full gap-2'>
<TopicCopyButton
topic={vm.topic}
copied={vm.isCopied}
onCopy={() => void copyTopic()}
placeholder={t('send:connection.placeholder')}
/>
<CopyLinkButton
topic={vm.topic}
copied={copiedId === 'link'}
locked={webLinkTooLarge}
onCopy={() => void copyLink()}
/>
<div className='flex h-12 w-12 shrink-0'>
<Button
variant='secondary'
iconOnly
width='full'
aria-label={t('send:connection.showQrLabel')}
icon={<QrCodeIcon size={18} />}
onClick={() => setIsQrOpen(true)}
/>
</div>
</div>
) : (
<div className='flex flex-col gap-5'>
<div className='flex gap-6'>
<aside
className={`flex w-[240px] shrink-0 justify-center ${vm.hasDevices ? 'self-start' : ''}`}
>
{vm.topic ? (
<QRCode
imageLabel={t('send:connection.qrCodeLabel')}
loadingLabel={t('send:connection.generating')}
size={216}
value={buildJoinUrl(vm.topic)}
/>
) : (
<div
className='flex items-center justify-center rounded-lg bg-surface-primary text-[12px] text-text-muted'
style={{ width: 216, height: 216 }}
>
{t('send:connection.generating')}
</div>
)}
</aside>
<div
className={`flex min-w-0 flex-1 flex-col gap-5 ${vm.hasDevices ? '' : 'justify-center'}`}
>
{vm.phase === 'waiting' && (
<div className='flex items-center gap-3'>
<WaitingRadar
size={44}
color={c.colorInfo}
pulsing
icon={<ShareIcon size={17} color={c.colorInfo} />}
/>
<div className='min-w-0'>
<p className='m-0 text-[15px] font-bold leading-snug text-text-primary'>
{t('send:status.waitingForJoin')}
</p>
<p className='m-0 mt-0.5 text-[12px] leading-snug text-text-muted'>
{t('send:hints.keepOpen')}
</p>
</div>
</div>
)}
<div className='flex w-full gap-2'>
<TopicCopyButton
topic={vm.topic}
copied={vm.isCopied}
onCopy={() => void copyTopic()}
placeholder={t('send:connection.placeholder')}
/>
<CopyLinkButton
topic={vm.topic}
copied={copiedId === 'link'}
locked={webLinkTooLarge}
onCopy={() => void copyLink()}
/>
</div>
{vm.hasDevices && filesCard}
</div>
</div>
{!vm.hasDevices && filesCard}
</div>
)}
{hasConnectedDevices && filesCard && <div>{filesCard}</div>}
{vm.hasDevices && (
<div className='flex flex-col gap-2'>
<div className='flex items-center justify-between px-1'>
<p className='m-0 text-[13px] font-medium text-text-muted'>
{t('send:peer.devices')}
</p>
{vm.connectedCount > 0 && (
<p className='m-0 text-[11.5px] font-medium text-text-muted'>
{t('send:peer.connectedCount', { count: vm.connectedCount })}
</p>
)}
</div>
<LinkCard>
{vm.devices.map((row, index) => {
const isLast = index === vm.devices.length - 1
if (row.kind === 'connected') {
const Icon = row.deviceType ? deviceIcon(row.deviceType) : null
return (
<LinkRow
key={row.peerKey}
compact
icon={
Icon ? (
<Icon size={18} color={c.colorTextSecondary} />
) : (
<span
style={{ color: c.colorInfo }}
className='font-mono text-[12px] font-semibold uppercase'
>
{row.name.slice(0, 2)}
</span>
)
}
iconBackground={Icon ? c.colorSurfacePrimary : c.colorInfoSubtle}
label={row.name}
subtitle={row.subtitle}
subtitleTone={row.subtitleTone}
status={row.status}
progressPercent={row.progressPercent}
trailing={
row.action === 'pair' ? (
<Button
onClick={() => vm.pair(row.peerKey)}
size='sm'
variant='secondary'
pill
>
{t('send:peer.pair')}
</Button>
) : row.action === 'pair-requested' ? (
<Button size='sm' variant='secondary' pill loading>
{t('send:peer.requested')}
</Button>
) : undefined
}
isLast={isLast}
/>
)
}
const PeerIcon = deviceIcon(row.deviceType)
const isActive = row.action === 'inviting' || row.action === 'invite-sent'
const label =
row.action === 'inviting'
? t('send:peer.inviting')
: row.action === 'invite-sent'
? t('send:peer.sent')
: t('send:peer.invite')
return (
<LinkRow
key={row.peerKey}
compact
icon={<PeerIcon size={18} color={c.colorTextSecondary} />}
label={row.name}
subtitle={row.subtitle}
subtitleTone={row.subtitleTone}
onPress={() => void vm.invite(row.peerKey)}
trailing={
<Button
disabled={isActive}
loading={isActive}
onClick={() => void vm.invite(row.peerKey)}
size='sm'
variant='primary'
pill
>
{label}
</Button>
}
isLast={isLast}
/>
)
})}
</LinkCard>
</div>
)}
</div>
<QRModal topic={vm.topic} open={isQrOpen} onClose={() => setIsQrOpen(false)} />
</>
)
}