Merge pull request #43 from clean99/feat/use-persisted-setting

Feat: use persisted setting
This commit is contained in:
Abi Raja 2023-11-20 12:10:43 -05:00 committed by GitHub
commit 67d423930b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 11 deletions

View File

@ -21,6 +21,7 @@ import { Settings } from "./types";
import { IS_RUNNING_ON_CLOUD } from "./config"; import { IS_RUNNING_ON_CLOUD } from "./config";
import { PicoBadge } from "./components/PicoBadge"; import { PicoBadge } from "./components/PicoBadge";
import { OnboardingNote } from "./components/OnboardingNote"; import { OnboardingNote } from "./components/OnboardingNote";
import { usePersistedState } from "./hooks/usePersistedState";
import { UrlInputSection } from "./components/UrlInputSection"; import { UrlInputSection } from "./components/UrlInputSection";
function App() { function App() {
@ -32,12 +33,15 @@ function App() {
const [executionConsole, setExecutionConsole] = useState<string[]>([]); const [executionConsole, setExecutionConsole] = useState<string[]>([]);
const [updateInstruction, setUpdateInstruction] = useState(""); const [updateInstruction, setUpdateInstruction] = useState("");
const [history, setHistory] = useState<string[]>([]); const [history, setHistory] = useState<string[]>([]);
const [settings, setSettings] = useState<Settings>({ const [settings, setSettings] = usePersistedState<Settings>(
{
openAiApiKey: null, openAiApiKey: null,
screenshotOneApiKey: null, screenshotOneApiKey: null,
isImageGenerationEnabled: true, isImageGenerationEnabled: true,
editorTheme: "cobalt", editorTheme: "cobalt",
}); },
"setting"
);
const downloadCode = () => { const downloadCode = () => {
// Create a blob from the generated code // Create a blob from the generated code

View File

@ -10,7 +10,8 @@ export function OnboardingNote() {
credit on the Billing dashboard. credit on the Billing dashboard.
<br /> <br />
<span> <span>
This key is never stored. This app is open source. You can{" "} This key is only stored in your browser. Never stored on servers. This
app is open source. You can{" "}
<a href="https://github.com/abi/screenshot-to-code" className="inline"> <a href="https://github.com/abi/screenshot-to-code" className="inline">
check the code to confirm. check the code to confirm.
</a> </a>

View File

@ -58,8 +58,9 @@ function SettingsDialog({ settings, setSettings }: Props) {
<div className="flex flex-col space-y-4"> <div className="flex flex-col space-y-4">
<Label htmlFor="openai-api-key"> <Label htmlFor="openai-api-key">
<div>OpenAI API key</div> <div>OpenAI API key</div>
<div className="font-light mt-2"> <div className="font-light mt-2 leading-relaxed">
Never stored. Overrides your .env config. Only stored in your browser. Never stored on servers. Overrides
your .env config.
</div> </div>
</Label> </Label>
@ -77,8 +78,8 @@ function SettingsDialog({ settings, setSettings }: Props) {
<Label htmlFor="screenshot-one-api-key"> <Label htmlFor="screenshot-one-api-key">
<div>ScreenshotOne API key</div> <div>ScreenshotOne API key</div>
<div className="font-light mt-2"> <div className="font-light mt-2 leading-relaxed">
Never stored.{" "} Only stored in your browser. Never stored on servers.{" "}
<a <a
href="https://screenshotone.com?via=screenshot-to-code" href="https://screenshotone.com?via=screenshot-to-code"
className="underline" className="underline"

View File

@ -0,0 +1,19 @@
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
type PersistedState<T> = [T, Dispatch<SetStateAction<T>>];
function usePersistedState<T>(defaultValue: T, key: string): PersistedState<T> {
const [value, setValue] = useState<T>(() => {
const value = window.localStorage.getItem(key);
return value ? (JSON.parse(value) as T) : defaultValue;
});
useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue];
}
export { usePersistedState };