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: Enable flexible number of colors for themes #3482

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

joeng03
Copy link
Contributor

@joeng03 joeng03 commented Aug 4, 2023

  • instead of fixing the number of colors for a theme to 8 colors, now we are able to support a variable number of colors
  • add a new theme, 'Tequila' with 10 colors as Proof of Concept

Context

#3443

Implementation

Screenshot 2023-08-03 124401
One issue I was facing was that when shifting from themes with higher number of colors to themes with lower number of colors, some courses would get rendered as white color as their colorIndex exceeds the numOfColors of the current theme. One way to solve this would be to dispatch a new action to set the colorIndex of all courses to colorIndex % numOfColors.

Other Information

- instead of fixing the number of colors for a theme to 8 colors,
  now we are able to support a variable number of colors
- add a new theme, 'Tequila' with 10 colors as Proof of Concept
@vercel
Copy link

vercel bot commented Aug 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nusmods-export ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 14, 2023 2:29am
nusmods-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 14, 2023 2:29am

@vercel
Copy link

vercel bot commented Aug 4, 2023

@joeng03 is attempting to deploy a commit to a Personal Account owned by @nusmodifications on Vercel.

@nusmodifications first needs to authorize it.

@codecov
Copy link

codecov bot commented Aug 7, 2023

Codecov Report

Patch coverage: 43.90% and project coverage change: -0.10% ⚠️

Comparison is base (f65e58a) 53.44% compared to head (a7ac61e) 53.34%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3482      +/-   ##
==========================================
- Coverage   53.44%   53.34%   -0.10%     
==========================================
  Files         271      271              
  Lines        5862     5882      +20     
  Branches     1396     1397       +1     
==========================================
+ Hits         3133     3138       +5     
- Misses       2729     2744      +15     
Files Changed Coverage Δ
website/src/entry/export/TimetableOnly.tsx 0.00% <0.00%> (ø)
website/src/reducers/timetables.ts 76.76% <0.00%> (-8.63%) ⬇️
website/src/types/reducers.ts 100.00% <ø> (ø)
website/src/types/settings.ts 100.00% <ø> (ø)
website/src/views/settings/SettingsContainer.tsx 0.00% <0.00%> (ø)
website/src/views/settings/ThemeOption.tsx 0.00% <0.00%> (ø)
website/src/views/venues/VenueDetails.tsx 8.33% <0.00%> (-0.76%) ⬇️
website/src/actions/timetables.ts 79.16% <16.66%> (-3.19%) ⬇️
website/src/actions/theme.ts 83.33% <100.00%> (ø)
website/src/reducers/theme.ts 90.47% <100.00%> (ø)
... and 4 more

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zwliew
Copy link
Member

zwliew commented Aug 10, 2023

Hi, thanks for working on this! Is this PR still WIP as the description states or is it ready for review?

@joeng03
Copy link
Contributor Author

joeng03 commented Aug 10, 2023

It is ready for review!

website/src/reducers/timetables.ts Outdated Show resolved Hide resolved
website/src/reducers/timetables.ts Outdated Show resolved Hide resolved
website/src/reducers/timetables.ts Outdated Show resolved Hide resolved
website/src/reducers/timetables.ts Outdated Show resolved Hide resolved
Comment on lines +145 to +148
onSelectTheme={() => {
props.selectTheme(theme);
props.reassignAllModulesColor(theme.numOfColors);
}}
Copy link
Member

@zwliew zwliew Aug 11, 2023

Choose a reason for hiding this comment

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

This has the disadvantage of updating the UI twice because we are dispatching two actions here. To fix this, you could remove the reassignAllModulesColor action, and change the timetables reducer to react to the SELECT_THEME action.


return {
...state,
id: newTheme,
id: newTheme.id,
numOfColors: newTheme.numOfColors,
Copy link
Member

@zwliew zwliew Aug 11, 2023

Choose a reason for hiding this comment

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

Since this affects local storage schema, we should be more careful here. In the long run, I don't think we'll want to store numOfColors in ThemeState as it only contains user customizable data (i.e. current theme ID, should show title, orientation of timetable).

Since numOfColors is a static property of a theme, instead of storing it in Redux, we should store it in a predefined readonly map like the one that you've defined in themes.json. This has the added benefit of you no longer having to pass numOfColors around everywhere as you can just read it from the map.

That said, if/when we add user-defined themes, they should be stored in Redux, but likely in a different place instead of ThemeState.

@@ -148,6 +149,19 @@ function semColors(state: ColorMapping = defaultSemColorMap, action: Actions): C
}
}

function recolor(state: SemesterColorMap, numOfColors: number): SemesterColorMap {
Copy link
Member

Choose a reason for hiding this comment

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

Minor thought: This is a fine approach, but I wonder if it'd be better to remove this function and leave color indices as is. Instead, we could modulo the colorIndex when rendering colors. I doubt it makes any difference though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason I used an action to reassign the colors is that there are too many places where we have to modulo the colorIndex when rendering colors, which might make code changes hard to keep track of.

@zwliew
Copy link
Member

zwliew commented Aug 11, 2023

The implementation looks sound but I have a few concerns mainly regarding (1) storing numOfColors in Redux and (2) dispatching multiple actions successively. Please let me know what you think :)

@joeng03
Copy link
Contributor Author

joeng03 commented Aug 13, 2023

Thanks for taking time to review my PR :)

  1. Storing numOfColor in Redux is a temporary step as we will store all themes ( default and user defined) in a new Redux state, which uses an array of hexcodes to track all colors of a theme, then use an id in ThemeState to reference the current theme.

  2. I agree that dispatching multiple actions is suboptimal, and we can refactor the code to reassign the colorIndex in the selectTheme instead. However, since we no longer need numOfColors in the next step, it may be more efficient to move straight to the next step ( use an array of hexcodes instead of numOfColors to render colors of a theme) since we will no longer need the reassignAllColors action anymore.

Would it be better if we combine steps 1 and 2 in this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants