@@ -4,7 +4,7 @@ import { useSearchParams } from "next/navigation";
4
4
import { useCallback , useEffect , useState } from "react" ;
5
5
import axios , { type AxiosError } from "axios" ;
6
6
import { Button } from "@/components/ui/button" ;
7
- import { type IPaper , type Filters } from "@/interface" ;
7
+ import { type IPaper , type Filters , IRelatedSubject , StoredSubjects } from "@/interface" ;
8
8
import Card from "./Card" ;
9
9
import { useRouter } from "next/navigation" ;
10
10
import Loader from "./ui/loader" ;
@@ -13,8 +13,8 @@ import Error from "./Error";
13
13
import { Filter } from "lucide-react" ;
14
14
import { Sheet , SheetContent , SheetTrigger } from "./ui/sheet" ;
15
15
import { Pin } from "lucide-react" ;
16
- import { StoredSubjects } from "@/interface" ;
17
16
import { getSecureUrl , generateFileName , downloadFile } from "@/util/download" ;
17
+ import Link from "next/link" ;
18
18
19
19
const CatalogueContent = ( ) => {
20
20
const router = useRouter ( ) ;
@@ -39,6 +39,29 @@ const CatalogueContent = () => {
39
39
const [ filtersPulled , setFiltersPulled ] = useState < boolean > ( false ) ;
40
40
const [ appliedFilters , setAppliedFilters ] = useState < boolean > ( false ) ;
41
41
const [ pinned , setPinned ] = useState < boolean > ( false ) ;
42
+ const [ relatedSubjects , setRelatedSubjects ] = useState < string [ ] > ( [ ] ) ;
43
+ // Fetch related subjects when subject changes
44
+ useEffect ( ( ) => {
45
+ if ( ! subject ) return ;
46
+ const fetchRelatedSubjects = async ( ) => {
47
+ try {
48
+ const res = await axios . get < { related_subjects : string [ ] } > ( "/api/related-subject" , {
49
+ params : { subject } ,
50
+ } ) ;
51
+ console . log ( res . data )
52
+ const data = res . data . related_subjects ;
53
+ console . log ( "data" , data [ 0 ] , data [ 1 ] ) ;
54
+ if ( data && data . length > 0 ) {
55
+ setRelatedSubjects ( data ) ;
56
+ } else {
57
+ setRelatedSubjects ( [ ] ) ;
58
+ }
59
+ } catch ( e ) {
60
+ setRelatedSubjects ( [ ] ) ;
61
+ }
62
+ } ;
63
+ void fetchRelatedSubjects ( ) ;
64
+ } , [ subject ] ) ;
42
65
43
66
// Set initial state from searchParams on client-side mount
44
67
useEffect ( ( ) => {
@@ -317,22 +340,38 @@ const CatalogueContent = () => {
317
340
</ SheetContent >
318
341
</ Sheet >
319
342
320
- < div className = "flex items-center gap-2 p-7" >
321
- < div >
322
- < p className = "text-s font-semibold text-gray-700 dark:text-white/80" >
323
- { subject ?. split ( "[" ) [ 1 ] ?. replace ( "]" , "" ) }
324
- </ p >
325
- < h2 className = "text-2xl font-extrabold text-gray-700 dark:text-white md:text-3xl" >
326
- { subject ?. split ( " [" ) [ 0 ] }
327
- </ h2 >
328
- </ div >
329
- < div className = "mt-7" >
330
- < button onClick = { handlePinToggle } >
331
- < Pin
332
- className = { `h-7 w-7 ${ pinned ? "fill-[#A78BFA]" : "" } stroke-gray-700 dark:stroke-white` }
333
- />
334
- </ button >
343
+ < div className = "p-7" >
344
+ < div className = "flex items-center gap-2" >
345
+ < div >
346
+ < p className = "text-s font-semibold text-gray-700 dark:text-white/80" >
347
+ { subject ?. split ( "[" ) [ 1 ] ?. replace ( "]" , "" ) }
348
+ </ p >
349
+ < h2 className = "text-2xl font-extrabold text-gray-700 dark:text-white md:text-3xl" >
350
+ { subject ?. split ( " [" ) [ 0 ] }
351
+ </ h2 >
352
+ </ div >
353
+ < div className = "mt-7" >
354
+ < button onClick = { handlePinToggle } >
355
+ < Pin
356
+ className = { `h-7 w-7 ${ pinned ? "fill-[#A78BFA]" : "" } stroke-gray-700 dark:stroke-white` }
357
+ />
358
+ </ button >
359
+ </ div >
335
360
</ div >
361
+ { relatedSubjects . length > 0 && (
362
+ < div className = "mt-3 flex flex-wrap items-center gap-2" >
363
+ < span className = "text-sm font-medium text-gray-500 dark:text-gray-300 mr-2" > Related subjects:</ span >
364
+ { relatedSubjects . map ( ( sub ) => (
365
+ < Link
366
+ key = { sub }
367
+ href = { `/catalogue?subject=${ encodeURIComponent ( sub ) } ` }
368
+ className = "rounded-full bg-violet-100 px-3 py-1 text-sm font-semibold text-violet-700 transition-colors hover:bg-violet-200 dark:bg-violet-900 dark:text-violet-200 dark:hover:bg-violet-800"
369
+ >
370
+ { sub }
371
+ </ Link >
372
+ ) ) }
373
+ </ div >
374
+ ) }
336
375
</ div >
337
376
338
377
{ loading ? (
0 commit comments