diff --git a/src/components/cstb/Notice.tsx b/src/components/cstb/Notice.tsx
index d1a8f82..2f19f78 100644
--- a/src/components/cstb/Notice.tsx
+++ b/src/components/cstb/Notice.tsx
@@ -1,3 +1,4 @@
+"use client"
import {
Card,
CardBody,
@@ -6,13 +7,15 @@ import {
CardTool,
} from "@/components/window/Card"
import { appStore } from "@/store/app"
+import { createClient } from "@/utils/supabase/client"
+import { Skeleton } from "@heroui/react"
import { Refresh, VolumeNotice } from "@icon-park/react"
+import useSWR, { useSWRConfig } from "swr"
import { useSnapshot } from "valtio"
import { ToolButton } from "../window/ToolButton"
const Notice = () => {
- void appStore.start()
- const app = useSnapshot(appStore.state)
+ const { mutate } = useSWRConfig()
return (
@@ -21,17 +24,54 @@ const Notice = () => {
公告
-
+ mutate("/api/notice")}>
刷新
- {app.notice || "不会真的有人要更新CSGO工具箱吧,不会吧不会吧 xswl"}
+
)
}
+const NoticeBody = () => {
+ void appStore.start()
+ const app = useSnapshot(appStore.state)
+
+ const noticeFetcher = async () => {
+ const supabase = createClient()
+ const { data /* , error */ } = await supabase
+ .from("Notice")
+ .select("created_at, content, url")
+ .order("created_at", { ascending: false })
+ .single()
+
+ return data
+ }
+
+ const { data: notice /* , error */, isLoading } = useSWR(
+ "/api/notice",
+ noticeFetcher,
+ )
+
+ // if (error) return <>错误:{error}>
+ if (isLoading)
+ return (
+
+
+
+ )
+
+ return (
+ <>
+ {notice?.content ||
+ app.notice ||
+ "不会真的有人要更新CSGO工具箱吧,不会吧不会吧 xswl"}
+ >
+ )
+}
+
export default Notice
diff --git a/src/components/window/Card.tsx b/src/components/window/Card.tsx
index bab340f..d18c3fd 100644
--- a/src/components/window/Card.tsx
+++ b/src/components/window/Card.tsx
@@ -11,7 +11,7 @@ interface CardProps {
const Card = ({ children }: CardProps) => {
return (
{children}
diff --git a/src/utils/supabase/client.ts b/src/utils/supabase/client.ts
new file mode 100644
index 0000000..f11c1af
--- /dev/null
+++ b/src/utils/supabase/client.ts
@@ -0,0 +1,7 @@
+import { createBrowserClient } from "@supabase/ssr"
+
+export const createClient = () =>
+ createBrowserClient(
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
+ )
diff --git a/src/utils/supabase/middleware.ts b/src/utils/supabase/middleware.ts
new file mode 100644
index 0000000..2a6fe05
--- /dev/null
+++ b/src/utils/supabase/middleware.ts
@@ -0,0 +1,36 @@
+import { createServerClient } from "@supabase/ssr"
+import { type NextRequest, NextResponse } from "next/server"
+
+export const createClient = (request: NextRequest) => {
+ // Create an unmodified response
+ let supabaseResponse = NextResponse.next({
+ request: {
+ headers: request.headers,
+ },
+ })
+
+ const supabase = createServerClient(
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
+ {
+ cookies: {
+ getAll() {
+ return request.cookies.getAll()
+ },
+ setAll(cookiesToSet) {
+ cookiesToSet.forEach(({ name, value /* , options */ }) =>
+ request.cookies.set(name, value),
+ )
+ supabaseResponse = NextResponse.next({
+ request,
+ })
+ cookiesToSet.forEach(({ name, value, options }) =>
+ supabaseResponse.cookies.set(name, value, options),
+ )
+ },
+ },
+ },
+ )
+
+ return supabaseResponse
+}
diff --git a/src/utils/supabase/server.ts b/src/utils/supabase/server.ts
new file mode 100644
index 0000000..dbe04e1
--- /dev/null
+++ b/src/utils/supabase/server.ts
@@ -0,0 +1,29 @@
+import { createServerClient } from "@supabase/ssr"
+import type { cookies } from "next/headers"
+
+export const createClient = (cookieStore: ReturnType) => {
+ return createServerClient(
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
+ {
+ cookies: {
+ getAll() {
+ // @ts-ignore
+ return cookieStore.getAll()
+ },
+ setAll(cookiesToSet) {
+ try {
+ cookiesToSet.forEach(({ name, value, options }) =>
+ // @ts-ignore
+ cookieStore.set(name, value, options),
+ )
+ } catch {
+ // The `setAll` method was called from a Server Component.
+ // This can be ignored if you have middleware refreshing
+ // user sessions.
+ }
+ },
+ },
+ },
+ )
+}