Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/api/activity/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { NextRequest, NextResponse } from "next/server";
import { createClient } from '@supabase/supabase-js';

const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
);

export async function POST(req: NextRequest) {
try {
const body = await req.json();
const { event_type, repo, username, action, payload } = body;
const { error } = await supabase.from('activities').insert({
event_type,
repo,
username,
action,
payload
});
if (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
return NextResponse.json({ ok: true });
} catch (err) {
return NextResponse.json({ error: 'Invalid request' }, { status: 400 });
}
}
16 changes: 16 additions & 0 deletions app/api/get-activity/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NextRequest, NextResponse } from "next/server";
import { getActivityLogs } from "@/lib/supabase/getActivityLogs";

export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const limit = Number(searchParams.get("limit")) || 50;
const repo = searchParams.get("repo") || undefined;
const username = searchParams.get("username") || undefined;
const event_type = searchParams.get("event_type") || undefined;

const { data, count, error } = await getActivityLogs({ limit, repo, username, event_type });
if (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
return NextResponse.json({ data, count });
}
48 changes: 48 additions & 0 deletions app/api/github/webhook/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NextRequest, NextResponse } from "next/server";
import { createClient } from '@supabase/supabase-js';

const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
);

export async function POST(req: NextRequest) {
try {
const payload = await req.json();
const eventType = req.headers.get("x-github-event");
console.log("Payload:", payload);

// Fire and forget (async side effect, don't await)
handleGitHubEvent(eventType, payload);

return NextResponse.json({ ok: true });

} catch (err) {
console.error("Error parsing webhook:", err);
return NextResponse.json({ error: "Invalid webhook" }, { status: 400 });
}
}

// Move logic to a background async handler (no await in main handler)
async function handleGitHubEvent(eventType: string | null, payload: any) {
console.log("Event received:", eventType);
console.log("Payload:", payload);

const repo = payload.repository?.full_name;
const user = payload.sender?.login || payload.pull_request?.user?.login;
const action = payload.action || 'unknown';

const { error } = await supabase.from('activities').insert({
event_type: eventType,
repo,
username: user,
action,
payload
});

if (error) {
console.error("Supabase insert error:", error);
} else {
console.log("Activity saved to Supabase ✅");
}
}
1 change: 1 addition & 0 deletions app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function Page() {
<div className="w-full">
<LoginForm />
</div>
<p className="text-sm text-gray-500 text-center mt-4">Sign in is only available via GitHub.</p>
</div>
</div>
);
Expand Down
10 changes: 5 additions & 5 deletions app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useParams } from "next/navigation";
import Image from "next/image";
import Link from "next/link";
import { useState, useEffect, useRef } from "react";
import { createClient } from "@/lib/supabase/client";
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
import { Button } from "@/components/ui/button";
import type { User } from "@supabase/supabase-js";
import type React from "react";
Expand Down Expand Up @@ -32,7 +32,7 @@ export default function BlogDetailPage() {
const [loading, setLoading] = useState(true);

useEffect(() => {
const supabase = createClient();
const supabase = createClientComponentClient();
const fetchData = async () => {
// Fetch blog
const { data: blogData } = await supabase.from("blogs").select("*").eq("slug", slug).single();
Expand Down Expand Up @@ -70,7 +70,7 @@ export default function BlogDetailPage() {

const handleLike = async () => {
if (!user || !blog) return;
const supabase = createClient();
const supabase = createClientComponentClient();
if (liked) {
await supabase.from("blog_likes").delete().eq("blog_id", blog.id).eq("user_id", user.id);
setLikes((l) => l - 1);
Expand All @@ -85,7 +85,7 @@ export default function BlogDetailPage() {
const handleComment = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!user || !blog || !comment.trim()) return;
const supabase = createClient();
const supabase = createClientComponentClient();
await supabase.from("blog_comments").insert({
blog_id: blog.id,
user_id: user.id,
Expand Down Expand Up @@ -157,4 +157,4 @@ export default function BlogDetailPage() {
</div>
</main>
);
}
}
6 changes: 3 additions & 3 deletions app/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Link from "next/link";
import Image from "next/image";
import { useEffect, useState } from "react";
import { createClient } from "@/lib/supabase/client";
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";

type Blog = {
id: string;
Expand All @@ -20,7 +20,7 @@ export default function BlogListPage() {

useEffect(() => {
const fetchBlogs = async () => {
const supabase = createClient();
const supabase = createClientComponentClient();
const { data } = await supabase
.from("blogs")
.select("id, slug, title, excerpt, image, author, date")
Expand Down Expand Up @@ -63,4 +63,4 @@ export default function BlogListPage() {
</div>
</main>
);
}
}
27 changes: 27 additions & 0 deletions app/dashboard/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export default async function DashboardLayout({ children }: { children: React.ReactNode }) {
const supabase = createServerComponentClient({ cookies });
const {
data: { session },
} = await supabase.auth.getSession();

if (!session) {
redirect("/auth/login");
}

// Fetch user profile
const { data: userProfile } = await supabase
.from("users")
.select("onboarding_complete")
.eq("id", session.user.id)
.single();

if (!userProfile?.onboarding_complete) {
redirect("/onboard");
}

return <>{children}</>;
}
Loading