mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-27 04:16:25 -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
156 lines
4.1 KiB
TypeScript
156 lines
4.1 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
import type { MatrixClient } from '$types/matrix-sdk';
|
|
import { fetch } from '$utils/fetch';
|
|
import { useMatrixClient } from './useMatrixClient';
|
|
|
|
export interface IntegrationManager {
|
|
apiUrl: string;
|
|
uiUrl: string;
|
|
}
|
|
|
|
const DEFAULT_MANAGERS: IntegrationManager[] = [
|
|
{
|
|
apiUrl: 'https://scalar.vector.im/api',
|
|
uiUrl: 'https://scalar.vector.im',
|
|
},
|
|
];
|
|
|
|
async function discoverManagers(mx: MatrixClient): Promise<IntegrationManager[]> {
|
|
const managers: IntegrationManager[] = [];
|
|
|
|
try {
|
|
const baseUrl = mx.getHomeserverUrl();
|
|
const url = new URL(baseUrl);
|
|
const wellKnownUrl = `https://${url.hostname}/.well-known/matrix/client`;
|
|
|
|
const resp = await fetch(wellKnownUrl, { method: 'GET' });
|
|
if (resp.ok) {
|
|
const data = await resp.json();
|
|
const integrations = data?.['m.integrations'];
|
|
if (integrations?.managers && Array.isArray(integrations.managers)) {
|
|
integrations.managers.forEach((mgr: { api_url?: string; ui_url?: string }) => {
|
|
if (mgr.api_url && mgr.ui_url) {
|
|
managers.push({ apiUrl: mgr.api_url, uiUrl: mgr.ui_url });
|
|
}
|
|
});
|
|
}
|
|
}
|
|
} catch {
|
|
// ignore malformed or unavailable .well-known data
|
|
}
|
|
|
|
try {
|
|
const widgetsEvent = mx.getAccountData('m.widgets' as never);
|
|
if (widgetsEvent) {
|
|
const content = widgetsEvent.getContent();
|
|
Object.values(content).forEach(
|
|
(widget: { type?: string; url?: string; data?: { api_url?: string } }) => {
|
|
if (widget?.type === 'm.integration_manager' && widget?.url) {
|
|
const existing = managers.some((m) => m.uiUrl === widget.url);
|
|
if (!existing) {
|
|
managers.push({
|
|
apiUrl: widget.data?.api_url || widget.url,
|
|
uiUrl: widget.url,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}
|
|
} catch {
|
|
// ignore malformed widget account data
|
|
}
|
|
|
|
if (managers.length === 0) {
|
|
return DEFAULT_MANAGERS;
|
|
}
|
|
|
|
return managers;
|
|
}
|
|
|
|
async function getScalarToken(mx: MatrixClient, apiUrl: string): Promise<string | null> {
|
|
try {
|
|
const openIdToken = await mx.getOpenIdToken();
|
|
|
|
const resp = await fetch(`${apiUrl}/register`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(openIdToken),
|
|
});
|
|
|
|
if (!resp.ok) return null;
|
|
|
|
const data = await resp.json();
|
|
return data?.scalar_token ?? null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function buildIntegrationManagerUrl(
|
|
uiUrl: string,
|
|
scalarToken: string | null,
|
|
roomId: string,
|
|
screen?: string,
|
|
integId?: string
|
|
): string {
|
|
const url = new URL(uiUrl);
|
|
url.pathname = '/index.html';
|
|
if (scalarToken) url.searchParams.set('scalar_token', scalarToken);
|
|
url.searchParams.set('room_id', roomId);
|
|
if (screen) url.searchParams.set('screen', screen);
|
|
if (integId) url.searchParams.set('integ_id', integId);
|
|
return url.toString();
|
|
}
|
|
|
|
export type IntegrationManagerState = {
|
|
managers: IntegrationManager[];
|
|
scalarToken: string | null;
|
|
loading: boolean;
|
|
error: string | null;
|
|
};
|
|
|
|
export function useIntegrationManager(): IntegrationManagerState & {
|
|
refresh: () => void;
|
|
} {
|
|
const mx = useMatrixClient();
|
|
const [state, setState] = useState<IntegrationManagerState>({
|
|
managers: [],
|
|
scalarToken: null,
|
|
loading: true,
|
|
error: null,
|
|
});
|
|
|
|
const loadManagers = useCallback(async () => {
|
|
setState((s) => ({ ...s, loading: true, error: null }));
|
|
try {
|
|
const managers = await discoverManagers(mx);
|
|
let scalarToken: string | null = null;
|
|
|
|
if (managers.length > 0 && managers[0]) {
|
|
scalarToken = await getScalarToken(mx, managers[0].apiUrl);
|
|
}
|
|
|
|
setState({
|
|
managers,
|
|
scalarToken,
|
|
loading: false,
|
|
error: null,
|
|
});
|
|
} catch (err) {
|
|
setState({
|
|
managers: [],
|
|
scalarToken: null,
|
|
loading: false,
|
|
error: err instanceof Error ? err.message : 'Failed to load integration managers',
|
|
});
|
|
}
|
|
}, [mx]);
|
|
|
|
useEffect(() => {
|
|
loadManagers();
|
|
}, [loadManagers]);
|
|
|
|
return { ...state, refresh: loadManagers };
|
|
}
|