feat: add app download
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
Flex,
|
||||
HStack,
|
||||
Heading,
|
||||
Spacer,
|
||||
Spinner,
|
||||
Stack,
|
||||
Text,
|
||||
VStack,
|
||||
useToast,
|
||||
} from '@chakra-ui/react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { api, ApiError, appDownloadUrl, type AppDownload } from '../../lib/api'
|
||||
import { formatFileSize } from '../../lib/format'
|
||||
|
||||
const DownloadIcon = () => (
|
||||
<Box as="svg" w="16px" h="16px" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||
<Box as="path" d="M12 3v12m0 0-4-4m4 4 4-4M5 21h14" />
|
||||
</Box>
|
||||
)
|
||||
|
||||
export function AppDownloads() {
|
||||
const toast = useToast()
|
||||
const navigate = useNavigate()
|
||||
const [items, setItems] = useState<AppDownload[]>([])
|
||||
const [eligible, setEligible] = useState(false)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.listAppDownloads()
|
||||
setItems(res.items ?? [])
|
||||
setEligible(res.eligible)
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.status === 401) navigate('/login')
|
||||
else toast({ status: 'error', title: 'Chargement des applications impossible' })
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [navigate, toast])
|
||||
|
||||
useEffect(() => {
|
||||
void load()
|
||||
}, [load])
|
||||
|
||||
return (
|
||||
<>
|
||||
<Flex mb={6} align="center">
|
||||
<Heading size="md">Applications</Heading>
|
||||
<Spacer />
|
||||
</Flex>
|
||||
|
||||
<Box mb={8} p={6} borderWidth="1px" borderRadius="lg" bg="bg-surface">
|
||||
{loading ? (
|
||||
<Spinner />
|
||||
) : !eligible ? (
|
||||
<Text color="gray.500">
|
||||
Le téléchargement des applications nécessite une démo ou un abonnement actif.
|
||||
</Text>
|
||||
) : items.length === 0 ? (
|
||||
<Text color="gray.500">Aucune application disponible pour le moment.</Text>
|
||||
) : (
|
||||
<VStack align="stretch" spacing={3}>
|
||||
{items.map((item) => (
|
||||
<HStack key={item.name} justify="space-between" flexWrap="wrap" rowGap={2}>
|
||||
<Stack spacing={0}>
|
||||
<Text fontFamily="mono">{item.name}</Text>
|
||||
<Text fontSize="sm" color="gray.500">
|
||||
{formatFileSize(item.size_bytes)}
|
||||
</Text>
|
||||
</Stack>
|
||||
<Button
|
||||
as="a"
|
||||
href={appDownloadUrl(item.name)}
|
||||
download={item.name}
|
||||
size="sm"
|
||||
colorScheme="primary"
|
||||
leftIcon={<DownloadIcon />}
|
||||
>
|
||||
Télécharger
|
||||
</Button>
|
||||
</HStack>
|
||||
))}
|
||||
</VStack>
|
||||
)}
|
||||
</Box>
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user