Skip to content

Commit fc7d817

Browse files
authored
chore(connections): make sure that getThemeOf method gets updated when colors change (#6162)
1 parent eddd080 commit fc7d817

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

packages/compass-connections/src/hooks/use-tab-connection-theme.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,29 @@ describe('useTabConnectionTheme', function () {
8989
).to.equal(undefined);
9090
});
9191
});
92+
93+
it('tracks updates of connection color state and returns a new method when they are changed', async function () {
94+
const { result, connectionsStore } = renderHookWithConnections(
95+
useTabConnectionTheme,
96+
{
97+
preferences: { enableMultipleConnectionSystem: true },
98+
connections: [CONNECTION_INFO],
99+
}
100+
);
101+
102+
const getThemeOf = result.current.getThemeOf;
103+
104+
await connectionsStore.actions.saveEditedConnection({
105+
...CONNECTION_INFO,
106+
favorite: {
107+
...CONNECTION_INFO.favorite,
108+
color: 'color1',
109+
},
110+
});
111+
112+
expect(result.current.getThemeOf).to.not.eq(getThemeOf);
113+
expect(result.current.getThemeOf(CONNECTION_INFO.id)).to.not.eq(
114+
getThemeOf(CONNECTION_INFO.id)
115+
);
116+
});
92117
});

packages/compass-connections/src/hooks/use-tab-connection-theme.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { type ConnectionInfo } from '@mongodb-js/connection-info';
22
import { useConnectionColor } from '@mongodb-js/connection-form';
3-
import { useConnectionRepository } from './use-connection-repository';
43
import { useDarkMode, type TabTheme } from '@mongodb-js/compass-components';
54
import { palette } from '@mongodb-js/compass-components';
65
import { useCallback } from 'react';
76
import { usePreference } from 'compass-preferences-model/provider';
7+
import { useConnectionsColorList } from '../stores/store-context';
88

99
type ThemeProvider = {
1010
getThemeOf(
@@ -16,20 +16,17 @@ type ThemeProvider = {
1616
export function useTabConnectionTheme(): ThemeProvider {
1717
const { connectionColorToHex, connectionColorToHexActive } =
1818
useConnectionColor();
19-
const { getConnectionInfoById } = useConnectionRepository();
19+
const connectionColorsList = useConnectionsColorList();
2020
const darkTheme = useDarkMode();
2121
const isMultipleConnectionsEnabled = usePreference(
2222
'enableMultipleConnectionSystem'
2323
);
2424

25-
// TODO: this method is not reactive and works only by accident, refactor the
26-
// hook to explicitly track changes to color in connections, otherwise the
27-
// value of the theme might be stale when we remove `useConnectionRepository`
28-
// hook completely
2925
const getThemeOf = useCallback(
3026
(connectionId: ConnectionInfo['id']) => {
31-
const connectionInfo = getConnectionInfoById(connectionId);
32-
const color = connectionInfo?.favorite?.color;
27+
const color = connectionColorsList.find((connection) => {
28+
return connection.id === connectionId;
29+
})?.color;
3330
const bgColor = connectionColorToHex(color);
3431
const activeBgColor = connectionColorToHexActive(color);
3532

@@ -70,7 +67,7 @@ export function useTabConnectionTheme(): ThemeProvider {
7067
},
7168
[
7269
palette,
73-
getConnectionInfoById,
70+
connectionColorsList,
7471
connectionColorToHex,
7572
connectionColorToHexActive,
7673
darkTheme,

packages/compass-connections/src/stores/store-context.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,17 @@ export function useConnectionsState() {
335335
noopCheck: 'never',
336336
});
337337
}
338+
339+
export function useConnectionsColorList(): {
340+
id: ConnectionId;
341+
color: string | undefined;
342+
}[] {
343+
return useSelector((state) => {
344+
return Object.values(state.connections.byId).map((connection) => {
345+
return {
346+
id: connection.info.id,
347+
color: connection.info.favorite?.color,
348+
};
349+
});
350+
}, isEqual);
351+
}

0 commit comments

Comments
 (0)