Skip to content

Commit

Permalink
Show error toast if url.hash is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
sverhoeven committed Oct 11, 2024
1 parent 334cd98 commit 7395094
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
11 changes: 6 additions & 5 deletions apps/class-solid/src/lib/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ export function encodeExperiment(experiment: Experiment) {
*
*/
export function decodeExperiment(encoded: string): ExperimentConfigSchema {
const decoded = JSON.parse(decodeURIComponent(encoded));
const decoded = decodeURIComponent(encoded);
const parsed = JSON.parse(decoded);
const rawExperiment = {
name: decoded.n,
description: decoded.d,
reference: decoded.r,
permutations: decoded.p.map((perm: { n: string; c: PartialConfig }) => ({
name: parsed.n,
description: parsed.d,
reference: parsed.r,
permutations: parsed.p.map((perm: { n: string; c: PartialConfig }) => ({
name: perm.n,
config: perm.c,
})),
Expand Down
15 changes: 12 additions & 3 deletions apps/class-solid/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
DropdownMenuTrigger,
} from "~/components/ui/dropdown-menu";
import { Flex } from "~/components/ui/flex";
import { Toaster } from "~/components/ui/toast";
import { Toaster, showToast } from "~/components/ui/toast";
import { decodeExperiment } from "~/lib/encode";

import { experiments, uploadExperiment } from "~/lib/store";
Expand All @@ -28,8 +28,17 @@ export default function Home() {
const navigate = useNavigate();
const rawExperiment = location.hash.substring(1);
if (!rawExperiment) return;
const experimentConfig = decodeExperiment(rawExperiment);
uploadExperiment(experimentConfig);
try {
const experimentConfig = decodeExperiment(rawExperiment);
uploadExperiment(experimentConfig);
} catch (error) {
console.error(error);
showToast({
title: "Failed to load experiment from URL",
description: `${error}`,
variant: "error",
});
}
// Remove hash after loading experiment from URL,
// as after editing the experiment the hash out of sync
navigate("/");
Expand Down

0 comments on commit 7395094

Please sign in to comment.