Skip to content

Instantly share code, notes, and snippets.

@arthyn
Created April 28, 2022 18:13
Show Gist options
  • Save arthyn/f4c68e2881dc7fe147000d65085b92b8 to your computer and use it in GitHub Desktop.
Save arthyn/f4c68e2881dc7fe147000d65085b92b8 to your computer and use it in GitHub Desktop.
embed updates
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<T> {
read: () => T;
}
-export function suspend<T>(awaiting: Promise<T>): Suspender<T> {
+export function suspend<T>(awaiting: Promise<T>, defaultValue?: any): Suspender<T> {
let state: SuspendState = 'pending';
let result: T | null = null;
@@ -22,8 +22,10 @@ export function suspend<T>(awaiting: Promise<T>): Suspender<T> {
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<EmbedState>((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<any> => {
@@ -32,7 +33,7 @@ const useEmbedState = create<EmbedState>((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 }}
></EmbedBox>
</EmbedContainer>
- ) : renderUrl ? (
+ ) : (
<RemoteContentEmbedFallback url={url} />
- ) : (() => {
- throw fallbackError;
-})()
+ )
}
</Col>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment