@@ -4,10 +4,11 @@ import Link from "next/link";
44import styled from "styled-components" ;
55import css from "@styled-system/css" ;
66import slugify from "slugify" ;
7- import { withAuth } from "../../utils/withAuth" ;
7+ import { withAuth , withAuthServerSideProps } from "../../utils/withAuth" ;
88import SEO from "../../components/SEO" ;
99import Box from "../../components/Box" ;
1010import Text from "../../components/Text" ;
11+ import qs from "qs" ;
1112
1213const CategoryItem = styled . li ( ( ) =>
1314 css ( {
@@ -24,7 +25,7 @@ const CategoryItem = styled.li(() =>
2425 } )
2526) ;
2627
27- const Categories = ( ) => {
28+ const Categories = ( { tags } ) => {
2829 const {
2930 publicRuntimeConfig : { SQ_TORRENT_CATEGORIES } ,
3031 } = getConfig ( ) ;
@@ -35,30 +36,91 @@ const Categories = () => {
3536 < Text as = "h1" mb = { 5 } >
3637 Categories
3738 </ Text >
38- { Object . keys ( SQ_TORRENT_CATEGORIES ) . length ? (
39- < Box
40- as = "ul"
41- display = "grid"
42- gridTemplateColumns = { [ "1fr" , "repeat(4, 1fr)" ] }
43- gridGap = { 4 }
44- _css = { { pl : 0 , listStyle : "none" } }
45- >
46- { Object . keys ( SQ_TORRENT_CATEGORIES ) . map ( ( category ) => (
47- < CategoryItem key = { category } >
48- < Link
49- href = { `/categories/${ slugify ( category , { lower : true } ) } ` }
50- passHref
51- >
52- < a > { category } </ a >
39+ < Box mb = { 5 } >
40+ { Object . keys ( SQ_TORRENT_CATEGORIES ) . length ? (
41+ < Box
42+ as = "ul"
43+ display = "grid"
44+ gridTemplateColumns = { [ "1fr" , "repeat(4, 1fr)" ] }
45+ gridGap = { 4 }
46+ _css = { { pl : 0 , listStyle : "none" } }
47+ >
48+ { Object . keys ( SQ_TORRENT_CATEGORIES ) . map ( ( category ) => (
49+ < CategoryItem key = { category } >
50+ < Link
51+ href = { `/categories/${ slugify ( category , { lower : true } ) } ` }
52+ passHref
53+ >
54+ < a > { category } </ a >
55+ </ Link >
56+ </ CategoryItem >
57+ ) ) }
58+ </ Box >
59+ ) : (
60+ < Text color = "grey" > No categories have been defined.</ Text >
61+ ) }
62+ </ Box >
63+ < Text as = "h1" mb = { 5 } >
64+ Tags
65+ </ Text >
66+ { tags . length ? (
67+ < Box display = "flex" flexWrap = "wrap" ml = { - 1 } mt = { - 1 } >
68+ { tags . map ( ( tag ) => (
69+ < Box
70+ key = { `tag-${ tag } ` }
71+ bg = "sidebar"
72+ border = "1px solid"
73+ borderColor = "border"
74+ borderRadius = { 1 }
75+ m = { 1 }
76+ >
77+ < Link href = { `/tags/${ tag } ` } passHref >
78+ < Text
79+ as = "a"
80+ display = "block"
81+ color = "text"
82+ _css = { { "&:visited" : { color : "text" } } }
83+ px = { 3 }
84+ py = { 1 }
85+ >
86+ { tag }
87+ </ Text >
5388 </ Link >
54- </ CategoryItem >
89+ </ Box >
5590 ) ) }
5691 </ Box >
5792 ) : (
58- < Text color = "grey" > No categories have been defined.</ Text >
93+ < Text color = "grey" > No tags have been defined.</ Text >
5994 ) }
6095 </ >
6196 ) ;
6297} ;
6398
64- export default withAuth ( Categories ) ;
99+ export const getServerSideProps = withAuthServerSideProps (
100+ async ( { token, fetchHeaders } ) => {
101+ if ( ! token ) return { props : { } } ;
102+
103+ const {
104+ publicRuntimeConfig : { SQ_API_URL } ,
105+ } = getConfig ( ) ;
106+
107+ try {
108+ const tagsRes = await fetch ( `${ SQ_API_URL } /torrent/tags` , {
109+ headers : fetchHeaders ,
110+ } ) ;
111+ if (
112+ tagsRes . status === 403 &&
113+ ( await tagsRes . text ( ) ) === "User is banned"
114+ ) {
115+ throw "banned" ;
116+ }
117+ const tags = await tagsRes . json ( ) ;
118+ return { props : { tags } } ;
119+ } catch ( e ) {
120+ if ( e === "banned" ) throw "banned" ;
121+ return { props : { } } ;
122+ }
123+ }
124+ ) ;
125+
126+ export default Categories ;
0 commit comments