mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
Change settings links to use explicit markers
This commit is contained in:
16 files changed
+171
-301
No files matched your search
@@ -0,0 +1,9 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
# Change how settings links are shared
|
||||
|
||||
Settings links copied from Sable now stay on the current client URL and include a small Sable marker in the link. That lets Sable recognize settings links copied from other Sable instances without treating unrelated third-party `/settings/...` links as Sable settings links.
|
||||
|
||||
If you previously set `settingsLinkBaseUrl` in `config.json`, remove it. Sable now derives settings links from the runtime app URL, and the old config key is no longer used.
|
||||
@@ -13,8 +13,6 @@
|
||||
"webPushAppID": "moe.sable.app.sygnal"
|
||||
},
|
||||
|
||||
"settingsLinkBaseUrl": "https://app.sable.moe",
|
||||
|
||||
"slidingSync": {
|
||||
"enabled": true
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { MsgType } from '$types/matrix-sdk';
|
||||
import { ClientConfigProvider } from '$hooks/useClientConfig';
|
||||
import { RenderMessageContent } from './RenderMessageContent';
|
||||
@@ -13,9 +13,9 @@ vi.mock('./url-preview', () => ({
|
||||
youtubeUrl: () => false,
|
||||
}));
|
||||
|
||||
function renderMessage(body: string, settingsLinkBaseUrl = 'https://app.sable.moe') {
|
||||
function renderMessage(body: string) {
|
||||
return render(
|
||||
<ClientConfigProvider value={{ settingsLinkBaseUrl }}>
|
||||
<ClientConfigProvider value={{}}>
|
||||
<RenderMessageContent
|
||||
displayName="Alice"
|
||||
msgType={MsgType.Text}
|
||||
@@ -30,9 +30,19 @@ function renderMessage(body: string, settingsLinkBaseUrl = 'https://app.sable.mo
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('location', { origin: 'https://app.example' } as Location);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('RenderMessageContent', () => {
|
||||
it('does not render url previews for settings links', () => {
|
||||
renderMessage('https://app.sable.moe/settings/account?focus=status');
|
||||
renderMessage(
|
||||
'https://app.example/settings/account?focus=status&moe.sable.client.action=settings'
|
||||
);
|
||||
|
||||
expect(screen.queryByTestId('url-preview-holder')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('url-preview-card')).not.toBeInTheDocument();
|
||||
|
||||
@@ -49,7 +49,7 @@ describe('SettingTile', () => {
|
||||
|
||||
await waitFor(() => {
|
||||
expect(writeText).toHaveBeenCalledWith(
|
||||
'https://settings.example/settings/appearance?focus=message-link-preview'
|
||||
'https://settings.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings'
|
||||
);
|
||||
});
|
||||
expect(screen.getByRole('button', { name: /copied settings link/i })).toBeInTheDocument();
|
||||
@@ -64,7 +64,7 @@ describe('SettingTile', () => {
|
||||
|
||||
await waitFor(() => {
|
||||
expect(writeText).toHaveBeenCalledWith(
|
||||
'https://settings.example/settings/appearance?focus=message-link-preview'
|
||||
'https://settings.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings'
|
||||
);
|
||||
});
|
||||
expect(screen.getByRole('button', { name: /copy settings link/i })).toBeInTheDocument();
|
||||
|
||||
@@ -12,8 +12,6 @@ const { mockMatrixClient, mockProfile } = vi.hoisted(() => ({
|
||||
mockProfile: { displayName: 'Alice', avatarUrl: undefined },
|
||||
}));
|
||||
|
||||
let settingsLinkBaseUrlOverride: string | undefined;
|
||||
|
||||
vi.mock('$hooks/useMatrixClient', () => ({
|
||||
useMatrixClient: () => mockMatrixClient,
|
||||
}));
|
||||
@@ -27,13 +25,7 @@ vi.mock('$hooks/useMediaAuthentication', () => ({
|
||||
}));
|
||||
|
||||
vi.mock('$state/hooks/settings', () => ({
|
||||
useSetting: (_atom: unknown, key: string) => {
|
||||
if (key === 'settingsLinkBaseUrlOverride') {
|
||||
return [settingsLinkBaseUrlOverride, vi.fn()] as const;
|
||||
}
|
||||
|
||||
return [true, vi.fn()] as const;
|
||||
},
|
||||
useSetting: () => [true, vi.fn()] as const,
|
||||
}));
|
||||
|
||||
vi.mock('$state/settings', () => ({
|
||||
@@ -137,7 +129,6 @@ vi.mock('./keyboard-shortcuts', () => ({
|
||||
|
||||
beforeEach(() => {
|
||||
writeText.mockReset();
|
||||
settingsLinkBaseUrlOverride = undefined;
|
||||
vi.stubGlobal('location', { origin: 'https://app.example' } as Location);
|
||||
vi.stubGlobal('navigator', { clipboard: { writeText } } as unknown as Navigator);
|
||||
});
|
||||
@@ -147,11 +138,11 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe('Settings', () => {
|
||||
it('uses the configured settings link base URL for copied settings links', async () => {
|
||||
it('uses the current app origin for copied settings links', async () => {
|
||||
writeText.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(
|
||||
<ClientConfigProvider value={{ settingsLinkBaseUrl: 'https://config.example' }}>
|
||||
<ClientConfigProvider value={{}}>
|
||||
<ScreenSizeProvider value={ScreenSize.Desktop}>
|
||||
<Settings activeSection="appearance" requestClose={vi.fn()} />
|
||||
</ScreenSizeProvider>
|
||||
@@ -162,17 +153,16 @@ describe('Settings', () => {
|
||||
|
||||
await waitFor(() => {
|
||||
expect(writeText).toHaveBeenCalledWith(
|
||||
'https://config.example/settings/appearance?focus=message-link-preview'
|
||||
'https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('prefers the client-side override over the configured settings link base URL', async () => {
|
||||
it('preserves the configured hash-router basename in copied settings links', async () => {
|
||||
writeText.mockResolvedValueOnce(undefined);
|
||||
settingsLinkBaseUrlOverride = 'https://override.example/';
|
||||
|
||||
render(
|
||||
<ClientConfigProvider value={{ settingsLinkBaseUrl: 'https://config.example' }}>
|
||||
<ClientConfigProvider value={{ hashRouter: { enabled: true, basename: '/app' } }}>
|
||||
<ScreenSizeProvider value={ScreenSize.Desktop}>
|
||||
<Settings activeSection="appearance" requestClose={vi.fn()} />
|
||||
</ScreenSizeProvider>
|
||||
@@ -183,7 +173,7 @@ describe('Settings', () => {
|
||||
|
||||
await waitFor(() => {
|
||||
expect(writeText).toHaveBeenCalledWith(
|
||||
'https://override.example/settings/appearance?focus=message-link-preview'
|
||||
'https://app.example/#/app/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,7 +54,6 @@ import { isKeyHotkey } from 'is-hotkey';
|
||||
import { settingsSyncLastSyncedAtom, settingsSyncStatusAtom } from '$hooks/useSettingsSync';
|
||||
import { exportSettingsAsJson, importSettingsFromJson } from '$utils/settingsSync';
|
||||
import { SettingsSectionPage } from '../SettingsSectionPage';
|
||||
import { SettingsLinkBaseUrlSetting } from './SettingsLinkBaseUrlSetting';
|
||||
|
||||
type DateHintProps = {
|
||||
hasChanges: boolean;
|
||||
@@ -1072,9 +1071,6 @@ function Messages() {
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingsLinkBaseUrlSetting />
|
||||
</SequenceCard>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Hide Member Events in Read-Only Rooms"
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { ClientConfigProvider } from '$hooks/useClientConfig';
|
||||
import { SettingsLinkBaseUrlSetting } from './SettingsLinkBaseUrlSetting';
|
||||
|
||||
let settingsLinkBaseUrlOverride: string | undefined;
|
||||
const setSettingsLinkBaseUrlOverride = vi.fn();
|
||||
|
||||
vi.mock('$state/hooks/settings', () => ({
|
||||
useSetting: () => [settingsLinkBaseUrlOverride, setSettingsLinkBaseUrlOverride] as const,
|
||||
}));
|
||||
|
||||
vi.mock('$state/settings', () => ({
|
||||
settingsAtom: {},
|
||||
}));
|
||||
|
||||
function renderSetting(settingsLinkBaseUrl = 'https://app.sable.moe') {
|
||||
return render(
|
||||
<ClientConfigProvider value={{ settingsLinkBaseUrl }}>
|
||||
<SettingsLinkBaseUrlSetting />
|
||||
</ClientConfigProvider>
|
||||
);
|
||||
}
|
||||
|
||||
describe('SettingsLinkBaseUrlSetting', () => {
|
||||
beforeEach(() => {
|
||||
settingsLinkBaseUrlOverride = undefined;
|
||||
setSettingsLinkBaseUrlOverride.mockReset();
|
||||
});
|
||||
|
||||
it('uses Url casing in the visible setting title', () => {
|
||||
renderSetting('https://config.example');
|
||||
|
||||
expect(screen.getByText('Settings Link Base Url')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the configured default in the input and no separate reset button', () => {
|
||||
renderSetting('https://config.example');
|
||||
|
||||
expect(screen.getByRole('textbox', { name: 'Settings link base URL' })).toHaveValue(
|
||||
'https://config.example'
|
||||
);
|
||||
expect(screen.getByRole('button', { name: 'Save' })).toBeDisabled();
|
||||
expect(screen.queryByRole('button', { name: 'Reset' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('uses an inline reset control to restore the configured default URL', () => {
|
||||
renderSetting('https://config.example');
|
||||
|
||||
fireEvent.change(screen.getByRole('textbox', { name: 'Settings link base URL' }), {
|
||||
target: { value: 'https://override.example' },
|
||||
});
|
||||
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Reset settings link base URL' })
|
||||
).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Reset settings link base URL' }));
|
||||
|
||||
expect(screen.getByRole('textbox', { name: 'Settings link base URL' })).toHaveValue(
|
||||
'https://config.example'
|
||||
);
|
||||
expect(screen.getByRole('button', { name: 'Save' })).toBeDisabled();
|
||||
});
|
||||
|
||||
it('clears the override when saving the configured default URL', () => {
|
||||
settingsLinkBaseUrlOverride = 'https://override.example';
|
||||
renderSetting('https://config.example');
|
||||
|
||||
fireEvent.change(screen.getByRole('textbox', { name: 'Settings link base URL' }), {
|
||||
target: { value: 'https://config.example' },
|
||||
});
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
expect(setSettingsLinkBaseUrlOverride).toHaveBeenCalledWith(undefined);
|
||||
});
|
||||
});
|
||||
@@ -1,108 +0,0 @@
|
||||
import { ChangeEventHandler, FormEventHandler, useEffect, useMemo, useState } from 'react';
|
||||
import { Box, Button, config, Icon, IconButton, Icons, Input, Text } from 'folds';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import { SettingTile } from '$components/setting-tile';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { getConfiguredSettingsLinkBaseUrl, normalizeSettingsLinkBaseUrl } from '../settingsLink';
|
||||
|
||||
export function SettingsLinkBaseUrlSetting() {
|
||||
const clientConfig = useClientConfig();
|
||||
const [settingsLinkBaseUrlOverride, setSettingsLinkBaseUrlOverride] = useSetting(
|
||||
settingsAtom,
|
||||
'settingsLinkBaseUrlOverride'
|
||||
);
|
||||
const configuredBaseUrl = useMemo(
|
||||
() => getConfiguredSettingsLinkBaseUrl(clientConfig),
|
||||
[clientConfig]
|
||||
);
|
||||
const currentValue =
|
||||
normalizeSettingsLinkBaseUrl(settingsLinkBaseUrlOverride) ?? configuredBaseUrl;
|
||||
const [inputValue, setInputValue] = useState(currentValue);
|
||||
|
||||
useEffect(() => {
|
||||
setInputValue(currentValue);
|
||||
}, [currentValue]);
|
||||
|
||||
const trimmedValue = inputValue.trim();
|
||||
const normalizedInputValue = normalizeSettingsLinkBaseUrl(trimmedValue);
|
||||
const nextOverrideValue =
|
||||
normalizedInputValue && normalizedInputValue !== configuredBaseUrl
|
||||
? normalizedInputValue
|
||||
: undefined;
|
||||
const hasChanges = normalizedInputValue !== currentValue;
|
||||
const isValid = Boolean(normalizedInputValue);
|
||||
|
||||
const handleChange: ChangeEventHandler<HTMLInputElement> = (evt) => {
|
||||
setInputValue(evt.currentTarget.value);
|
||||
};
|
||||
|
||||
const handleSubmit: FormEventHandler<HTMLFormElement> = (evt) => {
|
||||
evt.preventDefault();
|
||||
if (!isValid) return;
|
||||
|
||||
setSettingsLinkBaseUrlOverride(nextOverrideValue);
|
||||
setInputValue(normalizedInputValue ?? configuredBaseUrl);
|
||||
};
|
||||
|
||||
const handleReset = () => {
|
||||
setInputValue(configuredBaseUrl);
|
||||
};
|
||||
|
||||
return (
|
||||
<SettingTile
|
||||
title="Settings Link Base Url"
|
||||
focusId="settings-link-base-url"
|
||||
description={`Copied settings links use this URL. Default: ${configuredBaseUrl}`}
|
||||
>
|
||||
<Box direction="Column" grow="Yes" gap="100">
|
||||
<Box as="form" onSubmit={handleSubmit} gap="200">
|
||||
<Box grow="Yes" direction="Column">
|
||||
<Input
|
||||
aria-label="Settings link base URL"
|
||||
autoComplete="url"
|
||||
name="settingsLinkBaseUrlInput"
|
||||
radii="300"
|
||||
type="url"
|
||||
value={inputValue}
|
||||
variant="Secondary"
|
||||
style={{ paddingRight: config.space.S200 }}
|
||||
onChange={handleChange}
|
||||
after={
|
||||
hasChanges && (
|
||||
<IconButton
|
||||
type="reset"
|
||||
onClick={handleReset}
|
||||
size="300"
|
||||
radii="300"
|
||||
variant="Secondary"
|
||||
aria-label="Reset settings link base URL"
|
||||
title="Reset settings link base URL"
|
||||
>
|
||||
<Icon src={Icons.Cross} size="100" />
|
||||
</IconButton>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</Box>
|
||||
<Button
|
||||
disabled={!hasChanges || !isValid}
|
||||
fill={hasChanges && isValid ? 'Solid' : 'Soft'}
|
||||
outlined
|
||||
radii="300"
|
||||
size="400"
|
||||
type="submit"
|
||||
variant={hasChanges && isValid ? 'Success' : 'Secondary'}
|
||||
>
|
||||
<Text size="B400">Save</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
</Box>
|
||||
{!isValid && (
|
||||
<Text size="T200" priority="400">
|
||||
Enter a full `http://` or `https://` URL.
|
||||
</Text>
|
||||
)}
|
||||
</SettingTile>
|
||||
);
|
||||
}
|
||||
@@ -1,37 +1,25 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
DEFAULT_SETTINGS_LINK_BASE_URL,
|
||||
buildSettingsLink,
|
||||
getEffectiveSettingsLinkBaseUrl,
|
||||
parseSettingsLink,
|
||||
SETTINGS_LINK_ACTION_PARAM,
|
||||
SETTINGS_LINK_ACTION_SETTINGS,
|
||||
toSettingsFocusIdPart,
|
||||
} from './settingsLink';
|
||||
|
||||
describe('settingsLink', () => {
|
||||
it('builds settings links for plain and hash-router base urls', () => {
|
||||
it('builds settings links with the explicit action marker for plain and hash-router base urls', () => {
|
||||
expect(buildSettingsLink('https://app.example', 'appearance', 'message-link-preview')).toBe(
|
||||
'https://app.example/settings/appearance?focus=message-link-preview'
|
||||
`https://app.example/settings/appearance?focus=message-link-preview&${SETTINGS_LINK_ACTION_PARAM}=${SETTINGS_LINK_ACTION_SETTINGS}`
|
||||
);
|
||||
expect(
|
||||
buildSettingsLink('https://app.example/#/app', 'appearance', 'message-link-preview')
|
||||
).toBe('https://app.example/#/app/settings/appearance?focus=message-link-preview');
|
||||
).toBe(
|
||||
`https://app.example/#/app/settings/appearance?focus=message-link-preview&${SETTINGS_LINK_ACTION_PARAM}=${SETTINGS_LINK_ACTION_SETTINGS}`
|
||||
);
|
||||
});
|
||||
|
||||
it('resolves the settings link base URL from built-in default, config, and override', () => {
|
||||
expect(getEffectiveSettingsLinkBaseUrl({}, undefined)).toBe(DEFAULT_SETTINGS_LINK_BASE_URL);
|
||||
expect(getEffectiveSettingsLinkBaseUrl({}, true as never)).toBe(DEFAULT_SETTINGS_LINK_BASE_URL);
|
||||
expect(
|
||||
getEffectiveSettingsLinkBaseUrl({ settingsLinkBaseUrl: 'https://config.example/' })
|
||||
).toBe('https://config.example');
|
||||
expect(
|
||||
getEffectiveSettingsLinkBaseUrl(
|
||||
{ settingsLinkBaseUrl: 'https://config.example' },
|
||||
'https://override.example/'
|
||||
)
|
||||
).toBe('https://override.example');
|
||||
});
|
||||
|
||||
it('parses settings links from the same app origin only', () => {
|
||||
it('parses plain same-base settings links for compatibility', () => {
|
||||
expect(
|
||||
parseSettingsLink(
|
||||
'https://app.example',
|
||||
@@ -45,10 +33,39 @@ describe('settingsLink', () => {
|
||||
)
|
||||
).toEqual({ section: 'appearance', focus: 'message-link-preview' });
|
||||
|
||||
expect(parseSettingsLink('https://app.example', 'https://app.example/home/')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('parses cross-base settings links only when the explicit action marker is present', () => {
|
||||
expect(
|
||||
parseSettingsLink(
|
||||
'https://app.example',
|
||||
`https://other.example/settings/appearance?focus=message-link-preview&${SETTINGS_LINK_ACTION_PARAM}=${SETTINGS_LINK_ACTION_SETTINGS}`
|
||||
)
|
||||
).toEqual({ section: 'appearance', focus: 'message-link-preview' });
|
||||
|
||||
expect(
|
||||
parseSettingsLink(
|
||||
'https://app.example/#/app',
|
||||
`https://other.example/#/client/settings/account?focus=status&${SETTINGS_LINK_ACTION_PARAM}=${SETTINGS_LINK_ACTION_SETTINGS}`
|
||||
)
|
||||
).toEqual({ section: 'account', focus: 'status' });
|
||||
|
||||
expect(
|
||||
parseSettingsLink('https://app.example', 'https://other.example/settings/appearance')
|
||||
).toBeUndefined();
|
||||
expect(parseSettingsLink('https://app.example', 'https://app.example/home/')).toBeUndefined();
|
||||
expect(
|
||||
parseSettingsLink(
|
||||
'https://app.example',
|
||||
`https://other.example/settings/appearance?${SETTINGS_LINK_ACTION_PARAM}=not-settings`
|
||||
)
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
parseSettingsLink(
|
||||
'https://app.example',
|
||||
`https://other.example/redirect?next=/settings/appearance?focus=status&${SETTINGS_LINK_ACTION_PARAM}=${SETTINGS_LINK_ACTION_SETTINGS}`
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects a same-origin hash settings link that does not match the configured app base', () => {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { ClientConfig } from '$hooks/useClientConfig';
|
||||
import { getAppPathFromHref, getSettingsPath, withOriginBaseUrl } from '$pages/pathUtils';
|
||||
import { isSettingsSectionId, type SettingsSectionId } from './routes';
|
||||
|
||||
@@ -7,70 +6,101 @@ export type SettingsLink = {
|
||||
focus?: string;
|
||||
};
|
||||
|
||||
export const DEFAULT_SETTINGS_LINK_BASE_URL = 'https://app.sable.moe';
|
||||
export const SETTINGS_LINK_ACTION_PARAM = 'moe.sable.client.action';
|
||||
export const SETTINGS_LINK_ACTION_SETTINGS = 'settings';
|
||||
|
||||
export const normalizeSettingsLinkBaseUrl = (value?: string | null): string | undefined => {
|
||||
if (typeof value !== 'string') return undefined;
|
||||
const withSettingsLinkAction = (path: string): string => {
|
||||
const [pathname, search = ''] = path.split('?');
|
||||
const params = new URLSearchParams(search);
|
||||
params.set(SETTINGS_LINK_ACTION_PARAM, SETTINGS_LINK_ACTION_SETTINGS);
|
||||
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return undefined;
|
||||
|
||||
try {
|
||||
const url = new URL(trimmed);
|
||||
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return url.toString().replace(/\/+$/, '');
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return `${pathname}?${params.toString()}`;
|
||||
};
|
||||
|
||||
export const getConfiguredSettingsLinkBaseUrl = (
|
||||
clientConfig: Pick<ClientConfig, 'settingsLinkBaseUrl'>
|
||||
): string =>
|
||||
normalizeSettingsLinkBaseUrl(clientConfig.settingsLinkBaseUrl) ?? DEFAULT_SETTINGS_LINK_BASE_URL;
|
||||
const parseSettingsAppPath = (appPath: string): SettingsLink | undefined => {
|
||||
if (!appPath.startsWith('/settings/')) return undefined;
|
||||
|
||||
export const getEffectiveSettingsLinkBaseUrl = (
|
||||
clientConfig: Pick<ClientConfig, 'settingsLinkBaseUrl'>,
|
||||
override?: string
|
||||
): string =>
|
||||
normalizeSettingsLinkBaseUrl(override) ?? getConfiguredSettingsLinkBaseUrl(clientConfig);
|
||||
const [pathname, search = ''] = appPath.split('?');
|
||||
const sectionMatch = pathname.match(/^\/settings\/([^/]+)\/?$/);
|
||||
if (!sectionMatch) return undefined;
|
||||
|
||||
const section = sectionMatch[1];
|
||||
if (!isSettingsSectionId(section)) return undefined;
|
||||
|
||||
const focus = new URLSearchParams(search).get('focus') ?? undefined;
|
||||
|
||||
return { section, focus };
|
||||
};
|
||||
|
||||
const hasSettingsLinkAction = (search: string): boolean =>
|
||||
new URLSearchParams(search).get(SETTINGS_LINK_ACTION_PARAM) === SETTINGS_LINK_ACTION_SETTINGS;
|
||||
|
||||
const getCrossBaseSettingsPathname = (pathname: string): string | undefined =>
|
||||
pathname.match(/(\/settings\/[^/]+\/?)$/)?.[1];
|
||||
|
||||
const getCrossBaseSettingsAppPath = (pathname: string, search: string): string | undefined => {
|
||||
if (!hasSettingsLinkAction(search)) return undefined;
|
||||
|
||||
const settingsPathname = getCrossBaseSettingsPathname(pathname);
|
||||
if (!settingsPathname) return undefined;
|
||||
|
||||
const appPath = search ? `${settingsPathname}?${search}` : settingsPathname;
|
||||
return parseSettingsAppPath(appPath) ? appPath : undefined;
|
||||
};
|
||||
|
||||
const getSameBaseSettingsAppPath = (baseUrl: string, href: string): string | undefined => {
|
||||
const base = new URL(baseUrl);
|
||||
const target = new URL(href);
|
||||
|
||||
if (base.origin !== target.origin) return undefined;
|
||||
|
||||
if (base.hash) {
|
||||
const baseHash = base.hash.replace(/\/+$/, '');
|
||||
if (!(target.hash === baseHash || target.hash.startsWith(`${baseHash}/`))) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return getAppPathFromHref(baseUrl, href);
|
||||
};
|
||||
|
||||
const getCrossBaseSettingsAppPathFromHref = (href: string): string | undefined => {
|
||||
const target = new URL(href);
|
||||
|
||||
const directAppPath = getCrossBaseSettingsAppPath(
|
||||
target.pathname,
|
||||
target.search.replace(/^\?/, '')
|
||||
);
|
||||
if (directAppPath) {
|
||||
return directAppPath;
|
||||
}
|
||||
|
||||
const hashPath = target.hash.startsWith('#') ? target.hash.slice(1) : target.hash;
|
||||
if (!hashPath) return undefined;
|
||||
|
||||
const [hashPathname, hashSearch = ''] = hashPath.split('?');
|
||||
return getCrossBaseSettingsAppPath(hashPathname, hashSearch);
|
||||
};
|
||||
|
||||
export const buildSettingsLink = (
|
||||
baseUrl: string,
|
||||
section: SettingsSectionId,
|
||||
focus?: string
|
||||
): string => withOriginBaseUrl(baseUrl, getSettingsPath(section, focus));
|
||||
): string => withOriginBaseUrl(baseUrl, withSettingsLinkAction(getSettingsPath(section, focus)));
|
||||
|
||||
export const parseSettingsLink = (baseUrl: string, href: string): SettingsLink | undefined => {
|
||||
try {
|
||||
const base = new URL(baseUrl);
|
||||
const target = new URL(href);
|
||||
|
||||
if (base.origin !== target.origin) return undefined;
|
||||
|
||||
if (base.hash) {
|
||||
const baseHash = base.hash.replace(/\/+$/, '');
|
||||
if (!(target.hash === baseHash || target.hash.startsWith(`${baseHash}/`))) {
|
||||
return undefined;
|
||||
}
|
||||
const sameBaseAppPath = getSameBaseSettingsAppPath(baseUrl, href);
|
||||
if (sameBaseAppPath) {
|
||||
return parseSettingsAppPath(sameBaseAppPath);
|
||||
}
|
||||
|
||||
const appPath = getAppPathFromHref(baseUrl, href);
|
||||
if (!appPath.startsWith('/settings/')) return undefined;
|
||||
const crossBaseAppPath = getCrossBaseSettingsAppPathFromHref(href);
|
||||
if (crossBaseAppPath) {
|
||||
return parseSettingsAppPath(crossBaseAppPath);
|
||||
}
|
||||
|
||||
const [pathname, search = ''] = appPath.split('?');
|
||||
const sectionMatch = pathname.match(/^\/settings\/([^/]+)\/?$/);
|
||||
if (!sectionMatch) return undefined;
|
||||
|
||||
const section = sectionMatch[1];
|
||||
if (!isSettingsSectionId(section)) return undefined;
|
||||
|
||||
const focus = new URLSearchParams(search).get('focus') ?? undefined;
|
||||
|
||||
return { section, focus };
|
||||
return undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import { useMemo } from 'react';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { getEffectiveSettingsLinkBaseUrl } from './settingsLink';
|
||||
import { getOriginBaseUrl } from '$pages/pathUtils';
|
||||
|
||||
export const useSettingsLinkBaseUrl = (): string => {
|
||||
const clientConfig = useClientConfig();
|
||||
const [settingsLinkBaseUrlOverride] = useSetting(settingsAtom, 'settingsLinkBaseUrlOverride');
|
||||
|
||||
return useMemo(
|
||||
() => getEffectiveSettingsLinkBaseUrl(clientConfig, settingsLinkBaseUrlOverride),
|
||||
[clientConfig, settingsLinkBaseUrlOverride]
|
||||
);
|
||||
return useMemo(() => getOriginBaseUrl(clientConfig.hashRouter), [clientConfig.hashRouter]);
|
||||
};
|
||||
@@ -42,7 +42,6 @@ export type ClientConfig = {
|
||||
hashRouter?: HashRouterConfig;
|
||||
|
||||
matrixToBaseUrl?: string;
|
||||
settingsLinkBaseUrl?: string;
|
||||
};
|
||||
|
||||
const ClientConfigContext = createContext<ClientConfig | null>(null);
|
||||
|
||||
@@ -148,9 +148,10 @@ describe('react custom html parser', () => {
|
||||
{renderLink({
|
||||
tagName: 'a',
|
||||
attributes: {
|
||||
href: 'https://app.example/settings/appearance?focus=message-link-preview',
|
||||
href: 'https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings',
|
||||
},
|
||||
content: 'https://app.example/settings/appearance?focus=message-link-preview',
|
||||
content:
|
||||
'https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings',
|
||||
} as never)}
|
||||
</div>
|
||||
);
|
||||
@@ -165,14 +166,14 @@ describe('react custom html parser', () => {
|
||||
|
||||
it('renders same-origin settings links as internal app links with settings metadata', () => {
|
||||
renderParsedHtml(
|
||||
'<a href="https://app.example/settings/appearance?focus=message-link-preview">Appearance</a>',
|
||||
'<a href="https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings">Appearance</a>',
|
||||
{ sanitize: false }
|
||||
);
|
||||
|
||||
const link = screen.getByRole('link', { name: 'Appearance' });
|
||||
expect(link).toHaveAttribute(
|
||||
'href',
|
||||
'https://app.example/settings/appearance?focus=message-link-preview'
|
||||
'https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings'
|
||||
);
|
||||
expect(link).toHaveAttribute('data-settings-link-section', 'appearance');
|
||||
expect(link).toHaveAttribute('data-settings-link-focus', 'message-link-preview');
|
||||
@@ -181,6 +182,21 @@ describe('react custom html parser', () => {
|
||||
expect(link.className).toContain(customHtmlCss.MentionWithIcon);
|
||||
});
|
||||
|
||||
it('renders marked cross-instance settings links as internal app links with settings metadata', () => {
|
||||
renderParsedHtml(
|
||||
'<a href="https://other.example/#/client/settings/account?focus=status&moe.sable.client.action=settings">Account</a>',
|
||||
{ sanitize: false }
|
||||
);
|
||||
|
||||
const link = screen.getByRole('link', { name: 'Account' });
|
||||
expect(link).toHaveAttribute(
|
||||
'href',
|
||||
'https://other.example/#/client/settings/account?focus=status&moe.sable.client.action=settings'
|
||||
);
|
||||
expect(link).toHaveAttribute('data-settings-link-section', 'account');
|
||||
expect(link).toHaveAttribute('data-settings-link-focus', 'status');
|
||||
});
|
||||
|
||||
it('renders matrix message permalinks with an icon instead of the Message prefix', () => {
|
||||
render(
|
||||
<div>
|
||||
|
||||
@@ -74,7 +74,6 @@ export interface Settings {
|
||||
developerTools: boolean;
|
||||
enableMSC4268CMD: boolean;
|
||||
settingsSyncEnabled: boolean;
|
||||
settingsLinkBaseUrlOverride?: string;
|
||||
|
||||
// Cosmetics!
|
||||
jumboEmojiSize: JumboEmojiSize;
|
||||
@@ -177,7 +176,6 @@ const defaultSettings: Settings = {
|
||||
|
||||
developerTools: false,
|
||||
settingsSyncEnabled: false,
|
||||
settingsLinkBaseUrlOverride: undefined,
|
||||
|
||||
// Cosmetics!
|
||||
jumboEmojiSize: 'normal',
|
||||
|
||||
@@ -30,7 +30,6 @@ describe('NON_SYNCABLE_KEYS', () => {
|
||||
'isPeopleDrawer',
|
||||
'isWidgetDrawer',
|
||||
'memberSortFilterIndex',
|
||||
'settingsLinkBaseUrlOverride',
|
||||
'developerTools',
|
||||
'settingsSyncEnabled',
|
||||
] as const;
|
||||
|
||||
@@ -14,7 +14,6 @@ export const NON_SYNCABLE_KEYS = new Set<keyof Settings>([
|
||||
'isPeopleDrawer',
|
||||
'isWidgetDrawer',
|
||||
'memberSortFilterIndex',
|
||||
'settingsLinkBaseUrlOverride',
|
||||
// Developer / diagnostic
|
||||
'developerTools',
|
||||
// Sync toggle itself must never be uploaded (it's device-local)
|
||||
|
||||
Reference in new issue
Block a user