Skip to content

Commit

Permalink
feat: add color theme selector
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 5, 2023
1 parent b50e809 commit 5e6190b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
34 changes: 34 additions & 0 deletions api/v2/user_setting_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (s *UserSettingService) UpdateUserSetting(ctx context.Context, request *api
}); err != nil {
return nil, status.Errorf(codes.Internal, "failed to update user setting: %v", err)
}
} else if path == "color_theme" {
if _, err := s.Store.UpsertUserSetting(ctx, &storepb.UserSetting{
UserId: userID,
Key: storepb.UserSettingKey_USER_SETTING_COLOR_THEME,
Value: &storepb.UserSetting_ColorTheme{
ColorTheme: convertUserSettingColorThemeToStore(request.UserSetting.ColorTheme),
},
}); err != nil {
return nil, status.Errorf(codes.Internal, "failed to update user setting: %v", err)
}
} else {
return nil, status.Errorf(codes.InvalidArgument, "invalid path: %s", path)
}
Expand Down Expand Up @@ -80,6 +90,8 @@ func getUserSetting(ctx context.Context, s *store.Store, userID int32) (*apiv2pb
for _, setting := range userSettings {
if setting.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
userSetting.Locale = convertUserSettingLocaleFromStore(setting.GetLocale())
} else if setting.Key == storepb.UserSettingKey_USER_SETTING_COLOR_THEME {
userSetting.ColorTheme = convertUserSettingColorThemeFromStore(setting.GetColorTheme())
}
}
return userSetting, nil
Expand All @@ -106,3 +118,25 @@ func convertUserSettingLocaleFromStore(locale storepb.LocaleUserSetting) apiv2pb
return apiv2pb.UserSetting_LOCALE_UNSPECIFIED
}
}

func convertUserSettingColorThemeToStore(colorTheme apiv2pb.UserSetting_ColorTheme) storepb.ColorThemeUserSetting {
switch colorTheme {
case apiv2pb.UserSetting_COLOR_THEME_LIGHT:
return storepb.ColorThemeUserSetting_COLOR_THEME_USER_SETTING_LIGHT
case apiv2pb.UserSetting_COLOR_THEME_DARK:
return storepb.ColorThemeUserSetting_COLOR_THEME_USER_SETTING_DARK
default:
return storepb.ColorThemeUserSetting_COLOR_THEME_USER_SETTING_UNSPECIFIED
}
}

func convertUserSettingColorThemeFromStore(colorTheme storepb.ColorThemeUserSetting) apiv2pb.UserSetting_ColorTheme {
switch colorTheme {
case storepb.ColorThemeUserSetting_COLOR_THEME_USER_SETTING_LIGHT:
return apiv2pb.UserSetting_COLOR_THEME_LIGHT
case storepb.ColorThemeUserSetting_COLOR_THEME_USER_SETTING_DARK:
return apiv2pb.UserSetting_COLOR_THEME_DARK
default:
return apiv2pb.UserSetting_COLOR_THEME_UNSPECIFIED
}
}
40 changes: 39 additions & 1 deletion frontend/web/src/components/setting/PreferenceSection.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Option, Select } from "@mui/joy";
import { useTranslation } from "react-i18next";
import { UserSetting, UserSetting_Locale } from "@/types/proto/api/v2/user_setting_service_pb";
import { UserSetting, UserSetting_ColorTheme, UserSetting_Locale } from "@/types/proto/api/v2/user_setting_service_pb";
import useUserStore from "../../stores/v1/user";

const PreferenceSection: React.FC = () => {
const { t } = useTranslation();
const userStore = useUserStore();
const userSetting = userStore.getCurrentUserSetting();
const language = userSetting.locale || UserSetting_Locale.EN;
const colorTheme = userSetting.colorTheme;

const languageOptions = [
{
Expand All @@ -20,6 +21,21 @@ const PreferenceSection: React.FC = () => {
},
];

const colorThemeOptions = [
{
value: "COLOR_THEME_UNSPECIFIED",
label: "Auto",
},
{
value: "COLOR_THEME_LIGHT",
label: "Light",
},
{
value: "COLOR_THEME_DARK",
label: "Dark",
},
];

const handleSelectLanguage = async (locale: UserSetting_Locale) => {
if (!locale) {
return;
Expand All @@ -34,6 +50,16 @@ const PreferenceSection: React.FC = () => {
);
};

const handleSelectColorTheme = async (colorTheme: UserSetting_ColorTheme) => {
await userStore.updateUserSetting(
{
...userSetting,
colorTheme: colorTheme,
} as UserSetting,
["color_theme"]
);
};

return (
<>
<div className="w-full flex flex-col justify-start items-start gap-y-2">
Expand All @@ -50,6 +76,18 @@ const PreferenceSection: React.FC = () => {
})}
</Select>
</div>
<div className="w-full flex flex-row justify-between items-center">
<span>Color Theme</span>
<Select defaultValue={colorTheme} onChange={(_, value) => handleSelectColorTheme(value as UserSetting_ColorTheme)}>
{colorThemeOptions.map((option) => {
return (
<Option key={option.value} value={option.value}>
{option.label}
</Option>
);
})}
</Select>
</div>
</div>
</>
);
Expand Down

0 comments on commit 5e6190b

Please sign in to comment.