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

feat(ui): Added theme profile to log-viewer #21448

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,17 +3,65 @@ import * as React from 'react';
import {ToggleButton} from '../../../shared/components/toggle-button';

// DarkModeToggleButton is a component that renders a toggle button that toggles dark mode.
export const DarkModeToggleButton = ({prefs}: {prefs: ViewPreferences}) => (
<ToggleButton
title='Dark Mode'
onToggle={() => {
const inverted = prefs.appDetails.darkMode;
// Added a logviewer theme profile

interface LogViewerTheme {
light: boolean;
dark: boolean;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This data structure is strange to me. What would be the meaning of {light: true, dark: true}?

Copy link
Member

@jsoref jsoref Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the structure is really

interface UseDarkModeInLogViewerForAppTheme {
  light: boolean;
  dark: boolean;
}

where light: false means "don't use dark mode in log viewer when in light mode app theme" and dark: true means "use dark mode in log viewer when in dark mode app theme".
and light: true means "use dark mode in log viewer when in light mode app theme" and dark: false means "use light mode in log viewer when in dark mode app theme".

Those last two settings exist to enable users to hurt their eyes, but apparently they're desirable modes, so here we are :)

-- I can't think of a particularly better way to structure the thing, but...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is what it represent my naming wasn't quite right


export const DarkModeToggleButton = ({prefs}: {prefs: ViewPreferences}) => {
React.useEffect(() => {
const profiles = prefs.appDetails.LogViewerTheme as LogViewerTheme;
const currentTheme = prefs.theme;

if (!profiles) {
services.viewPreferences.updatePreferences({
...prefs,
appDetails: {
...prefs.appDetails,
LogViewerTheme: {
light: false,
dark: true
},
darkMode: currentTheme === 'dark'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we duplicating the theme params here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 yes, there is duplication happening here as both darkMode and LogViewerTheme are controlling the state and used to update and store preferences,

tried multiple ways to have the userpreference with the current darkMode controller without duplication coudn't get to a good solution without it, I am refactoring it with small change like using darkMode itself

LogViewerTheme: {
light: prefs.appDetails.darkMode,
dark: prefs.appDetails.darkMode
}
and if this is getting overly complicated or redundant we can just have the log-viewer darkMode toggle controlled by global theme instead with no saved preferences. let me know where can I improve if this is not quite right

}
});
return;
}

const profilePreference = currentTheme === 'dark' ? profiles.dark : profiles.light;
if (prefs.appDetails.darkMode !== profilePreference) {
services.viewPreferences.updatePreferences({
...prefs,
appDetails: {...prefs.appDetails, darkMode: !inverted}
appDetails: {
...prefs.appDetails,
darkMode: profilePreference
}
});
}}
toggled={prefs.appDetails.darkMode}
icon='moon'
/>
);
}
}, [prefs.theme]);

const handleToggle = () => {
const newDarkMode = !prefs.appDetails.darkMode;
const currentTheme = prefs.theme;
const profiles = (prefs.appDetails.LogViewerTheme as LogViewerTheme) || {
light: false,
dark: true
};

services.viewPreferences.updatePreferences({
...prefs,
appDetails: {
...prefs.appDetails,
darkMode: newDarkMode,
LogViewerTheme: {
...profiles,
[currentTheme]: newDarkMode
}
}
});
};

return <ToggleButton title='Dark Mode' onToggle={handleToggle} toggled={prefs.appDetails.darkMode} icon='moon' />;
};
1 change: 1 addition & 0 deletions ui/src/app/shared/services/view-preferences-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface AppDetailsPreferences {
zoom: number;
podGroupCount: number;
userHelpTipMsgs: UserMessages[];
LogViewerTheme?: {light: boolean; dark: boolean};
}

export interface PodViewPreferences {
Expand Down
Loading