Skip to content

Commit

Permalink
Merge pull request #2 from metrue/domain-binding
Browse files Browse the repository at this point in the history
Read the github username from environment variable when deployment to own domain
  • Loading branch information
metrue authored Oct 13, 2024
2 parents 56cc72d + 424640c commit 16e81f1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 20 deletions.
27 changes: 25 additions & 2 deletions app/blog/page.tsx
Original file line number Diff line number Diff line change
@@ -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 <GitHubSignInButton />;
const username = process.env.GITHUB_USERNAME ?? '';

if (username) {
const octokit = new Octokit();
const blogPosts = await getBlogPostsPublic(
octokit,
username,
"tinymind-blog"
);

return (
<div className="max-w-4xl mx-auto px-4 py-8">
<div className="max-w-2xl mx-auto">
<PublicBlogList posts={blogPosts} username={username} />
</div>
</div>
);
} else {
return (
<GitHubSignInButton />
);
}
}

try {
Expand Down
6 changes: 5 additions & 1 deletion app/editor/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export default async function EditorPage({
const defaultType = searchParams.type === "blog" ? "blog" : "thought";

if (!session) {
return <GitHubSignInButton />;

const username = process.env.GITHUB_USERNAME;
if (!username) {
return <GitHubSignInButton />;
}
}

return (
Expand Down
4 changes: 3 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import ThoughtsList from "@/components/ThoughtsList";

export default function Home() {
const username = process.env.GITHUB_USERNAME ?? ''

return (
<div className="flex flex-col min-h-screen">
<main className="flex-grow container mx-auto px-4 sm:px-6">
<ThoughtsList />
<ThoughtsList username={username} />
</main>
</div>
);
Expand Down
28 changes: 25 additions & 3 deletions app/thoughts/page.tsx
Original file line number Diff line number Diff line change
@@ -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 <GitHubSignInButton />;
if (username) {
const octokit = new Octokit();
const blogPosts = await getThoughtsPublic(
octokit,
process.env.GITHUB_USERNAME ?? '',
"tinymind-blog"
);
return (
<div className="max-w-4xl mx-auto px-4 py-8">
<div className="max-w-2xl mx-auto">
<PublicThoughtsList thoughts={blogPosts} />
</div>
</div>
);
} else {
return (
<GitHubSignInButton />
);
}
}

return <ThoughtsList />;
return <ThoughtsList username={username} />;
}
27 changes: 14 additions & 13 deletions components/ThoughtsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand All @@ -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<Thought[]>([]);
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
Expand All @@ -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) {
Expand All @@ -79,7 +78,7 @@ export default function ThoughtsList() {
}

fetchThoughts();
}, [session, status]);
}, [session, status, username]);

const handleDeleteThought = async (id: string) => {
if (!session?.accessToken) {
Expand Down Expand Up @@ -125,7 +124,9 @@ export default function ThoughtsList() {
};

if (status === "unauthenticated" || error === "authentication_failed") {
return <GitHubSignInButton />;
if (!username) {
return <GitHubSignInButton />;
}
}

if (error && error !== "authentication_failed") {
Expand Down

0 comments on commit 16e81f1

Please sign in to comment.