From 6d4cbbf03b8ae31b8a8858e5ac48e394aecfc594 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Tue, 23 Dec 2025 15:31:44 +0100 Subject: [PATCH] Server supabase structure --- src/actions/get-server.ts | 19 +++++++++++++++++++ src/app/api/server/[id]/route.ts | 27 --------------------------- src/components/app/SidebarServers.tsx | 3 ++- src/hooks/use-app.ts | 7 +++---- src/lib/auth.ts | 5 ++--- 5 files changed, 26 insertions(+), 35 deletions(-) create mode 100644 src/actions/get-server.ts delete mode 100644 src/app/api/server/[id]/route.ts diff --git a/src/actions/get-server.ts b/src/actions/get-server.ts new file mode 100644 index 0000000..d9c32f3 --- /dev/null +++ b/src/actions/get-server.ts @@ -0,0 +1,19 @@ +"use server"; + +import createClient from "@/actions/supa"; + +export async function getServerById(id: string) { + const supabase = await createClient(); + + const { error, data: server } = await supabase + .from("servers") + .select("id, name, created_at, owner, address, icon_url") + .eq("id", id) + .maybeSingle(); + + if (error) throw error; + + if (!server) throw "Server is null"; + + return server; +} diff --git a/src/app/api/server/[id]/route.ts b/src/app/api/server/[id]/route.ts deleted file mode 100644 index edbe085..0000000 --- a/src/app/api/server/[id]/route.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { supabase } from "../../supa"; -import { StatusCodes } from "http-status-codes"; - -/** - * GET /api/server/ - */ -export async function GET( - req: NextRequest, - { params }: { params: Promise<{ id: string }> } -) { - const { id } = await params; - - const { data: profile } = await supabase - .from("servers") - .select("id, name, created_at, owner, address, icon_url") - .eq("id", id) - .maybeSingle(); - - if (!profile) - return NextResponse.json( - { message: "not found" }, - { status: StatusCodes.NOT_FOUND } - ); - - return NextResponse.json({ message: "ok", ...profile }); -} diff --git a/src/components/app/SidebarServers.tsx b/src/components/app/SidebarServers.tsx index fb69aa1..c6a4502 100644 --- a/src/components/app/SidebarServers.tsx +++ b/src/components/app/SidebarServers.tsx @@ -23,6 +23,7 @@ import App from "@/types/app"; import ProfilePicture from "../ProfilePicture"; import SettingsDialog from "../settings/SettingsDialog"; import { useRouter } from "next/navigation"; +import { getServerById } from "@/actions/get-server"; export default function SidebarServers({ app }: { app: App }) { const [newServer, setNewServer] = useState(""); @@ -31,7 +32,7 @@ export default function SidebarServers({ app }: { app: App }) { const handleAddServer = async () => { if (!newServer || Object.hasOwn(app.servers, newServer)) return; try { - const server = (await get(`/api/server/${newServer}`)).data as Server; + const server = await getServerById(newServer); app.setServers((prev) => ({ ...prev, [server.id]: server })); Cookies.set("servers", Object.keys(app.servers).join(",")); setNewServer(""); diff --git a/src/hooks/use-app.ts b/src/hooks/use-app.ts index 6ca9fe3..58a04d3 100644 --- a/src/hooks/use-app.ts +++ b/src/hooks/use-app.ts @@ -9,6 +9,7 @@ import { Channel, Server } from "@/types/types"; import Cookies from "js-cookie"; import { toast } from "sonner"; import { getProfileById } from "@/actions/get-profile"; +import { getServerById } from "@/actions/get-server"; export default function useApp(): App { const node = useRef(null); @@ -30,11 +31,9 @@ export default function useApp(): App { .split(",") .map((x) => x.trim()) .filter((id) => id) - .map((id) => get(`/api/server/${id}`)) + .map((id) => getServerById(id)) ) - .then((o) => - setServers(Object.fromEntries(o.map((i) => [i.data.id, i.data]))) - ) + .then((o) => setServers(Object.fromEntries(o.map((i) => [i.id, i])))) .catch(toast.error); }, []); diff --git a/src/lib/auth.ts b/src/lib/auth.ts index aa703b6..db007dd 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,11 +1,10 @@ import { Message, Server } from "@/types/types"; -import axios from "axios"; import { Dispatch, RefObject, SetStateAction } from "react"; -import { get } from "./request"; import { toast } from "sonner"; import { MessageStore } from "@/hooks/use-messages"; import { IndicatorContext, ServerMessage } from "@/types/protocol"; import { authRelay } from "@/actions/auth"; +import { getServerById } from "@/actions/get-server"; type AuthParams = { id: string; @@ -28,7 +27,7 @@ export default async function auth({ if (!id) return; - const ip = ((await get(`/api/server/${id}`)).data as any).address as string; + const ip = (await getServerById(id)).address; console.log("Authenticating with server at:", ip); const server_auth = await authRelay(id);