1
+ 'use client' ;
2
+
3
+ import React , { useEffect , useState } from "react" ;
4
+ import { PostCardSingle } from "@/content/Posts/PostSingle" ;
5
+ import { useSession , useSupabaseClient } from "@supabase/auth-helpers-react" ;
6
+ import StarnetLayout from "@/components/Layout/Starnet" ;
7
+
8
+ interface Classification {
9
+ id : number ;
10
+ created_at : string ;
11
+ content : string | null ;
12
+ author : string | null ;
13
+ anomaly : number | null ;
14
+ media : any | null ;
15
+ classificationtype : string | null ;
16
+ classificationConfiguration : any | null ;
17
+ category : string ;
18
+ tags : string [ ] ;
19
+ } ;
20
+
21
+ export default function Starnet ( ) {
22
+ const supabase = useSupabaseClient ( ) ;
23
+ const session = useSession ( ) ;
24
+
25
+ const [ classifications , setClassifications ] = useState < Classification [ ] > ( [ ] ) ;
26
+ const [ loading , setLoading ] = useState < boolean > ( true ) ;
27
+ const [ error , setError ] = useState < string | null > ( null ) ;
28
+
29
+ const fetchClassifications = async ( ) => {
30
+ if ( ! session ?. user ) {
31
+ setError ( "User session not found." ) ;
32
+ setLoading ( false ) ;
33
+ return ;
34
+ }
35
+
36
+ setLoading ( true ) ;
37
+ setError ( null ) ;
38
+ try {
39
+ const { data, error } = await supabase
40
+ . from ( 'classifications' )
41
+ . select ( '*' )
42
+ . eq ( 'author' , session . user . id )
43
+ . order ( 'created_at' , { ascending : false } ) as { data : Classification [ ] ; error : any } ;
44
+
45
+ if ( error ) throw error ;
46
+
47
+ const processedData = data . map ( ( classification ) => {
48
+ const media = classification . media ;
49
+ let images : string [ ] = [ ] ;
50
+
51
+ // Ensure 'images' is always an array
52
+ if ( Array . isArray ( media ) ) {
53
+ if ( media . length === 2 && typeof media [ 1 ] === "string" ) {
54
+ images . push ( media [ 1 ] ) ;
55
+ }
56
+ } else if ( media && typeof media . uploadUrl === 'string' ) {
57
+ images . push ( media . uploadUrl ) ;
58
+ }
59
+
60
+ const votes = classification . classificationConfiguration ?. votes || 0 ;
61
+
62
+ return { ...classification , images, votes } ;
63
+ } ) ;
64
+
65
+ setClassifications ( processedData ) ;
66
+ } catch ( error ) {
67
+ console . error ( "Error fetching classifications:" , error ) ;
68
+ setError ( "Failed to load classifications." ) ;
69
+ } finally {
70
+ setLoading ( false ) ;
71
+ }
72
+ } ;
73
+
74
+ useEffect ( ( ) => {
75
+ fetchClassifications ( ) ;
76
+ } , [ session ] ) ;
77
+
78
+ const handleVote = async ( classificationId : number , currentConfig : any ) => {
79
+ try {
80
+ const currentVotes = currentConfig ?. votes || 0 ;
81
+
82
+ const updatedConfig = {
83
+ ...currentConfig ,
84
+ votes : currentVotes + 1 ,
85
+ } ;
86
+
87
+ const { error } = await supabase
88
+ . from ( "classifications" )
89
+ . update ( { classificationConfiguration : updatedConfig } )
90
+ . eq ( "id" , classificationId ) ;
91
+
92
+ if ( error ) {
93
+ console . error ( "Error updating classificationConfiguration:" , error ) ;
94
+ } else {
95
+ setClassifications ( ( prevClassifications ) =>
96
+ prevClassifications . map ( ( classification ) =>
97
+ classification . id === classificationId
98
+ ? { ...classification , votes : updatedConfig . votes }
99
+ : classification
100
+ )
101
+ ) ;
102
+ }
103
+ } catch ( error ) {
104
+ console . error ( "Error voting:" , error ) ;
105
+ }
106
+ } ;
107
+
108
+ return (
109
+ < StarnetLayout >
110
+ < div className = "space-y-8" >
111
+ { loading ? (
112
+ < p > Loading classifications...</ p >
113
+ ) : error ? (
114
+ < p > { error } </ p >
115
+ ) : (
116
+ classifications . map ( ( classification ) => (
117
+ < PostCardSingle
118
+ key = { classification . id }
119
+ classificationId = { classification . id }
120
+ title = { classification . content || "Untitled" }
121
+ author = { classification . author || "Unknown" }
122
+ content = { classification . content || "" }
123
+ category = { classification . category }
124
+ tags = { classification . tags || [ ] }
125
+ images = { classification . media || [ ] }
126
+ anomalyId = { classification . anomaly ?. toString ( ) || "" }
127
+ classificationConfig = { classification . classificationConfiguration }
128
+ classificationType = { classification . classificationtype || "Unknown" }
129
+ onVote = { ( ) => handleVote ( classification . id , classification . classificationConfiguration ) }
130
+ votes = { 0 }
131
+ />
132
+ ) )
133
+ ) }
134
+ </ div >
135
+ </ StarnetLayout >
136
+ ) ;
137
+ } ;
0 commit comments