Skip to content

Commit

Permalink
fix: safe parse smart sign text
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Dec 17, 2023
1 parent 0a951ee commit f34c1f8
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { generateMarkerTypes } from "../render-map-blips";
import { Button, Input } from "@snailycad/ui";
import { Permissions, usePermission } from "hooks/usePermission";
import { toastMessage } from "lib/toastMessage";
import { z } from "zod";

interface Props {
marker: SmartSignMarker;
Expand All @@ -19,7 +20,7 @@ export function SmartSignsMarker({ marker }: Props) {
const socket = useSocketStore((state) => state.socket);
const [markerText, setMarkerText] = React.useState<
SmartSignMarker["defaultText"] & { editing?: boolean }
>(marker.defaultText);
>(safeParseSmartSignText(marker.defaultText));

const t = useTranslations("Leo");
const hiddenItems = useDispatchMapState((state) => state.hiddenItems);
Expand All @@ -41,7 +42,7 @@ export function SmartSignsMarker({ marker }: Props) {
}, [markerTypes]);

React.useEffect(() => {
if (!markerText.editing) setMarkerText(marker.defaultText);
if (!markerText.editing) setMarkerText(safeParseSmartSignText(marker.defaultText));
}, [marker.defaultText, markerText.editing]);

function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
Expand Down Expand Up @@ -124,3 +125,20 @@ export function SmartSignsMarker({ marker }: Props) {
</Marker>
);
}

const smartSignTextSchema = z.object({
firstLine: z.string(),
secondLine: z.string(),
thirdLine: z.string(),
});

function safeParseSmartSignText(obj: unknown) {
const data = smartSignTextSchema.safeParse(obj);
return data.success
? data.data
: {
firstLine: "",
secondLine: "",
thirdLine: "",
};
}

0 comments on commit f34c1f8

Please sign in to comment.