From 68f525d702de480421a3800747b3383a5ce1938b Mon Sep 17 00:00:00 2001 From: Misode Date: Sat, 19 Mar 2022 21:37:03 +0100 Subject: [PATCH] Share if preview is shown --- src/app/pages/Generator.tsx | 8 ++++++-- src/app/services/Sharing.ts | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/app/pages/Generator.tsx b/src/app/pages/Generator.tsx index 5fc7fbd6..9c947d49 100644 --- a/src/app/pages/Generator.tsx +++ b/src/app/pages/Generator.tsx @@ -67,6 +67,10 @@ export function Generator({}: Props) { route(`${cleanUrl(snippetGen.url)}?${SHARE_KEY}=${snippet.id}`) } } + if (snippet.show_preview && !previewShown) { + setPreviewShown(true) + setSourceShown(false) + } model.reset(DataModel.wrapLists(snippet.data), false) } @@ -231,10 +235,10 @@ export function Generator({}: Props) { } else if (model && blockStates) { const output = getOutput(model, blockStates) if (deepEqual(output, model.schema.default())) { - setShareUrl(`${location.protocol}//${location.host}/${gen.url}/`) + setShareUrl(`${location.protocol}//${location.host}/${gen.url}/?version=${version}`) setShareShown(true) } else { - shareSnippet(gen.id, version, output) + shareSnippet(gen.id, version, output, previewShown) .then(url => { setShareUrl(url) setShareShown(true) diff --git a/src/app/services/Sharing.ts b/src/app/services/Sharing.ts index 8286d3cf..3739bdb7 100644 --- a/src/app/services/Sharing.ts +++ b/src/app/services/Sharing.ts @@ -7,12 +7,12 @@ export const SHARE_KEY = 'share' const ShareCache = new Map() -export async function shareSnippet(type: string, version: VersionId, jsonData: any) { +export async function shareSnippet(type: string, version: VersionId, jsonData: any, show_preview: boolean) { try { const data = lz.compressToBase64(JSON.stringify(jsonData)) const raw = btoa(JSON.stringify(jsonData)) console.log('Compression rate', raw.length / data.length) - const body = JSON.stringify({ data, type, version }) + const body = JSON.stringify({ data, type, version, show_preview }) let id = ShareCache.get(body) if (!id) { const snippet = await fetchApi('/snippets', body)