Skip to content

Commit

Permalink
make temporary settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
chakkun1121 committed Feb 2, 2024
1 parent d3bff27 commit 6e3b032
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
14 changes: 14 additions & 0 deletions app/(app)/_library/settings/localSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { localSettings } from "@/@types/settings";
import { atom } from "recoil";
import { recoilPersist } from "recoil-persist";

// recoil
const { persistAtom } = recoilPersist({
key: "settings",
storage: typeof window === "undefined" ? undefined : localStorage,
});
export const localSettingsState = atom<localSettings>({
key: "settings",
default: {},
effects_UNSTABLE: [persistAtom],
});
2 changes: 1 addition & 1 deletion app/(app)/_library/settings/useServerSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useServerSettings(token: string) {
if (settingsFile) {
setSettingsFileID(settingsFile.id);
setServerSettings(
JSON.parse(await getFileContent(token, settingsFile.id)) || {}
JSON.parse((await getFileContent(token, settingsFile.id)) || "{}")
);
}
setIsLoading(false);
Expand Down
47 changes: 33 additions & 14 deletions app/(app)/settings/main.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
"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";
import { useRecoilState } from "recoil";
import { localSettingsState } from "../_library/settings/localSettings";

export default function Settings() {
const { data: session }: { data: customSession | null } =
useSession() as unknown as { data: customSession };
const token = session?.accessToken;
const [localSettings, setLocalSettings] = useRecoilState(localSettingsState);
const { serverSettings, setServerSettings, isLoading, isSaving } =
useServerSettings(token);
if (isLoading) return <p className="text-center">loading...</p>;
return <div></div>;
return (
<form
onSubmit={(e) => {
e.preventDefault();
const form = e.target as HTMLFormElement;
const serverSettings = form.serverSettings.value;
const localSettings = form.localSettings.value;
setServerSettings(serverSettings);
setLocalSettings(JSON.parse(localSettings));
}}
>
<div>
<h2>サーバー保存の設定</h2>
<textarea
name="serverSettings"
defaultValue={JSON.stringify(serverSettings, null, 2)}
/>
</div>
<div>
<h2>ローカル保存の設定</h2>
<textarea
name="localSettings"
defaultValue={JSON.stringify(localSettings || "{}", null, 2)}
/>
</div>
<button disabled={isSaving} type="submit">
保存
</button>
</form>
);
}
// recoil
const { persistAtom } = recoilPersist({
key: "settings",
storage: typeof window === "undefined" ? undefined : localStorage,
});
export const localSettingsState = atom<localSettings>({
key: "settings",
default: {},
effects_UNSTABLE: [persistAtom],
});

0 comments on commit 6e3b032

Please sign in to comment.