Skip to content

Commit dd343ee

Browse files
committed
extract decodeQueryValue helper, to make it less ugly
1 parent c5860eb commit dd343ee

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/main/hooks/useEditorUrlSync.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ const QUERY_KEY = "code";
99

1010
const encodeText = (text: string) => encodeURIComponent(text);
1111
const decodeText = (value: string) => decodeURIComponent(value);
12+
const decodeQueryValue = (raw: string | null) => {
13+
if (!raw) {
14+
return "";
15+
}
16+
17+
try {
18+
return decodeText(raw);
19+
} catch {
20+
return null;
21+
}
22+
};
1223

1324
export const useEditorUrlSync = (debounceMs = 1000) => {
1425
const dispatch = useDispatch();
@@ -58,15 +69,7 @@ export const useEditorUrlSync = (debounceMs = 1000) => {
5869
// If we're currently applying URL -> store, wait until debounced store matches it
5970
if (hydratingRef.current) {
6071
const raw = searchParams.get(QUERY_KEY);
61-
const decoded = raw
62-
? (() => {
63-
try {
64-
return decodeText(raw);
65-
} catch {
66-
return null;
67-
}
68-
})()
69-
: "";
72+
const decoded = decodeQueryValue(raw);
7073

7174
if (decoded !== debouncedText) {
7275
return;

0 commit comments

Comments
 (0)