Skip to content

Commit 2782188

Browse files
committed
πŸ§‘πŸ»β€πŸ’ΌπŸͺ² ↝ Projects now ref'd by ipfs upload
1 parent 8f0dfd6 commit 2782188

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React, { useEffect, useState } from "react";
2+
import { useSession, useSupabaseClient } from "@supabase/auth-helpers-react";
3+
import dayjs from "dayjs";
4+
5+
interface Post {
6+
id: number;
7+
content: string;
8+
author: string;
9+
created_at: string;
10+
planets: string;
11+
}
12+
13+
const CheckPostsOld: React.FC = () => {
14+
const supabase = useSupabaseClient();
15+
const session = useSession();
16+
const [posts, setPosts] = useState<Post[]>([]);
17+
18+
useEffect(() => {
19+
const fetchPosts = async () => {
20+
try {
21+
const { data, error } = await supabase
22+
.from("posts_old")
23+
.select("*")
24+
.eq("author", session?.user?.id)
25+
.gte("created_at", dayjs("2024-01-01").toISOString()); // Filter posts after Jan 1, 2024
26+
27+
if (error) {
28+
console.error("Error fetching posts:", error.message);
29+
return;
30+
}
31+
32+
if (data) {
33+
setPosts(data);
34+
}
35+
} catch (error) {
36+
console.error("Error fetching posts:", error.message);
37+
}
38+
};
39+
40+
if (session?.user?.id) {
41+
fetchPosts();
42+
}
43+
}, [supabase, session]);
44+
45+
return (
46+
<div className="p-4">
47+
<h2 className="text-xl font-bold mb-4">Your project nodes after January 1, 2024:</h2>
48+
{posts.length === 0 ? (
49+
<p>No project nodes found after January 1, 2024.</p>
50+
) : (
51+
<ul>
52+
{posts.map((post) => (
53+
<li key={post.id} className="mb-4">
54+
<p>{post.content}</p>
55+
<p className="text-sm text-gray-500">Created at: {post.created_at}</p>
56+
</li>
57+
))}
58+
</ul>
59+
)}
60+
</div>
61+
);
62+
};
63+
64+
export default CheckPostsOld;
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { useSession, useSupabaseClient } from "@supabase/auth-helpers-react";
2+
import { ConnectWallet, useStorageUpload, MediaRenderer } from "@thirdweb-dev/react";
3+
import { NextPage } from "next";
4+
import { useCallback, useEffect, useState } from "react";
5+
import { useDropzone } from "react-dropzone";
6+
import { useMutation, useQuery } from "react-query";
7+
8+
const IPFS_GATEWAY = "https://d4b4e2e663a6efce5f7f8310426ba24a.ipfscdn.io/ipfs/";
9+
10+
interface FileUploadProps {
11+
projectId: number; // Pass project ID as prop
12+
}
13+
14+
const UploadFileToRepo: NextPage<FileUploadProps> = ({ projectId }) => {
15+
const supabase = useSupabaseClient();
16+
const session = useSession();
17+
18+
const [uris, setUris] = useState<string[]>([]);
19+
20+
const { mutateAsync: upload } = useStorageUpload();
21+
22+
const uploadAndAddToSupabase = useCallback(async (files: File[]) => {
23+
try {
24+
const uris = await upload({ data: files });
25+
26+
// Add URIs to Supabase table
27+
const { error, data } = await supabase.from('comments').insert(
28+
uris.map(uri => ({
29+
content: uri,
30+
author: session?.user?.id,
31+
parent: projectId, // Associate with project ID
32+
}))
33+
);
34+
35+
if (error) {
36+
console.error("Error inserting data into Supabase:", error.message);
37+
return;
38+
};
39+
40+
console.log("Data inserted successfully:", data);
41+
42+
setUris(uris);
43+
} catch (error) {
44+
console.error("Error uploading files and adding to Supabase:", error);
45+
}
46+
}, [upload, supabase, session?.user?.id, projectId]);
47+
48+
const fetchAndRenderFromSupabase = useCallback(async () => {
49+
try {
50+
const { data, error } = await supabase
51+
.from("comments")
52+
.select("content")
53+
.gte("id", 23)
54+
.lt("id", 29)
55+
.eq("author", session?.user?.id)
56+
.eq("parent", projectId); // Fetch files associated with project ID
57+
58+
if (error) {
59+
console.error("Error fetching files from Supabase:", error.message);
60+
return;
61+
}
62+
63+
if (data) {
64+
const urisFromSupabase = data.map((row: any) => row.content);
65+
setUris(urisFromSupabase);
66+
}
67+
} catch (error: any) {
68+
console.error("Error fetching files from Supabase:", error.message);
69+
}
70+
}, [session?.user?.id, supabase, projectId]);
71+
72+
useEffect(() => {
73+
fetchAndRenderFromSupabase();
74+
}, [fetchAndRenderFromSupabase]);
75+
76+
const onDrop = useCallback(async (acceptedFiles: File[]) => {
77+
await uploadAndAddToSupabase(acceptedFiles);
78+
}, [uploadAndAddToSupabase]);
79+
80+
const { getRootProps, getInputProps } = useDropzone({ onDrop });
81+
82+
return (
83+
<div>
84+
<div {...getRootProps()}>
85+
<input {...getInputProps()} />
86+
<button>Drop files to upload them to IPFS</button>
87+
</div>
88+
<div>
89+
{uris.map((uri) => (
90+
<MediaRenderer key={uri} src={uri} alt="image" />
91+
))}
92+
</div>
93+
</div>
94+
);
95+
};
96+
97+
export default UploadFileToRepo;

