mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-11-28 11:40:11 +03:00
applied patch from @arthyn
This commit is contained in:
parent
d7221f0c66
commit
d657dafbd9
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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>
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user