🎨 refactor local storage

This commit is contained in:
Nate Sesti 2024-04-13 10:34:34 -07:00
parent 8630677ad8
commit 2bd370d23d
8 changed files with 29 additions and 72 deletions

View file

@ -222,41 +222,13 @@ const Layout = () => {
<DropdownPortalDiv id="model-select-top-div"></DropdownPortalDiv>
<Footer>
<div className="mr-auto flex gap-2 items-center">
{/* {localStorage.getItem("ide") === "jetbrains" ||
localStorage.getItem("hideFeature") === "true" || (
<SparklesIcon
className="cursor-pointer"
onClick={() => {
localStorage.setItem("hideFeature", "true");
}}
onMouseEnter={() => {
dispatch(
setBottomMessage(
`🎁 New Feature: Use ${getMetaKeyLabel()}⇧R automatically debug errors in the terminal (you can click the sparkle icon to make it go away)`
)
);
}}
onMouseLeave={() => {
dispatch(
setBottomMessageCloseTimeout(
setTimeout(() => {
dispatch(setBottomMessage(undefined));
}, 2000)
)
);
}}
width="1.3em"
height="1.3em"
color="yellow"
/>
)} */}
<ModelSelect />
{indexingProgress >= 1 && // Would take up too much space together with indexing progress
defaultModel?.provider === "free-trial" &&
(location.pathname === "/settings" ||
parseInt(localStorage.getItem("ftc") || "0") >= 125) && (
(getLocalStorage("ftc") ?? 0) >= 125) && (
<ProgressBar
completed={parseInt(localStorage.getItem("ftc") || "0")}
completed={getLocalStorage("ftc") ?? 0}
total={250}
/>
)}

View file

@ -7,6 +7,7 @@ import {
vscForeground,
} from "..";
import { getPlatform } from "../../util";
import { isJetBrains } from "../../util/ide";
const GridDiv = styled.div`
display: grid;
@ -209,18 +210,17 @@ function KeyboardShortcutsDialog() {
<div className="p-2">
<h3 className="my-3 mx-auto text-center">Keyboard Shortcuts</h3>
<GridDiv>
{(localStorage.getItem("ide") === "jetbrains"
? jetbrainsShortcuts
: vscodeShortcuts
).map((shortcut) => {
return (
<KeyboardShortcut
mac={shortcut.mac}
windows={shortcut.windows}
description={shortcut.description}
/>
);
})}
{(isJetBrains() ? jetbrainsShortcuts : vscodeShortcuts).map(
(shortcut) => {
return (
<KeyboardShortcut
mac={shortcut.mac}
windows={shortcut.windows}
description={shortcut.description}
/>
);
},
)}
</GridDiv>
</div>
);

View file

@ -1,5 +1,4 @@
import { PlayIcon, StopIcon } from "@heroicons/react/24/outline";
import { useEffect, useState } from "react";
import styled from "styled-components";
import { Button } from "..";
@ -43,28 +42,9 @@ function ContinueButton(props: {
disabled: boolean;
showStop: boolean;
}) {
const [buttonColor, setButtonColor] = useState<string | null>(
localStorage.getItem("continueButtonColor")
);
useEffect(() => {
const handleStorageChange = (e: any) => {
if (e.key === "continueButtonColor") {
// Update your state or do whatever you need to do here
setButtonColor(e.newValue);
}
};
window.addEventListener("storage", handleStorageChange);
// Don't forget to cleanup the event listener
return () => window.removeEventListener("storage", handleStorageChange);
}, []);
return (
<StyledButton
showStop={props.showStop}
color={buttonColor as any}
hidden={props.hidden}
style={{ fontSize: "10px" }}
className="m-auto"

View file

@ -230,19 +230,18 @@ function GUI(props: GUIProps) {
const sendInput = useCallback(
(editorState: JSONContent, modifiers: InputModifiers) => {
if (defaultModel?.provider === "free-trial") {
const ftc = localStorage.getItem("ftc");
const ftc = getLocalStorage("ftc");
if (ftc) {
const u = parseInt(ftc);
localStorage.setItem("ftc", (u + 1).toString());
setLocalStorage("ftc", ftc + 1);
if (u >= 250) {
if (ftc >= 250) {
dispatch(setShowDialog(true));
dispatch(setDialogMessage(<FTCDialog />));
posthog?.capture("ftc_reached");
return;
}
} else {
localStorage.setItem("ftc", "1");
setLocalStorage("ftc", 1);
}
}

View file

@ -20,6 +20,7 @@ import Loader from "../components/loaders/Loader";
import { RootState } from "../redux/store";
import { getFontSize, getPlatform } from "../util";
import { postToIde } from "../util/ide";
import { setLocalStorage } from "../util/localStorage";
const CancelButton = styled(Button)`
background-color: transparent;
@ -264,7 +265,7 @@ function Settings() {
step="1"
defaultValue={getFontSize()}
onChange={(e) => {
localStorage.setItem("fontSize", e.target.value);
setLocalStorage("fontSize", parseInt(e.target.value));
}}
/>
</div>

View file

@ -6,6 +6,7 @@ import {
} from "core/web/webviewProtocol";
import { v4 as uuidv4 } from "uuid";
import "vscode-webview";
import { getLocalStorage } from "./localStorage";
interface vscode {
postMessage(message: any): vscode;
}
@ -14,7 +15,7 @@ declare const vscode: any;
function _postToIde(messageType: string, data: any, messageId?: string) {
if (typeof vscode === "undefined") {
if (localStorage.getItem("ide") === "jetbrains") {
if (isJetBrains()) {
if (window.postIntellijMessage === undefined) {
console.log(
"Unable to send message: postIntellijMessage is undefined. ",
@ -183,5 +184,5 @@ export function appendText(text: string) {
}
export function isJetBrains() {
return localStorage.getItem("ide") === "jetbrains";
return getLocalStorage("ide") === "jetbrains";
}

View file

@ -1,3 +1,5 @@
import { getLocalStorage } from "./localStorage";
type Platform = "mac" | "linux" | "windows" | "unknown";
export function getPlatform(): Platform {
@ -43,6 +45,5 @@ export function getMetaKeyLabel(): string {
}
export function getFontSize(): number {
const fontSize = localStorage.getItem("fontSize");
return fontSize ? parseInt(fontSize) : 14;
return getLocalStorage("fontSize") ?? 14;
}

View file

@ -1,6 +1,9 @@
type LocalStorageTypes = {
onboardingComplete: boolean;
mainTextEntryCounter: number;
ide: "vscode" | "jetbrains";
ftc: number;
fontSize: number;
};
export function getLocalStorage<T extends keyof LocalStorageTypes>(