Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(connections): make sure that getThemeOf method gets updated when colors change #6162

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,29 @@ describe('useTabConnectionTheme', function () {
).to.equal(undefined);
});
});

it('tracks updates of connection color state and returns a new method when they are changed', async function () {
const { result, connectionsStore } = renderHookWithConnections(
useTabConnectionTheme,
{
preferences: { enableMultipleConnectionSystem: true },
connections: [CONNECTION_INFO],
}
);

const getThemeOf = result.current.getThemeOf;

await connectionsStore.actions.saveEditedConnection({
...CONNECTION_INFO,
favorite: {
...CONNECTION_INFO.favorite,
color: 'color1',
},
});

expect(result.current.getThemeOf).to.not.eq(getThemeOf);
expect(result.current.getThemeOf(CONNECTION_INFO.id)).to.not.eq(
getThemeOf(CONNECTION_INFO.id)
);
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type ConnectionInfo } from '@mongodb-js/connection-info';
import { useConnectionColor } from '@mongodb-js/connection-form';
import { useConnectionRepository } from './use-connection-repository';
import { useDarkMode, type TabTheme } from '@mongodb-js/compass-components';
import { palette } from '@mongodb-js/compass-components';
import { useCallback } from 'react';
import { usePreference } from 'compass-preferences-model/provider';
import { useConnectionsColorList } from '../stores/store-context';

type ThemeProvider = {
getThemeOf(
Expand All @@ -16,20 +16,17 @@ type ThemeProvider = {
export function useTabConnectionTheme(): ThemeProvider {
const { connectionColorToHex, connectionColorToHexActive } =
useConnectionColor();
const { getConnectionInfoById } = useConnectionRepository();
const connectionColorsList = useConnectionsColorList();
const darkTheme = useDarkMode();
const isMultipleConnectionsEnabled = usePreference(
'enableMultipleConnectionSystem'
);

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

Expand Down Expand Up @@ -70,7 +67,7 @@ export function useTabConnectionTheme(): ThemeProvider {
},
[
palette,
getConnectionInfoById,
connectionColorsList,
connectionColorToHex,
connectionColorToHexActive,
darkTheme,
Expand Down
14 changes: 14 additions & 0 deletions packages/compass-connections/src/stores/store-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,17 @@ export function useConnectionsState() {
noopCheck: 'never',
});
}

export function useConnectionsColorList(): {
id: ConnectionId;
color: string | undefined;
}[] {
return useSelector((state) => {
return Object.values(state.connections.byId).map((connection) => {
return {
id: connection.info.id,
color: connection.info.favorite?.color,
};
});
}, isEqual);
}
Loading