Skip to content

Commit

Permalink
Persist playground model across page refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 23, 2024
1 parent 0b5b39d commit f64db7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/playground/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { errorMessage } from "@/lib/utils";
import { ZombieSurvival } from "@/simulators/zombie-survival";

const STORAGE_MAP_KEY = "playground-map";
const STORAGE_MODEL_KEY = "playground-model";

export default function PlaygroundPage() {
const isAdmin = useQuery(api.users.isAdmin);
Expand Down Expand Up @@ -114,6 +115,7 @@ export default function PlaygroundPage() {
function handleChangeModel(value: string) {
setModel(value);
setError(null);
window.localStorage.setItem(STORAGE_MODEL_KEY, value);
}

function handleEdit() {
Expand Down Expand Up @@ -172,10 +174,14 @@ export default function PlaygroundPage() {

React.useEffect(() => {
const maybeMap = window.localStorage.getItem(STORAGE_MAP_KEY);
const maybeModel = window.localStorage.getItem(STORAGE_MODEL_KEY);

if (maybeMap !== null) {
setMap(JSON.parse(maybeMap));
}
if (maybeModel !== null) {
setModel(maybeModel);
}
}, []);

React.useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions components/ModelSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export function ModelSelector({
const models = useQuery(api.models.getActiveModels);

React.useEffect(() => {
if (models !== undefined && models.length !== 0) {
if (models !== undefined && models.length !== 0 && value === "") {
onChange(models[0].slug);
}
}, [models]);
}, [models, value]);

if (models === undefined) {
return <p>Loading...</p>;
Expand Down

0 comments on commit f64db7c

Please sign in to comment.