|
1 | 1 | import React from 'react' |
2 | 2 | import getConfig from 'next/config' |
| 3 | +import { useRouter } from 'next/router' |
3 | 4 | import moment from 'moment' |
4 | 5 | import { ListUl } from '@styled-icons/boxicons-regular/ListUl' |
5 | 6 | import { Upload } from '@styled-icons/boxicons-regular/Upload' |
6 | 7 | import { Download } from '@styled-icons/boxicons-regular/Download' |
7 | 8 | import { Chat } from '@styled-icons/boxicons-solid/Chat' |
| 9 | +import { ChevronsLeft } from '@styled-icons/boxicons-solid/ChevronsLeft' |
| 10 | +import { ChevronLeft } from '@styled-icons/boxicons-solid/ChevronLeft' |
| 11 | +import { ChevronsRight } from '@styled-icons/boxicons-solid/ChevronsRight' |
| 12 | +import { ChevronRight } from '@styled-icons/boxicons-solid/ChevronRight' |
8 | 13 | import List from './List' |
9 | 14 | import Text from './Text' |
| 15 | +import Box from './Box' |
| 16 | +import Button from './Button' |
10 | 17 |
|
11 | | -const TorrentList = ({ torrents = [], categories }) => { |
| 18 | +const TorrentList = ({ torrents = [], categories, total }) => { |
12 | 19 | const { |
13 | 20 | publicRuntimeConfig: { SQ_SITE_WIDE_FREELEECH }, |
14 | 21 | } = getConfig() |
15 | 22 |
|
| 23 | + const router = useRouter() |
| 24 | + const { |
| 25 | + asPath, |
| 26 | + query: { page: pageParam }, |
| 27 | + } = router |
| 28 | + |
| 29 | + const page = pageParam ? parseInt(pageParam) - 1 : 0 |
| 30 | + |
| 31 | + const maxPage = Math.floor(total / 25) |
| 32 | + const canPrevPage = page > 0 |
| 33 | + const canNextPage = page < maxPage |
| 34 | + |
| 35 | + const setPage = (number) => { |
| 36 | + if (number === 0) router.push(asPath.split('?')[0]) |
| 37 | + else router.push(`${asPath.split('?')[0]}?page=${number + 1}`) |
| 38 | + } |
| 39 | + |
16 | 40 | return ( |
17 | | - <List |
18 | | - data={torrents.map((torrent) => ({ |
19 | | - ...torrent, |
20 | | - href: `/torrent/${torrent.infoHash}`, |
21 | | - }))} |
22 | | - columns={[ |
23 | | - { |
24 | | - header: 'Name', |
25 | | - accessor: 'name', |
26 | | - cell: ({ value, row }) => ( |
27 | | - <Text> |
28 | | - {value} |
29 | | - {(row.freeleech || SQ_SITE_WIDE_FREELEECH === true) && ( |
30 | | - <Text as="span" fontSize={0} color="primary" ml={3}> |
31 | | - FL! |
32 | | - </Text> |
33 | | - )} |
34 | | - </Text> |
35 | | - ), |
36 | | - gridWidth: '2fr', |
37 | | - }, |
38 | | - { |
39 | | - header: 'Category', |
40 | | - accessor: 'type', |
41 | | - cell: ({ value }) => ( |
42 | | - <Text icon={ListUl}> |
43 | | - {categories.find((c) => c.slug === value)?.name || 'None'} |
44 | | - </Text> |
45 | | - ), |
46 | | - gridWidth: '2fr', |
47 | | - }, |
48 | | - { |
49 | | - header: 'Seeders', |
50 | | - accessor: 'seeders', |
51 | | - cell: ({ value }) => <Text icon={Upload}>{value}</Text>, |
52 | | - gridWidth: '1fr', |
53 | | - }, |
54 | | - { |
55 | | - header: 'Leechers', |
56 | | - accessor: 'leechers', |
57 | | - cell: ({ value }) => <Text icon={Download}>{value}</Text>, |
58 | | - gridWidth: '1fr', |
59 | | - }, |
60 | | - { |
61 | | - header: 'Comments', |
62 | | - accessor: 'comments.count', |
63 | | - cell: ({ value }) => <Text icon={Chat}>{value || 0}</Text>, |
64 | | - gridWidth: '1fr', |
65 | | - }, |
66 | | - { |
67 | | - header: 'Uploaded', |
68 | | - accessor: 'created', |
69 | | - cell: ({ value }) => ( |
70 | | - <Text>{moment(value).format('Do MMM YYYY')}</Text> |
71 | | - ), |
72 | | - gridWidth: '1fr', |
73 | | - rightAlign: true, |
74 | | - }, |
75 | | - ]} |
76 | | - /> |
| 41 | + <> |
| 42 | + <List |
| 43 | + data={torrents.map((torrent) => ({ |
| 44 | + ...torrent, |
| 45 | + href: `/torrent/${torrent.infoHash}`, |
| 46 | + }))} |
| 47 | + columns={[ |
| 48 | + { |
| 49 | + header: 'Name', |
| 50 | + accessor: 'name', |
| 51 | + cell: ({ value, row }) => ( |
| 52 | + <Text> |
| 53 | + {value} |
| 54 | + {(row.freeleech || SQ_SITE_WIDE_FREELEECH === true) && ( |
| 55 | + <Text as="span" fontSize={0} color="primary" ml={3}> |
| 56 | + FL! |
| 57 | + </Text> |
| 58 | + )} |
| 59 | + </Text> |
| 60 | + ), |
| 61 | + gridWidth: '2fr', |
| 62 | + }, |
| 63 | + { |
| 64 | + header: 'Category', |
| 65 | + accessor: 'type', |
| 66 | + cell: ({ value }) => ( |
| 67 | + <Text icon={ListUl}> |
| 68 | + {categories.find((c) => c.slug === value)?.name || 'None'} |
| 69 | + </Text> |
| 70 | + ), |
| 71 | + gridWidth: '2fr', |
| 72 | + }, |
| 73 | + { |
| 74 | + header: 'Seeders', |
| 75 | + accessor: 'seeders', |
| 76 | + cell: ({ value }) => <Text icon={Upload}>{value}</Text>, |
| 77 | + gridWidth: '1fr', |
| 78 | + }, |
| 79 | + { |
| 80 | + header: 'Leechers', |
| 81 | + accessor: 'leechers', |
| 82 | + cell: ({ value }) => <Text icon={Download}>{value}</Text>, |
| 83 | + gridWidth: '1fr', |
| 84 | + }, |
| 85 | + { |
| 86 | + header: 'Comments', |
| 87 | + accessor: 'comments.count', |
| 88 | + cell: ({ value }) => <Text icon={Chat}>{value || 0}</Text>, |
| 89 | + gridWidth: '1fr', |
| 90 | + }, |
| 91 | + { |
| 92 | + header: 'Uploaded', |
| 93 | + accessor: 'created', |
| 94 | + cell: ({ value }) => ( |
| 95 | + <Text>{moment(value).format('Do MMM YYYY')}</Text> |
| 96 | + ), |
| 97 | + gridWidth: '1fr', |
| 98 | + rightAlign: true, |
| 99 | + }, |
| 100 | + ]} |
| 101 | + /> |
| 102 | + {typeof total === 'number' && ( |
| 103 | + <Box display="flex" alignItems="center" mt={4}> |
| 104 | + <Button |
| 105 | + onClick={() => setPage(0)} |
| 106 | + variant="secondary" |
| 107 | + disabled={!canPrevPage} |
| 108 | + px={1} |
| 109 | + py={1} |
| 110 | + mr={2} |
| 111 | + > |
| 112 | + <ChevronsLeft size={24} /> |
| 113 | + </Button> |
| 114 | + <Button |
| 115 | + onClick={() => setPage(page - 1)} |
| 116 | + variant="secondary" |
| 117 | + disabled={!canPrevPage} |
| 118 | + px={1} |
| 119 | + py={1} |
| 120 | + mr={2} |
| 121 | + > |
| 122 | + <ChevronLeft size={24} /> |
| 123 | + </Button> |
| 124 | + <Button |
| 125 | + onClick={() => setPage(page + 1)} |
| 126 | + variant="secondary" |
| 127 | + disabled={!canNextPage} |
| 128 | + px={1} |
| 129 | + py={1} |
| 130 | + mr={2} |
| 131 | + > |
| 132 | + <ChevronRight size={24} /> |
| 133 | + </Button> |
| 134 | + <Button |
| 135 | + onClick={() => setPage(maxPage)} |
| 136 | + variant="secondary" |
| 137 | + disabled={!canNextPage} |
| 138 | + px={1} |
| 139 | + py={1} |
| 140 | + mr={3} |
| 141 | + > |
| 142 | + <ChevronsRight size={24} /> |
| 143 | + </Button> |
| 144 | + <Text color="grey"> |
| 145 | + {total.toLocaleString()} results — Page {page + 1} of{' '} |
| 146 | + {(maxPage + 1).toLocaleString()} |
| 147 | + </Text> |
| 148 | + </Box> |
| 149 | + )} |
| 150 | + </> |
77 | 151 | ) |
78 | 152 | } |
79 | 153 |
|
|
0 commit comments