From d162478100f2aa5572210a5817a03cda83a1c746 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:45:34 +0800 Subject: [PATCH] perf(ui): hoist static dialog identities and memoize settings search The DNS viewer rebuilt Monaco's options object per keystroke, the update dialog rebuilt its markdown component map and plugin array per progress chunk (remounting link and blockquote subtrees), and the settings search rescanned the ~180-entry catalog on unrelated renders such as arrow-key selection. These identities are now module constants or single memos; the width-dependent minimap option is applied at editor mount. --- src/components/setting/mods/dns-viewer.tsx | 58 +++--- src/components/setting/mods/update-viewer.tsx | 165 +++++++++--------- src/components/setting/settings-search.tsx | 21 ++- 3 files changed, 132 insertions(+), 112 deletions(-) diff --git a/src/components/setting/mods/dns-viewer.tsx b/src/components/setting/mods/dns-viewer.tsx index a6cace3ea..f0282a7ba 100644 --- a/src/components/setting/mods/dns-viewer.tsx +++ b/src/components/setting/mods/dns-viewer.tsx @@ -51,6 +51,25 @@ const Item = styled(ListItem)(() => ({ }, })) +// Static editor settings; a per-render object would re-trigger Monaco's +// updateOptions and listener resubscription on every keystroke. The minimap +// depends on the live window width and is applied at editor mount instead. +const MONACO_OPTIONS = { + tabSize: 2, + mouseWheelZoom: true, + quickSuggestions: { + strings: true, + comments: true, + other: true, + }, + padding: { + top: 33, + }, + fontFamily: MONACO_FONT_FAMILY, + fontLigatures: false, + smoothScrolling: true, +} as const + type NameserverPolicy = Record function parseNameserverPolicy(str: string): NameserverPolicy { @@ -520,9 +539,21 @@ export function DnsViewer({ ref }: { ref?: Ref }) { } }) - const handleYamlChange = (value?: string) => { + const handleYamlChange = useCallback((value?: string) => { setYamlContent(value || '') - } + }, []) + + const handleEditorMount = useCallback( + (editorInstance: MonacoEditorInstance) => { + editorRef.current = editorInstance + editorInstance.updateOptions({ + minimap: { + enabled: document.documentElement.clientWidth >= 1500, + }, + }) + }, + [], + ) const handleChange = (field: string) => (event: any) => { const value = @@ -1052,27 +1083,8 @@ export function DnsViewer({ ref }: { ref?: Ref }) { value={yamlContent} theme={themeMode === 'light' ? 'light' : 'vs-dark'} className="flex-grow" - onMount={(editorInstance) => { - editorRef.current = editorInstance - }} - options={{ - tabSize: 2, - minimap: { - enabled: document.documentElement.clientWidth >= 1500, - }, - mouseWheelZoom: true, - quickSuggestions: { - strings: true, - comments: true, - other: true, - }, - padding: { - top: 33, - }, - fontFamily: MONACO_FONT_FAMILY, - fontLigatures: false, - smoothScrolling: true, - }} + onMount={handleEditorMount} + options={MONACO_OPTIONS} onChange={handleYamlChange} /> )} diff --git a/src/components/setting/mods/update-viewer.tsx b/src/components/setting/mods/update-viewer.tsx index dc6a2659f..6ec19d1ab 100644 --- a/src/components/setting/mods/update-viewer.tsx +++ b/src/components/setting/mods/update-viewer.tsx @@ -114,6 +114,84 @@ const getAlertTypeFromClassName = ( return match?.[1] as GitHubAlertType | null } +const openUrlWithNotice = (url: string) => { + void openExternalUrl(url).catch(showNotice.error) +} + +// Renderer map is static; a per-render identity would remount every link and +// blockquote subtree while download progress re-renders the dialog. +const MARKDOWN_COMPONENTS: ReactMarkdownOptions['components'] = { + a: ({ href, children, ...props }) => { + const isFragment = href?.startsWith('#') ?? false + const renderedHref = isFragment + ? `#${UPDATE_MARKDOWN_ID_PREFIX}${href?.slice(1)}` + : href + ? '#' + : undefined + + return ( + { + if (isFragment) return + event.preventDefault() + if (!href) return + openUrlWithNotice(href) + }} + onAuxClick={(event) => { + if (isFragment) return + event.preventDefault() + if (event.button === 1 && href) { + openUrlWithNotice(href) + } + }} + > + {children} + + ) + }, + blockquote: ({ className, children }) => { + const alertType = getAlertTypeFromClassName(className) + + if (!alertType) { + return
{children}
+ } + + return ( + { + const color = GITHUB_ALERTS[alertType].color + return { + m: '12px 0 18px', + px: 2, + py: 1, + borderLeft: `4px solid ${color}`, + borderRadius: 1, + bgcolor: alpha(color, theme.palette.mode === 'dark' ? 0.16 : 0.08), + '& p': { + my: 0.75, + }, + '& .markdown-alert-title': { + display: 'flex', + alignItems: 'center', + gap: 0.75, + fontWeight: 700, + lineHeight: 1.4, + }, + } + }} + > + {children} + + ) + }, +} + const findFirstTextNode = (node: MarkdownNode): MarkdownNode | null => { if (node.type === 'text') return node for (const child of node.children ?? []) { @@ -185,10 +263,6 @@ export function UpdateViewer({ ref }: { ref?: Ref }) { }) const { downloaded, total } = download - const openUrlWithNotice = (url: string) => { - void openExternalUrl(url).catch(showNotice.error) - } - const progress = useMemo(() => { if (total <= 0) return 0 return Math.min((downloaded / total) * 100, 100) @@ -212,8 +286,8 @@ export function UpdateViewer({ ref }: { ref?: Ref }) { ) as Record, [t], ) - const remarkGitHubAlertsPlugin = useMemo( - () => () => remarkGitHubAlerts(githubAlertLabels), + const remarkPlugins = useMemo( + () => [() => remarkGitHubAlerts(githubAlertLabels)], [githubAlertLabels], ) @@ -475,83 +549,8 @@ export function UpdateViewer({ ref }: { ref?: Ref }) { {open && ( }> { - const isFragment = href?.startsWith('#') ?? false - const renderedHref = isFragment - ? `#${UPDATE_MARKDOWN_ID_PREFIX}${href?.slice(1)}` - : href - ? '#' - : undefined - - return ( - { - if (isFragment) return - event.preventDefault() - if (!href) return - openUrlWithNotice(href) - }} - onAuxClick={(event) => { - if (isFragment) return - event.preventDefault() - if (event.button === 1 && href) { - openUrlWithNotice(href) - } - }} - > - {children} - - ) - }, - blockquote: ({ className, children }) => { - const alertType = getAlertTypeFromClassName(className) - - if (!alertType) { - return ( -
{children}
- ) - } - - return ( - { - const color = GITHUB_ALERTS[alertType].color - return { - m: '12px 0 18px', - px: 2, - py: 1, - borderLeft: `4px solid ${color}`, - borderRadius: 1, - bgcolor: alpha( - color, - theme.palette.mode === 'dark' ? 0.16 : 0.08, - ), - '& p': { - my: 0.75, - }, - '& .markdown-alert-title': { - display: 'flex', - alignItems: 'center', - gap: 0.75, - fontWeight: 700, - lineHeight: 1.4, - }, - } - }} - > - {children} - - ) - }, - }} + remarkPlugins={remarkPlugins} + components={MARKDOWN_COMPONENTS} > {markdownContent}
diff --git a/src/components/setting/settings-search.tsx b/src/components/setting/settings-search.tsx index dc62b021c..71d16d83a 100644 --- a/src/components/setting/settings-search.tsx +++ b/src/components/setting/settings-search.tsx @@ -10,7 +10,7 @@ import { TextField, Typography, } from '@mui/material' -import { useEffect, useRef, useState, type ReactNode } from 'react' +import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react' import { useTranslation } from 'react-i18next' import getSystem from '@/utils/get-system' @@ -21,6 +21,9 @@ import { type SearchDialog, } from './settings-search-context' +// Constant for the runtime; module-level like the other getSystem() callers. +const OS = getSystem() + export function SettingsSearch({ children, }: { @@ -40,11 +43,17 @@ export function SettingsSearch({ const originalScrollRef = useRef(0) const resultScrollRef = useRef(0) const cancelLocateRef = useRef<() => void>(() => {}) - const results = searchSettings( - query, - getSystem(), - (key) => t(key), - (key) => t(key, { lng: 'en' }), + // The catalog scan touches every entry with translations; memoizing keeps + // unrelated state (arrow-key selection) from re-running it. + const results = useMemo( + () => + searchSettings( + query, + OS, + (key) => t(key), + (key) => t(key, { lng: 'en' }), + ), + [query, t], ) const searching = !!query.trim() const scroller = () =>