1
- import { useSupabaseClient } from "@supabase/auth-helpers-react" ;
1
+ import { useSession , useSupabaseClient } from "@supabase/auth-helpers-react" ;
2
2
import { ConnectWallet , useStorageUpload , MediaRenderer } from "@thirdweb-dev/react" ;
3
3
import { NextPage } from "next" ;
4
4
import { useCallback , useEffect , useState } from "react" ;
@@ -9,6 +9,8 @@ const IPFS_GATEWAY = "https://d4b4e2e663a6efce5f7f8310426ba24a.ipfscdn.io/ipfs/"
9
9
10
10
export default function FileUpload ( ) {
11
11
const supabase = useSupabaseClient ( ) ;
12
+ const session = useSession ( ) ;
13
+
12
14
const [ uris , setUris ] = useState < string [ ] > ( [ ] ) ;
13
15
14
16
const { mutateAsync : upload } = useStorageUpload ( ) ;
@@ -18,7 +20,7 @@ export default function FileUpload() {
18
20
const uris = await upload ( { data : files } ) ;
19
21
20
22
// Add URIs to Supabase table
21
- const { error, data } = await supabase . from ( 'comments' ) . insert ( uris . map ( uri => ( { content : uri } ) ) ) ;
23
+ const { error, data } = await supabase . from ( 'comments' ) . insert ( uris . map ( uri => ( { content : uri , author : session ?. user ?. id } ) ) ) ;
22
24
23
25
if ( error ) {
24
26
console . error ( "Error inserting data into Supabase:" , error . message ) ;
@@ -33,9 +35,36 @@ export default function FileUpload() {
33
35
}
34
36
} , [ upload , supabase ] ) ;
35
37
38
+ const fetchAndRenderFromSupabase = useCallback ( async ( ) => {
39
+ try {
40
+ const { data, error } = await supabase
41
+ . from ( "comments" )
42
+ . select ( "content" )
43
+ . gte ( "id" , 23 )
44
+ . lt ( "id" , 29 )
45
+ . eq ( "author" , session ?. user ?. id ) ;
46
+
47
+ if ( error ) {
48
+ console . error ( "Error fetching files from Supabase:" , error . message ) ;
49
+ return ;
50
+ }
51
+
52
+ if ( data ) {
53
+ const urisFromSupabase = data . map ( ( row : any ) => row . content ) ;
54
+ setUris ( urisFromSupabase ) ;
55
+ }
56
+ } catch ( error : any ) {
57
+ console . error ( "Error fetching files from Supabase:" , error . message ) ;
58
+ }
59
+ } , [ session ?. user ?. id , supabase ] ) ;
60
+
61
+ useEffect ( ( ) => {
62
+ fetchAndRenderFromSupabase ( ) ;
63
+ } , [ fetchAndRenderFromSupabase ] ) ;
64
+
36
65
const onDrop = useCallback ( async ( acceptedFiles : File [ ] ) => {
37
66
await uploadAndAddToSupabase ( acceptedFiles ) ;
38
- } , [ uploadAndAddToSupabase ] ) ;
67
+ } , [ ] ) ;
39
68
40
69
const { getRootProps, getInputProps } = useDropzone ( { onDrop } ) ;
41
70
@@ -78,22 +107,6 @@ export default function FileUpload() {
78
107
}
79
108
*/
80
109
81
- const fetchAndRenderFromSupabase = async ( ) => {
82
- try {
83
- const { data, error } = await supabase . from ( "files" ) . select ( "file_url" ) ;
84
- if ( error ) {
85
- console . error ( "Error fetching files from Supabase:" , error . message ) ;
86
- return ;
87
- }
88
- if ( data ) {
89
- const urisFromSupabase = data . map ( ( row : any ) => row . file_url ) ;
90
- setUris ( urisFromSupabase ) ;
91
- }
92
- } catch ( error : any ) {
93
- console . error ( "Error fetching files from Supabase:" , error . message ) ;
94
- }
95
- } ;
96
-
97
110
useEffect ( ( ) => {
98
111
fetchAndRenderFromSupabase ( ) ;
99
112
} , [ ] ) ;
0 commit comments