Skip to content

Commit 5d399a9

Browse files
Fix loading of shared code in playground of website (#1575)
* Fix loading of shared code in playground of website * Fall back to the default example when the code param fails to decompress * Show an explicit error placeholder when shared code fails to decompress * Update editor model when value signal changes to avoid init race --------- Co-authored-by: Fabian Hiller <hillerfabian11@gmail.com>
1 parent e789528 commit 5d399a9

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

website/src/components/CodeEditor.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ export const CodeEditor = component$<CodeEditorProps>(
111111
});
112112
});
113113

114+
// Update value of model on change
115+
useTask$(({ track }) => {
116+
track(value);
117+
if (isBrowser && model.value && value.value !== model.value.getValue()) {
118+
model.value.setValue(value.value);
119+
}
120+
});
121+
114122
// Update theme on change
115123
useTask$(async ({ track }) => {
116124
track(editorTheme);

website/src/routes/playground/index.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
type QRL,
66
type Signal,
77
sync$,
8-
useComputed$,
98
useSignal,
109
useVisibleTask$,
1110
} from '@builder.io/qwik';
@@ -51,9 +50,8 @@ export const head: DocumentHead = {
5150
};
5251

5352
export default component$(() => {
54-
// Use navigate, location and side bar toggle
53+
// Use navigate and side bar toggle
5554
const navigate = useNavigate();
56-
const location = useLocation();
5755
const toggle = useSideBarToggle();
5856

5957
// Use editor and side bar elements signals
@@ -69,10 +67,18 @@ export default component$(() => {
6967
const logsElement = useSignal<HTMLOListElement>();
7068
const lastLogElement = useSignal<Element | null>();
7169

72-
// Computed initial code of editor
73-
const initialCode = useComputed$(() => {
74-
const code = location.url.searchParams.get('code');
75-
return code ? lz.decompressFromEncodedURIComponent(code) : editorCode;
70+
// Use initial code of editor signal
71+
const initialCode = useSignal(editorCode);
72+
73+
// Get initial code of editor from search params
74+
// eslint-disable-next-line qwik/no-use-visible-task
75+
useVisibleTask$(() => {
76+
const code = new URLSearchParams(window.location.search).get('code');
77+
if (code) {
78+
initialCode.value =
79+
lz.decompressFromEncodedURIComponent(code) ||
80+
'// Failed to load the shared code. The link may be corrupted or incomplete.';
81+
}
7682
});
7783

7884
/**

0 commit comments

Comments
 (0)