β€Žpackage.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@thirdweb-dev/sdk": "^4",
3030
"axios": "^1.6.2",
3131
"clsx": "^2.1.0",
32+
"dayjs": "^1.11.10",
3233
"ethers": "^5",
3334
"lucide-react": "^0.293.0",
3435
"next": "^13.0.2",

β€Žpages/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useRouter } from "next/router";
55
import { Metadata } from "next";
66
import FileUpload from "../components/Content/FileUpload";
77
import CreateNode from "../components/Content/CreateRepo";
8+
import CheckPostsOld from "../components/Content/CheckUserData";
89

910
export const metadata: Metadata = {
1011
title: "Star Sailors",
@@ -27,6 +28,7 @@ export default function Home() {
2728
{/* {userId} */}
2829
<div className="flex flex-col gap-4">
2930
<CreateNode />
31+
<CheckPostsOld />
3032
<FileUpload />
3133
</div>
3234
</Layout>

β€Žpages/project/[id].tsx

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { useRouter } from "next/router";
2+
import { useEffect, useState } from "react";
3+
import { useSupabaseClient, useSession } from "@supabase/auth-helpers-react";
4+
import Layout from "../../components/Section/Layout";
5+
import UploadFileToRepo from "../../components/Content/UploadToProject";
6+
7+
interface Post {
8+
id: number;
9+
content: string;
10+
author: string;
11+
created_at: string;
12+
media: any[];
13+
planets: string;
14+
}
15+
16+
const ProjectDetailPage = () => {
17+
const router = useRouter();
18+
const { id } = router.query;
19+
const supabase = useSupabaseClient();
20+
const [post, setPost] = useState<Post | null>(null);
21+
22+
useEffect(() => {
23+
const fetchPost = async () => {
24+
try {
25+
if (!id || Array.isArray(id)) return;
26+
27+
const postId = parseInt(id as string);
28+
29+
const { data, error } = await supabase
30+
.from("posts_old")
31+
.select("*")
32+
.eq("id", postId)
33+
.single();
34+
35+
if (error) {
36+
console.error("Error fetching post:", error.message);
37+
return;
38+
}
39+
40+
if (data) {
41+
setPost(data);
42+
}
43+
} catch (error) {
44+
console.error("Error fetching post:", error.message);
45+
}
46+
};
47+
48+
fetchPost();
49+
}, [id, supabase]);
50+
51+
if (!post) {
52+
return <div>Loading...</div>;
53+
}
54+
55+
return (
56+
<Layout>
57+
<div>
58+
<h1>Project Detail</h1>
59+
<p>ID: {post.id}</p>
60+
<p>Content: {post.content}</p>
61+
<p>Author: {post.author}</p>
62+
<p>Created At: {post.created_at}</p>
63+
<UploadFileToRepo projectId={post.id} />
64+
</div>
65+
</Layout>
66+
);
67+
};
68+
69+
export default ProjectDetailPage;

β€Žyarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -4380,6 +4380,11 @@ dashdash@^1.12.0:
43804380
dependencies:
43814381
assert-plus "^1.0.0"
43824382

4383+
dayjs@^1.11.10:
4384+
version "1.11.10"
4385+
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.10.tgz#68acea85317a6e164457d6d6947564029a6a16a0"
4386+
integrity sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==
4387+
43834388
death@^1.1.0:
43844389
version "1.1.0"
43854390
resolved "https://registry.npmjs.org/death/-/death-1.1.0.tgz"

0 commit comments

Comments
Β (0)