|
| 1 | +import React from "react"; |
| 2 | +import getConfig from "next/config"; |
| 3 | +import { useRouter } from "next/router"; |
| 4 | +import qs from "qs"; |
| 5 | +import { withAuthServerSideProps } from "../utils/withAuth"; |
| 6 | +import SEO from "../components/SEO"; |
| 7 | +import Text from "../components/Text"; |
| 8 | +import TorrentList from "../components/TorrentList"; |
| 9 | + |
| 10 | +const Bookmarks = ({ results }) => { |
| 11 | + const { |
| 12 | + publicRuntimeConfig: { SQ_TORRENT_CATEGORIES }, |
| 13 | + } = getConfig(); |
| 14 | + |
| 15 | + return ( |
| 16 | + <> |
| 17 | + <SEO title="Your bookmarks" /> |
| 18 | + <Text as="h1" mb={5}> |
| 19 | + Your bookmarks |
| 20 | + </Text> |
| 21 | + {results.torrents.length ? ( |
| 22 | + <TorrentList |
| 23 | + torrents={results.torrents} |
| 24 | + categories={SQ_TORRENT_CATEGORIES} |
| 25 | + total={results.total} |
| 26 | + /> |
| 27 | + ) : ( |
| 28 | + <Text color="grey">You do not have any bookmarks.</Text> |
| 29 | + )} |
| 30 | + </> |
| 31 | + ); |
| 32 | +}; |
| 33 | + |
| 34 | +export const getServerSideProps = withAuthServerSideProps( |
| 35 | + async ({ token, fetchHeaders }) => { |
| 36 | + if (!token) return { props: {} }; |
| 37 | + |
| 38 | + const { |
| 39 | + publicRuntimeConfig: { SQ_API_URL }, |
| 40 | + } = getConfig(); |
| 41 | + |
| 42 | + try { |
| 43 | + const bookmarksRes = await fetch(`${SQ_API_URL}/account/bookmarks`, { |
| 44 | + headers: fetchHeaders, |
| 45 | + }); |
| 46 | + if ( |
| 47 | + bookmarksRes.status === 403 && |
| 48 | + (await bookmarksRes.text()) === "User is banned" |
| 49 | + ) { |
| 50 | + throw "banned"; |
| 51 | + } else { |
| 52 | + const results = await bookmarksRes.json(); |
| 53 | + return { props: { results } }; |
| 54 | + } |
| 55 | + } catch (e) { |
| 56 | + if (e === "banned") throw "banned"; |
| 57 | + return { props: { results: { torrents: [] } } }; |
| 58 | + } |
| 59 | + } |
| 60 | +); |
| 61 | + |
| 62 | +export default Bookmarks; |
0 commit comments