mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-04 07:42:24 -08:00
# Conflicts: # .gitignore # .prettierignore # .vscode/settings.json # eslint.config.js # flake.lock # flake.nix # knip.json # package.json # pnpm-lock.yaml # src/app/components/AuthFlowsLoader.tsx # src/app/components/ClientConfigLoader.tsx # src/app/components/SpecVersionsLoader.tsx # src/app/components/SwipeableChatWrapper.tsx # src/app/components/SwipeableOverlayWrapper.tsx # src/app/components/emoji-board/components/Item.tsx # src/app/components/message/content/ImageContent.tsx # src/app/components/message/content/ThumbnailContent.tsx # src/app/components/power/PowerIcon.tsx # src/app/components/url-preview/ClientPreview.tsx # src/app/features/room/RoomInput.tsx # src/app/features/room/message/Message.tsx # src/app/features/settings/Settings.tsx # src/app/features/settings/emojis-stickers/GlobalPacks.tsx # src/app/features/settings/notifications/PushNotifications.tsx # src/app/features/settings/notifications/SystemNotification.tsx # src/app/hooks/useClientConfig.ts # src/app/hooks/useIntegrationManager.ts # src/app/hooks/useSessionProfiles.ts # src/app/pages/App.tsx # src/app/pages/Router.tsx # src/app/pages/auth/SSOLogin.tsx # src/app/pages/auth/login/Login.tsx # src/app/pages/auth/register/PasswordRegisterForm.tsx # src/app/pages/auth/register/Register.tsx # src/app/pages/auth/reset-password/PasswordResetForm.tsx # src/app/pages/client/AutoDiscovery.tsx # src/app/pages/client/BackgroundNotifications.tsx # src/app/pages/client/ClientNonUIFeatures.tsx # src/app/pages/client/ClientRoot.tsx # src/app/pages/client/SyncStatus.tsx # src/app/plugins/react-custom-html-parser.tsx # src/app/state/serviceWorkerRegistration.ts # src/app/state/settings.ts # src/app/utils/matrix.ts # src/app/utils/notifications.ts # src/app/utils/settingsSync.test.ts # src/client/initMatrix.ts # src/index.tsx # src/sw.ts # src/sw/pushNotification.ts # tsconfig.json # vite.config.ts
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import type { ReactNode } from 'react';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
|
|
import { fetch } from '$utils/fetch';
|
|
import type { SpecVersions } from '../cs-api';
|
|
import { specVersions } from '../cs-api';
|
|
|
|
type SpecVersionsLoaderProps = {
|
|
baseUrl: string;
|
|
loadVersions?: () => Promise<SpecVersions>;
|
|
fallback?: () => ReactNode;
|
|
error?: (err: unknown, retry: () => void, ignore: () => void) => ReactNode;
|
|
children: (versions: SpecVersions) => ReactNode;
|
|
};
|
|
export function SpecVersionsLoader({
|
|
baseUrl,
|
|
loadVersions,
|
|
fallback,
|
|
error,
|
|
children,
|
|
}: SpecVersionsLoaderProps) {
|
|
const [state, load] = useAsyncCallback(
|
|
useCallback(() => loadVersions?.() ?? specVersions(fetch, baseUrl), [baseUrl, loadVersions])
|
|
);
|
|
const [ignoreError, setIgnoreError] = useState(false);
|
|
|
|
const ignoreCallback = useCallback(() => setIgnoreError(true), []);
|
|
|
|
useEffect(() => {
|
|
load();
|
|
}, [load]);
|
|
|
|
if (state.status === AsyncStatus.Idle || state.status === AsyncStatus.Loading) {
|
|
return fallback?.();
|
|
}
|
|
|
|
if (!ignoreError && state.status === AsyncStatus.Error) {
|
|
if (error) return error(state.error, load, ignoreCallback);
|
|
}
|
|
|
|
return children(
|
|
state.status === AsyncStatus.Success
|
|
? state.data
|
|
: {
|
|
versions: [],
|
|
}
|
|
);
|
|
}
|