applied patch from @arthyn

This commit is contained in:
rcrdlbl 2022-04-28 18:28:28 -04:00
parent d7221f0c66
commit d657dafbd9
3 changed files with 10 additions and 10 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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>
);