From d0a0bb8c7e3387e48c379594f89c2f7fa834958c Mon Sep 17 00:00:00 2001 From: chakkun1121 <99847806+chakkun1121@users.noreply.github.com> Date: Tue, 30 Jan 2024 04:03:38 +0000 Subject: [PATCH] =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=83=9A=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E3=82=92=E6=BA=96=E5=82=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- @types/settings.ts | 7 +++- .../_library/settings/useServerSettings.tsx} | 37 ++++++------------- app/(app)/settings/main.tsx | 28 ++++++++++++++ app/(app)/settings/page.tsx | 11 ++++++ app/(home)/settings/page.tsx | 15 -------- 5 files changed, 57 insertions(+), 41 deletions(-) rename app/{(home)/settings/main.tsx => (app)/_library/settings/useServerSettings.tsx} (60%) create mode 100644 app/(app)/settings/main.tsx create mode 100644 app/(app)/settings/page.tsx delete mode 100644 app/(home)/settings/page.tsx diff --git a/@types/settings.ts b/@types/settings.ts index 26dbcbd..c93ce46 100644 --- a/@types/settings.ts +++ b/@types/settings.ts @@ -1,2 +1,7 @@ -export type serverSettingsType = {}; +export type serverSettingsType = { + customSearch: { + name: string; + url: string; //検索キーワードが%sに入る + }[]; +}; export type localSettings = {}; diff --git a/app/(home)/settings/main.tsx b/app/(app)/_library/settings/useServerSettings.tsx similarity index 60% rename from app/(home)/settings/main.tsx rename to app/(app)/_library/settings/useServerSettings.tsx index 5f1194c..36cfa52 100644 --- a/app/(home)/settings/main.tsx +++ b/app/(app)/_library/settings/useServerSettings.tsx @@ -1,24 +1,19 @@ "use client"; - -import { customSession } from "@/@types/customSession"; -import { localSettings, serverSettingsType } from "@/@types/settings"; +import { serverSettingsType } from "@/@types/settings"; import { listFiles, getFileContent, uploadFile } from "@/googledrive"; -import { useSession } from "next-auth/react"; import { useEffect, useState } from "react"; -import { atom, useRecoilState } from "recoil"; -import { recoilPersist } from "recoil-persist"; -export default function Settings() { - const [serverSettings, setServerSettings] = useState({}); - const [localSettings, setLocalSettings] = useRecoilState(localSettingsState); +export function useServerSettings(token: string) { const [settingsFileID, setSettingsFileID] = useState(); - const { data: session }: { data: customSession | null } = - useSession() as unknown as { data: customSession }; - const token = session?.accessToken; - + const [serverSettings, setServerSettings] = useState< + serverSettingsType | undefined + >(); + const [isLoading, setIsLoading] = useState(true); + const [isSaving, setIsSaving] = useState(false); useEffect(() => { (async () => { if (!token) return; + setIsLoading(true); const settingsFile = await listFiles( token, "name='settings.json'", @@ -32,11 +27,13 @@ export default function Settings() { JSON.parse(await getFileContent(token, settingsFile.id)) || {} ); } + setIsLoading(false); })(); }, [token]); useEffect(() => { (async () => { if (!token) return; + setIsSaving(true); if (settingsFileID) { uploadFile(token, settingsFileID, JSON.stringify(serverSettings)); } else { @@ -56,18 +53,8 @@ export default function Settings() { .then((r) => r.id) ); } + setIsSaving(false); })(); }, [serverSettings, settingsFileID, token]); - return <>; + return { serverSettings, setServerSettings, isLoading, isSaving }; } - -// recoil -const { persistAtom } = recoilPersist({ - key: "settings", - storage: typeof window === "undefined" ? undefined : localStorage, -}); -export const localSettingsState = atom({ - key: "settings", - default: {}, - effects_UNSTABLE: [persistAtom], -}); diff --git a/app/(app)/settings/main.tsx b/app/(app)/settings/main.tsx new file mode 100644 index 0000000..d5c9dbf --- /dev/null +++ b/app/(app)/settings/main.tsx @@ -0,0 +1,28 @@ +"use client"; + +import { customSession } from "@/@types/customSession"; +import { localSettings } from "@/@types/settings"; +import { useSession } from "next-auth/react"; +import { atom, useRecoilState } from "recoil"; +import { recoilPersist } from "recoil-persist"; +import { useServerSettings } from "../_library/settings/useServerSettings"; + +export default function Settings() { + const { data: session }: { data: customSession | null } = + useSession() as unknown as { data: customSession }; + const token = session?.accessToken; + const { serverSettings, setServerSettings, isLoading, isSaving } = + useServerSettings(token); + if (isLoading) return

loading...

; + return
; +} +// recoil +const { persistAtom } = recoilPersist({ + key: "settings", + storage: typeof window === "undefined" ? undefined : localStorage, +}); +export const localSettingsState = atom({ + key: "settings", + default: {}, + effects_UNSTABLE: [persistAtom], +}); diff --git a/app/(app)/settings/page.tsx b/app/(app)/settings/page.tsx new file mode 100644 index 0000000..43bf9f9 --- /dev/null +++ b/app/(app)/settings/page.tsx @@ -0,0 +1,11 @@ +import { Metadata } from "next"; +import Settings from "./main"; + +export default function Page() { + return ; +} +export const metadata: Metadata = { + title: "設定", + description: "VocabPhraseの設定ページです。", + robots: "noindex", +}; diff --git a/app/(home)/settings/page.tsx b/app/(home)/settings/page.tsx deleted file mode 100644 index f10253c..0000000 --- a/app/(home)/settings/page.tsx +++ /dev/null @@ -1,15 +0,0 @@ -import { Metadata } from "next"; -import { getServerSession } from "next-auth"; -import { redirect } from "next/navigation"; -import Settings from "./main"; - -export default async function Page() { - const session = await getServerSession(); - if (!session) redirect("/login?redirectTo=/settings"); - return ; -} -export const metadata: Metadata = { - title: "設定", - description: "VocabPhraseの設定ページです。", - robots: "noindex", -};