From fbf074ff4c4de591297bb44a8d2adf26c3df305b Mon Sep 17 00:00:00 2001 From: Stan Ke <156306548@qq.com> Date: Tue, 27 Aug 2024 19:54:34 +0800 Subject: [PATCH] darkmode --- app/api/chat/google/route.ts | 9 --------- app/components/DarkSwitch.tsx | 12 ++++++++++-- tailwind.config.js | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/api/chat/google/route.ts b/app/api/chat/google/route.ts index ac45be3..be68e48 100644 --- a/app/api/chat/google/route.ts +++ b/app/api/chat/google/route.ts @@ -1,15 +1,6 @@ import { getServerConfig } from "@/config/server"; import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai"; -// Ensure the environment variable is correctly set and decoded -const googleServiceKey = process.env.GOOGLE_SERVICE_KEY; -if (!googleServiceKey) { - throw new Error("Missing GOOGLE_SERVICE_KEY environment variable."); -} - -const credentials = JSON.parse( - Buffer.from(googleServiceKey, "base64").toString() -); export async function POST(req: Request) { try { diff --git a/app/components/DarkSwitch.tsx b/app/components/DarkSwitch.tsx index 3b154a4..db9ec0a 100644 --- a/app/components/DarkSwitch.tsx +++ b/app/components/DarkSwitch.tsx @@ -5,8 +5,13 @@ import React, { useState, useEffect } from "react"; // DarkSwitch component const DarkSwitch = () => { // State to track dark mode - const [darkMode, setDarkMode] = useState(false); - + const [darkMode, setDarkMode] = useState(() => { + if (typeof window !== "undefined" ) { + return localStorage.getItem("theme") === "dark"; + } + return false; + }); + // Function to toggle dark mode const toggleDarkMode = () => { setDarkMode(!darkMode); @@ -15,6 +20,9 @@ const DarkSwitch = () => { // Use effect to update the class on body based on dark mode state useEffect(() => { document.body.classList.toggle("dark", darkMode); + if (typeof window !== "undefined") { + localStorage.setItem("theme", darkMode ? "dark" : "light"); + } }, [darkMode]); return ( diff --git a/tailwind.config.js b/tailwind.config.js index a9ff995..b5d37c1 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -5,7 +5,7 @@ module.exports = { './components/**/*.{js,ts,jsx,tsx,mdx}', './app/**/*.{js,ts,jsx,tsx,mdx}', ], - darkMode: ["class"], + darkMode: ['selector'], theme: { extend: {}, },