@@ -19,11 +19,38 @@ export type SettingsTarget = EnvironmentPresentation & {
1919 readonly serverConfig : NonNullable < EnvironmentPresentation [ "serverConfig" ] > ;
2020} ;
2121
22+ // Several mounted Settings components read the scope at once (the list, its header, a page and
23+ // its editor), so share the derived lists across them for the same inputs.
24+ const targetsCache = new WeakMap < readonly EnvironmentPresentation [ ] , readonly SettingsTarget [ ] > ( ) ;
25+ const groupsCache = new WeakMap < object , Map < string , ReturnType < typeof buildProjectGroups > > > ( ) ;
26+
2227function connectedSettingsTargets ( environments : readonly EnvironmentPresentation [ ] ) {
23- return environments . filter (
24- ( entry ) : entry is SettingsTarget =>
25- entry . connection . phase === "connected" && entry . serverConfig !== null ,
26- ) ;
28+ let targets = targetsCache . get ( environments ) ;
29+ if ( ! targets ) {
30+ targets = environments . filter (
31+ ( entry ) : entry is SettingsTarget =>
32+ entry . connection . phase === "connected" && entry . serverConfig !== null ,
33+ ) ;
34+ targetsCache . set ( environments , targets ) ;
35+ }
36+ return targets ;
37+ }
38+
39+ function settingsProjectGroups (
40+ projects : Parameters < typeof buildProjectGroups > [ 0 ] [ "projects" ] ,
41+ groupingMode : Parameters < typeof buildProjectGroups > [ 0 ] [ "settings" ] [ "sidebarProjectGroupingMode" ] ,
42+ ) {
43+ let byMode = groupsCache . get ( projects ) ;
44+ if ( ! byMode ) groupsCache . set ( projects , ( byMode = new Map ( ) ) ) ;
45+ let groups = byMode . get ( groupingMode ) ;
46+ if ( ! groups ) {
47+ groups = buildProjectGroups ( {
48+ projects,
49+ settings : { sidebarProjectGroupingMode : groupingMode , sidebarProjectGroupingOverrides : { } } ,
50+ } ) ;
51+ byMode . set ( groupingMode , groups ) ;
52+ }
53+ return groups ;
2754}
2855
2956interface SettingsEnvironmentSelection {
@@ -72,15 +99,8 @@ export function useSettingsEnvironmentFilter() {
7299 const projects = useProjects ( ) ;
73100 const groupingSettings = useMobileProjectGroupingSettings ( ) ;
74101 const groupingMode = groupingSettings . sidebarProjectGroupingMode ;
75- const projectGroups = useMemo (
76- ( ) =>
77- buildProjectGroups ( {
78- projects,
79- settings : { sidebarProjectGroupingMode : groupingMode , sidebarProjectGroupingOverrides : { } } ,
80- } ) ,
81- [ projects , groupingMode ] ,
82- ) ;
83- const availableTargets = useMemo ( ( ) => connectedSettingsTargets ( environments ) , [ environments ] ) ;
102+ const projectGroups = settingsProjectGroups ( projects , groupingMode ) ;
103+ const availableTargets = connectedSettingsTargets ( environments ) ;
84104 const selectedTargets = useMemo (
85105 ( ) =>
86106 availableTargets . filter (
0 commit comments