From 27497f4b48e99e6027176e87b1d44ff8c2fd4f5e Mon Sep 17 00:00:00 2001 From: Minghe Huang Date: Sat, 5 Oct 2024 22:19:38 +0200 Subject: [PATCH] try domain --- app/blog/page.tsx | 27 +++++++++++++++++++++++++-- app/editor/page.tsx | 6 +++++- app/page.tsx | 4 +++- app/thoughts/page.tsx | 28 +++++++++++++++++++++++++--- components/ThoughtsList.tsx | 27 ++++++++++++++------------- 5 files changed, 72 insertions(+), 20 deletions(-) diff --git a/app/blog/page.tsx b/app/blog/page.tsx index 787ede2..6a9f80f 100644 --- a/app/blog/page.tsx +++ b/app/blog/page.tsx @@ -1,14 +1,37 @@ import { getServerSession } from "next-auth/next"; import { authOptions } from "@/lib/auth"; import BlogList from "@/components/BlogList"; -import { getBlogPosts } from "@/lib/githubApi"; import GitHubSignInButton from "@/components/GitHubSignInButton"; +import { getBlogPosts, getBlogPostsPublic } from "@/lib/githubApi"; +import { Octokit } from "@octokit/rest"; +import PublicBlogList from "@/components/PublicBlogList"; export default async function BlogPage() { const session = await getServerSession(authOptions); if (!session || !session.accessToken) { - return ; + const username = process.env.GITHUB_USERNAME ?? ''; + + if (username) { + const octokit = new Octokit(); + const blogPosts = await getBlogPostsPublic( + octokit, + username, + "tinymind-blog" + ); + + return ( +
+
+ +
+
+ ); + } else { + return ( + + ); + } } try { diff --git a/app/editor/page.tsx b/app/editor/page.tsx index 0e3081c..0d716a7 100644 --- a/app/editor/page.tsx +++ b/app/editor/page.tsx @@ -12,7 +12,11 @@ export default async function EditorPage({ const defaultType = searchParams.type === "blog" ? "blog" : "thought"; if (!session) { - return ; + + const username = process.env.GITHUB_USERNAME; + if (!username) { + return ; + } } return ( diff --git a/app/page.tsx b/app/page.tsx index 85fe210..e1427f7 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,10 +1,12 @@ import ThoughtsList from "@/components/ThoughtsList"; export default function Home() { + const username = process.env.GITHUB_USERNAME ?? '' + return (
- +
); diff --git a/app/thoughts/page.tsx b/app/thoughts/page.tsx index e93faa3..c6d79cb 100644 --- a/app/thoughts/page.tsx +++ b/app/thoughts/page.tsx @@ -1,14 +1,36 @@ import { getServerSession } from "next-auth/next"; import { authOptions } from "@/lib/auth"; -import ThoughtsList from "@/components/ThoughtsList"; import GitHubSignInButton from "@/components/GitHubSignInButton"; +import ThoughtsList from "@/components/ThoughtsList"; +import { getThoughtsPublic } from "@/lib/githubApi"; +import { Octokit } from "@octokit/rest"; +import PublicThoughtsList from "@/components/PublicThoughtsList"; export default async function ThoughtsPage() { const session = await getServerSession(authOptions); + const username = process.env.GITHUB_USERNAME ?? ''; if (!session || !session.accessToken) { - return ; + if (username) { + const octokit = new Octokit(); + const blogPosts = await getThoughtsPublic( + octokit, + process.env.GITHUB_USERNAME ?? '', + "tinymind-blog" + ); + return ( +
+
+ +
+
+ ); + } else { + return ( + + ); + } } - return ; + return ; } diff --git a/components/ThoughtsList.tsx b/components/ThoughtsList.tsx index 555b34b..07073d8 100644 --- a/components/ThoughtsList.tsx +++ b/components/ThoughtsList.tsx @@ -2,7 +2,8 @@ import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; -import { getThoughts, Thought } from "@/lib/githubApi"; +import { getThoughts, getThoughtsPublic, Thought } from "@/lib/githubApi"; +import { Octokit } from "@octokit/rest"; import GitHubSignInButton from "./GitHubSignInButton"; import { useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; @@ -16,7 +17,7 @@ import { DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, -} from "@/components/ui/dropdown-menu"; +} from "@/components/ui/dropdown-menu" import { AiOutlineEllipsis } from "react-icons/ai"; import { useToast } from "@/components/ui/use-toast"; import { @@ -33,7 +34,7 @@ import remarkMath from "remark-math"; import rehypeKatex from "rehype-katex"; import { formatTimestamp } from "@/utils/dateFormatting"; -export default function ThoughtsList() { +export default function ThoughtsList({ username }: { username: string }) { const [thoughts, setThoughts] = useState([]); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(true); @@ -48,18 +49,16 @@ export default function ThoughtsList() { async function fetchThoughts() { if (status === "loading") return; if (status === "unauthenticated") { - setError("Please log in to view thoughts"); setIsLoading(false); - return; - } - if (!session?.accessToken) { - setError("Access token not available"); - setIsLoading(false); - return; } try { - const fetchedThoughts = await getThoughts(session.accessToken); + const octokit = new Octokit(); + const fetchedThoughts = session?.accessToken + ? await getThoughts(session.accessToken) + : username + ? await getThoughtsPublic(octokit, username, "tinymind-blog") + : []; setThoughts(fetchedThoughts); setError(null); } catch (error) { @@ -79,7 +78,7 @@ export default function ThoughtsList() { } fetchThoughts(); - }, [session, status]); + }, [session, status, username]); const handleDeleteThought = async (id: string) => { if (!session?.accessToken) { @@ -125,7 +124,9 @@ export default function ThoughtsList() { }; if (status === "unauthenticated" || error === "authentication_failed") { - return ; + if (!username) { + return ; + } } if (error && error !== "authentication_failed") {