diff --git a/src/app/components/RenderMessageContent.tsx b/src/app/components/RenderMessageContent.tsx index 07d2476a7..533c1f82f 100644 --- a/src/app/components/RenderMessageContent.tsx +++ b/src/app/components/RenderMessageContent.tsx @@ -71,6 +71,10 @@ const getMediaType = (url: string) => { return null; }; +const isSableChatEmbedCandidate = (url: string): boolean => + /^https:\/\//i.test(url) && + (/\.preview\.sable\.css(\?|#|$)/i.test(url) || isHttpsFullSableCssUrl(url)); + const CAPTION_STYLE = { marginTop: config.space.S200 }; function RenderMessageContentInternal({ @@ -93,7 +97,7 @@ function RenderMessageContentInternal({ const [autoplayGifs] = useSetting(settingsAtom, 'autoplayGifs'); const [captionPosition] = useSetting(settingsAtom, 'captionPosition'); - const [themeChatAny] = useSetting(settingsAtom, 'themeChatPreviewAnyUrl'); + const [themeChatSableWidgets] = useSetting(settingsAtom, 'themeChatSableWidgetsEnabled'); const [multiplePreviews] = useSetting(settingsAtom, 'multiplePreviews'); const settingsLinkBaseUrl = useSettingsLinkBaseUrl(); const captionPositionMap = { @@ -124,12 +128,14 @@ function RenderMessageContentInternal({ ); if (filteredUrls.length === 0) return undefined; - const themePreviewUrls = themeChatAny - ? filteredUrls.filter((u) => /\.preview\.sable\.css(\?|#|$)/i.test(u)) + const themePreviewUrls = themeChatSableWidgets + ? filteredUrls.filter( + (u) => /^https:\/\//i.test(u) && /\.preview\.sable\.css(\?|#|$)/i.test(u) + ) : []; const themeToRender = themePreviewUrls.filter((u) => /^https:\/\//i.test(u)); - const tweakCandidateUrls = themeChatAny + const tweakCandidateUrls = themeChatSableWidgets ? filteredUrls.filter((u) => isHttpsFullSableCssUrl(u)) : []; @@ -155,6 +161,7 @@ function RenderMessageContentInternal({ if (type) { return ; } + if (!themeChatSableWidgets && isSableChatEmbedCandidate(url)) return null; if (clientUrlPreview && youtubeUrl(url)) { return ; } @@ -166,7 +173,7 @@ function RenderMessageContentInternal({ ); }, - [multiplePreviews, themeChatAny, settingsLinkBaseUrl, clientUrlPreview, urlPreview, ts] + [multiplePreviews, themeChatSableWidgets, settingsLinkBaseUrl, clientUrlPreview, urlPreview, ts] ); const renderBundledPreviews = useCallback( (bundles: IPreviewUrlResponse[]) => ( @@ -183,7 +190,7 @@ function RenderMessageContentInternal({ ), [urlPreview] ); - const messageUrlsPreview = urlPreview || themeChatAny ? renderUrlsPreview : undefined; + const messageUrlsPreview = urlPreview || themeChatSableWidgets ? renderUrlsPreview : undefined; const messageBundlePreview = bundledPreview ? renderBundledPreviews : undefined; const renderCaption = () => { diff --git a/src/app/components/url-preview/SableChatPreviewPlaceholder.tsx b/src/app/components/url-preview/SableChatPreviewPlaceholder.tsx new file mode 100644 index 000000000..1fa6d6222 --- /dev/null +++ b/src/app/components/url-preview/SableChatPreviewPlaceholder.tsx @@ -0,0 +1,71 @@ +import { Box, Button, Text, config, toRem } from 'folds'; + +import { ThemeThirdPartyBanner } from './ThemeThirdPartyBanner'; + +export type SableChatPreviewPlaceholderProps = { + kind: 'theme' | 'tweak'; + url: string; + hostLabel: string; + isApprovedHost: boolean; + onLoadPreview: () => void; +}; + +export function SableChatPreviewPlaceholder({ + kind, + url, + hostLabel, + isApprovedHost, + onLoadPreview, +}: SableChatPreviewPlaceholderProps) { + const title = kind === 'theme' ? 'Theme preview' : 'Tweak preview'; + const bodyApproved = + kind === 'theme' + ? 'This message links to a catalog theme preview. Nothing is fetched until you load it.' + : 'This message links to a catalog tweak. Nothing is fetched until you load it.'; + const bodyThirdParty = + kind === 'theme' + ? 'This message links to third-party theme CSS. Nothing is fetched until you confirm. Only load previews you trust.' + : 'This message links to third-party tweak CSS. Nothing is fetched until you confirm. Only load previews you trust.'; + + return ( + + + {title} + {!isApprovedHost ? : null} + + {isApprovedHost ? bodyApproved : bodyThirdParty} + + + {isApprovedHost ? 'Official catalog' : hostLabel} + + + {url} + + + + + ); +} diff --git a/src/app/components/url-preview/ThemePreviewUrlCard.tsx b/src/app/components/url-preview/ThemePreviewUrlCard.tsx index 9c207bb9d..f0a5e8eed 100644 --- a/src/app/components/url-preview/ThemePreviewUrlCard.tsx +++ b/src/app/components/url-preview/ThemePreviewUrlCard.tsx @@ -17,6 +17,7 @@ import { putCachedThemeCss } from '../../theme/cache'; import { fullUrlFromPreviewUrl } from '../../theme/previewUrls'; import { isApprovedCatalogHostUrl } from '../../theme/themeApproval'; import { ThemePreviewCard } from '../theme/ThemePreviewCard'; +import { SableChatPreviewPlaceholder } from './SableChatPreviewPlaceholder'; import { ThemeThirdPartyBanner } from './ThemeThirdPartyBanner'; function isHttps(url: string): boolean { @@ -54,22 +55,36 @@ export function ThemePreviewUrlCard({ url }: { url: string }) { }; }, []); - const [chatAny] = useSetting(settingsAtom, 'themeChatPreviewAnyUrl'); + const [autoPreviewApproved] = useSetting(settingsAtom, 'themeChatAutoPreviewApprovedUrls'); + const [autoPreviewAny] = useSetting(settingsAtom, 'themeChatAutoPreviewAnyUrl'); const [favorites] = useSetting(settingsAtom, 'themeRemoteFavorites'); const [systemTheme] = useSetting(settingsAtom, 'useSystemTheme'); const [manualRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteManualFullUrl'); const [lightRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteLightFullUrl'); const [darkRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteDarkFullUrl'); - const allowed = useMemo(() => { - if (!chatAny) return false; - return isHttps(url) && isPreviewThemeUrl(url); - }, [chatAny, url]); + const isEligibleUrl = useMemo(() => isHttps(url) && isPreviewThemeUrl(url), [url]); const prefixes = clientConfig.themeCatalogApprovedHostPrefixes; const isOfficial = useMemo(() => isApprovedCatalogHostUrl(url, prefixes), [prefixes, url]); + const shouldAutoFetch = useMemo( + () => (isOfficial && autoPreviewApproved) || (!isOfficial && autoPreviewAny), + [isOfficial, autoPreviewApproved, autoPreviewAny] + ); + + const [userTriggeredLoad, setUserTriggeredLoad] = useState(false); + + useEffect(() => { + setUserTriggeredLoad(false); + }, [url]); + + const fetchEnabled = useMemo( + () => isEligibleUrl && (shouldAutoFetch || userTriggeredLoad), + [isEligibleUrl, shouldAutoFetch, userTriggeredLoad] + ); + const showThirdPartyBanner = useMemo( () => !isApprovedCatalogHostUrl(url, prefixes), [prefixes, url] @@ -77,7 +92,7 @@ export function ThemePreviewUrlCard({ url }: { url: string }) { const previewQuery = useQuery({ queryKey: ['theme-preview-embed', url], - enabled: allowed, + enabled: fetchEnabled, staleTime: 10 * 60_000, queryFn: async () => { const res = await fetch(url, { mode: 'cors' }); @@ -360,7 +375,21 @@ export function ThemePreviewUrlCard({ url }: { url: string }) { store, ]); - if (!allowed) return null; + if (!isEligibleUrl) return null; + + if (!shouldAutoFetch && !userTriggeredLoad) { + return ( + { + setUserTriggeredLoad(true); + }} + /> + ); + } const title = previewQuery.data?.displayName ?? 'Theme preview'; let kindLabel = 'Theme'; diff --git a/src/app/components/url-preview/ThemeThirdPartyBanner.tsx b/src/app/components/url-preview/ThemeThirdPartyBanner.tsx index 1f97a89bb..ca31d8008 100644 --- a/src/app/components/url-preview/ThemeThirdPartyBanner.tsx +++ b/src/app/components/url-preview/ThemeThirdPartyBanner.tsx @@ -2,9 +2,17 @@ import { Box, Icon, Icons, Text, toRem, config } from 'folds'; type ThemeThirdPartyBannerProps = { hostLabel: string; + /** Default `theme` for backward compatibility. */ + kind?: 'theme' | 'tweak'; }; -export function ThemeThirdPartyBanner({ hostLabel }: ThemeThirdPartyBannerProps) { +export function ThemeThirdPartyBanner({ hostLabel, kind = 'theme' }: ThemeThirdPartyBannerProps) { + const title = kind === 'tweak' ? 'Third-party tweak' : 'Third-party theme'; + const body = + kind === 'tweak' + ? `This tweak is hosted on ${hostLabel}, outside the Sable catalog allowlist. Saving or enabling applies CSS from that host. Only use tweaks from providers you trust.` + : `This preview is hosted on ${hostLabel}, outside the Sable catalog allowlist. Saving or applying installs full theme CSS from that host. Only use themes from providers you trust.`; + return ( - Third-party theme + {title} - This preview is hosted on {hostLabel}, outside the Sable catalog allowlist. Saving or - applying installs full theme CSS from that host. Only use themes from Providers you - trust. + {body} diff --git a/src/app/components/url-preview/TweakPreviewUrlCard.tsx b/src/app/components/url-preview/TweakPreviewUrlCard.tsx index 8d5baece7..ab3ce5e2c 100644 --- a/src/app/components/url-preview/TweakPreviewUrlCard.tsx +++ b/src/app/components/url-preview/TweakPreviewUrlCard.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useMemo, useRef } from 'react'; +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { Box, @@ -25,6 +25,7 @@ import { getSableCssPackageKind, parseSableTweakMetadata } from '../../theme/met import { isHttpsFullSableCssUrl } from '../../theme/previewUrls'; import { buildPreviewStyleBlock, extractSafePreviewCustomProperties } from '../../theme/previewCss'; import { isApprovedCatalogHostUrl, isThirdPartyThemeUrl } from '../../theme/themeApproval'; +import { SableChatPreviewPlaceholder } from './SableChatPreviewPlaceholder'; import { ThemeThirdPartyBanner } from './ThemeThirdPartyBanner'; function baseLabel(url: string): string { @@ -73,18 +74,35 @@ export function TweakPreviewUrlCard({ url }: { url: string }) { }; }, []); - const [chatAny] = useSetting(settingsAtom, 'themeChatPreviewAnyUrl'); + const [autoPreviewApproved] = useSetting(settingsAtom, 'themeChatAutoPreviewApprovedUrls'); + const [autoPreviewAny] = useSetting(settingsAtom, 'themeChatAutoPreviewAnyUrl'); const [tweakFavorites] = useSetting(settingsAtom, 'themeRemoteTweakFavorites'); const [enabledTweakFullUrls] = useSetting(settingsAtom, 'themeRemoteEnabledTweakFullUrls'); const [copied, setCopied] = useTimeoutToggle(); - const allowed = useMemo(() => chatAny && isHttpsFullSableCssUrl(url), [chatAny, url]); + const isEligibleUrl = useMemo(() => isHttpsFullSableCssUrl(url), [url]); const prefixes = clientConfig.themeCatalogApprovedHostPrefixes; const isOfficial = useMemo(() => isApprovedCatalogHostUrl(url, prefixes), [prefixes, url]); + const shouldAutoFetch = useMemo( + () => (isOfficial && autoPreviewApproved) || (!isOfficial && autoPreviewAny), + [isOfficial, autoPreviewApproved, autoPreviewAny] + ); + + const [userTriggeredLoad, setUserTriggeredLoad] = useState(false); + + useEffect(() => { + setUserTriggeredLoad(false); + }, [url]); + + const fetchEnabled = useMemo( + () => isEligibleUrl && (shouldAutoFetch || userTriggeredLoad), + [isEligibleUrl, shouldAutoFetch, userTriggeredLoad] + ); + const showThirdPartyBanner = useMemo( () => !isApprovedCatalogHostUrl(url, prefixes), [prefixes, url] @@ -92,7 +110,7 @@ export function TweakPreviewUrlCard({ url }: { url: string }) { const tweakPreviewQuery = useQuery({ queryKey: ['tweak-preview-embed', url], - enabled: allowed, + enabled: fetchEnabled, staleTime: 10 * 60_000, queryFn: async (): Promise => { const res = await fetch(url, { mode: 'cors' }); @@ -187,7 +205,21 @@ export function TweakPreviewUrlCard({ url }: { url: string }) { [data, enabledTweakFullUrls, patchSettings, tweakFavorites, url] ); - if (!allowed) return null; + if (!isEligibleUrl) return null; + + if (!shouldAutoFetch && !userTriggeredLoad) { + return ( + { + setUserTriggeredLoad(true); + }} + /> + ); + } if (tweakPreviewQuery.isPending) { return ( @@ -313,7 +345,9 @@ export function TweakPreviewUrlCard({ url }: { url: string }) { - {showThirdPartyBanner ? : undefined} + {showThirdPartyBanner ? ( + + ) : undefined} {styleBlock ? ( <> diff --git a/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx b/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx index 913e5aba5..192473284 100644 --- a/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx +++ b/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx @@ -216,7 +216,18 @@ export function ThemeCatalogSettings({ const [manualRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteManualFullUrl'); const [lightRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteLightFullUrl'); const [darkRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteDarkFullUrl'); - const [chatAny, setChatAny] = useSetting(settingsAtom, 'themeChatPreviewAnyUrl'); + const [sableChatWidgets, setSableChatWidgets] = useSetting( + settingsAtom, + 'themeChatSableWidgetsEnabled' + ); + const [autoPreviewApprovedUrls, setAutoPreviewApprovedUrls] = useSetting( + settingsAtom, + 'themeChatAutoPreviewApprovedUrls' + ); + const [autoPreviewAnyUrl, setAutoPreviewAnyUrl] = useSetting( + settingsAtom, + 'themeChatAutoPreviewAnyUrl' + ); const [themeSearch, setThemeSearch] = useState(''); const [tweakSearch, setTweakSearch] = useState(''); @@ -1410,19 +1421,59 @@ export function ThemeCatalogSettings({ )} {isChatMode && ( - - } - /> - + <> + + + } + /> + + + + } + /> + + + + } + /> + {sableChatWidgets && autoPreviewAnyUrl && ( + + Third-party CSS can change how Sable looks and may not be safe. Prefer keeping this + off and loading previews manually only when you trust the link. + + )} + + )} ); diff --git a/src/app/state/settings.ts b/src/app/state/settings.ts index 38715c0d9..8fafebafd 100644 --- a/src/app/state/settings.ts +++ b/src/app/state/settings.ts @@ -155,8 +155,9 @@ export interface Settings { themeCatalogOnboardingDone: boolean; themeRemoteFavorites: ThemeRemoteFavorite[]; themeRemoteCatalogEnabled: boolean; - themeChatPreviewAnyUrl: boolean; - themeChatPreviewApprovedCatalogOnly: boolean; + themeChatSableWidgetsEnabled: boolean; + themeChatAutoPreviewApprovedUrls: boolean; + themeChatAutoPreviewAnyUrl: boolean; themeRemoteManualFullUrl?: string; themeRemoteLightFullUrl?: string; themeRemoteDarkFullUrl?: string; @@ -271,8 +272,9 @@ export const defaultSettings: Settings = { themeCatalogOnboardingDone: false, themeRemoteFavorites: [], themeRemoteCatalogEnabled: false, - themeChatPreviewAnyUrl: false, - themeChatPreviewApprovedCatalogOnly: false, + themeChatSableWidgetsEnabled: true, + themeChatAutoPreviewApprovedUrls: true, + themeChatAutoPreviewAnyUrl: false, themeRemoteManualFullUrl: undefined, themeRemoteLightFullUrl: undefined, themeRemoteDarkFullUrl: undefined, @@ -309,6 +311,16 @@ export const getSettings = () => { parsed.renderUserCards = 'both'; } + const parsedRecord = parsed as Record; + if ( + typeof parsedRecord.themeChatAutoPreviewAnyUrl !== 'boolean' && + typeof parsedRecord.themeChatPreviewAnyUrl === 'boolean' + ) { + parsedRecord.themeChatAutoPreviewAnyUrl = parsedRecord.themeChatPreviewAnyUrl; + } + delete parsedRecord.themeChatPreviewAnyUrl; + delete parsedRecord.themeChatPreviewApprovedCatalogOnly; + return { ...defaultSettings, ...(parsed as Settings),