From d657dafbd95bc348ecf1f4fa5d4d075e97cf812b Mon Sep 17 00:00:00 2001 From: rcrdlbl Date: Thu, 28 Apr 2022 18:28:28 -0400 Subject: [PATCH] applied patch from @arthyn --- pkg/interface/src/logic/lib/suspend.ts | 6 ++++-- pkg/interface/src/logic/state/embed.tsx | 5 +++-- .../src/views/components/RemoteContent/embed.tsx | 9 +++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/interface/src/logic/lib/suspend.ts b/pkg/interface/src/logic/lib/suspend.ts index 1d6d5f4688..63e497afa9 100644 --- a/pkg/interface/src/logic/lib/suspend.ts +++ b/pkg/interface/src/logic/lib/suspend.ts @@ -4,7 +4,7 @@ export interface Suspender { read: () => T; } -export function suspend(awaiting: Promise): Suspender { +export function suspend(awaiting: Promise, defaultValue?: any): Suspender { let state: SuspendState = 'pending'; let result: T | null = null; @@ -22,8 +22,10 @@ export function suspend(awaiting: Promise): Suspender { read: () => { if (state === 'result') { return result!; - } else if (state === 'error') { + } else if (state === 'error' && typeof defaultValue === 'undefined') { throw result; + } else if (state === 'error' && typeof defaultValue !== 'undefined') { + return defaultValue; } else { throw promise; } diff --git a/pkg/interface/src/logic/state/embed.tsx b/pkg/interface/src/logic/state/embed.tsx index c43104d4cc..e472616339 100644 --- a/pkg/interface/src/logic/state/embed.tsx +++ b/pkg/interface/src/logic/state/embed.tsx @@ -23,7 +23,8 @@ const useEmbedState = create((set, get) => ({ const search = new URLSearchParams({ url }); - const embed = await jsonFetch(`${OEMBED_PROVIDER}?${search.toString()}`) + + const embed = await jsonFetch(`${OEMBED_PROVIDER}?${search.toString()}`); return embed; }, getEmbed: (url: string): Suspender => { @@ -32,7 +33,7 @@ const useEmbedState = create((set, get) => ({ return embeds[url]; } const { embeds: es } = get(); - const embed = suspend(fetch(url)) + const embed = suspend(fetch(url), {}); set({ embeds: { ...es, [url]: embed } }); return embed; } diff --git a/pkg/interface/src/views/components/RemoteContent/embed.tsx b/pkg/interface/src/views/components/RemoteContent/embed.tsx index 4f11c12334..8e0f061610 100644 --- a/pkg/interface/src/views/components/RemoteContent/embed.tsx +++ b/pkg/interface/src/views/components/RemoteContent/embed.tsx @@ -335,8 +335,7 @@ export const RemoteContentOembed = React.forwardRef< >((props, ref) => { const { url, oembed, renderUrl = false, thumbnail = false, ...rest } = props; - const embed = oembed.read() - const fallbackError = new Error('fallback'); + const embed = oembed.read(); const [aspect, width, height] = useMemo(() => { if(!('height' in embed && typeof embed.height === 'number' @@ -374,11 +373,9 @@ export const RemoteContentOembed = React.forwardRef< dangerouslySetInnerHTML={{ __html: embed.html }} > - ) : renderUrl ? ( + ) : ( - ) : (() => { - throw fallbackError; -})() + ) } );