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

refactor hardcoded layout strings #834

Closed
wants to merge 11 commits into from

Conversation

adriangohjw
Copy link
Contributor

Problem

Currently all layout are hardcoded as string throughout the app, across UI and Studio.

This makes it hard to debug/improve + more prone to error due to carelessness

Solution

Breaking Changes

  • Yes - this PR contains breaking changes
    • Details ...
  • No - this PR is backwards compatible

Features:

  • Details ...

Improvements:

Using a Single Source of Truth approach

  • import ISOMER_PAGE_LAYOUTS and use it whenever possible instead
  • create and import TAILWIND_SIMPLIFIED_LAYOUTS and use it whenever possible instead

@adriangohjw adriangohjw added the enhancement New feature or request label Oct 27, 2024
@adriangohjw adriangohjw self-assigned this Oct 27, 2024
@adriangohjw adriangohjw requested a review from a team as a code owner October 27, 2024 06:52
Copy link

vercel bot commented Oct 27, 2024

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

Name Status Preview Comments Updated (UTC)
isomer-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 11, 2024 2:42am

@adriangohjw adriangohjw changed the title Adriangohjw/refactor hardcoded layout strings refactor hardcoded layout strings Oct 27, 2024
@datadog-opengovsg
Copy link

datadog-opengovsg bot commented Oct 27, 2024

Datadog Report

Branch report: adriangohjw/refactor-hardcoded-layout-strings
Commit report: d06b8ff
Test service: isomer-studio

✅ 0 Failed, 167 Passed, 34 Skipped, 35.4s Total Time
➡️ Test Sessions change in coverage: 1 no change

@dcshzj
Copy link
Contributor

dcshzj commented Oct 27, 2024

This makes it hard to debug/improve + more prone to error due to carelessness

I'm a bit confused by this statement. I feel we should lean in to the inferred types from TypeScript, which can provide an early warning system specifically for such issues. The layout prop is inferred by TypeScript which allows us to specify things like "content" safely, as "conteent" would not match the allowed type and throw an error. This also allows us to only define the universe of layouts in a single place, instead of having potentially two sources of truth.

If there are places where this wasn't caught, we should fix the root cause which is that the types are not being defined correctly.

@adriangohjw
Copy link
Contributor Author

adriangohjw commented Oct 27, 2024

You have a point!

Before starteing this PR, I reflected on the challenges I had in the past weeks, esp. as a newcomer to the codebase. There's some area of improvements that slowed down my onboarding which I hope I can address to make things easier for others in the future.

The goal of this PR is to improve two key areas:

  1. Ease of maintenance – Making future changes more consistent and ensuring the same values are used throughout the codebase.
  2. Development speed – Especially for newcomers, reducing friction and confusion by providing more clarity and context.

Yes, we could address this with more documentation, but IMO documentation can sometimes cause more problems than it solves. This is esp. true if it isn’t regularly maintained, which will happen to us more often since the team is limited in time and manpower (also, keeping it up-to-date is more chore than anything)

Here are a few examples to illustrate this (Note: they are overlapping in some cases)

  • Searchability: Instead of hunting for a string like "content", it's much faster to search for a constant like ISOMER_PAGE_LAYOUTS.Content. This is especially useful when strings like "content" are ambiguous or reused in different contexts (e.g., as a link or file in Button components).
  • Clarity for newcomers: Sometimes, we have overlapping terms that might be confusing.
    • For instance, the term homepage could refer to either a layout or a Tailwind simplified layout. By using constants like ISOMER_PAGE_LAYOUTS.Homepage or TAILWIND_SIMPLIFIED_LAYOUTS.Homepage, we can eliminate this ambiguity.
  • Explicitness: In cases where values are used as keys (not just as strings), it’s clearer to write [TAILWIND_SIMPLIFIED_LAYOUTS.Default]: {...} instead of default: {...}. This makes it more explicit and easier to understand at a glance.

To be clear, this change will probably have minimal impact when it comes to enforcing type checks. Since both ISOMER_PAGE_LAYOUTS.Homepage and "homepage" are ultimately the same string value, it will unlikely trigger any new errors that didn’t already exist.

…ngohjw/refactor-hardcoded-layout-strings
Comment on lines +17 to +18
case "homepage":
case "notfound":
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: for standardisation should we also use ISOMER_PAGE_LAYOUTS 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 but... i have no idea why npm run build fails with a totally unrelated error message when i import that. Spend quite some time trying to debug that but couldn't figure out why too

tooling/build/scripts/publishing/index.ts Outdated Show resolved Hide resolved
Copy link

mergify bot commented Dec 11, 2024

This pull request has been stale for more than 30 days! Could someone please take a look at it @opengovsg/isomer-engineers

@mergify mergify bot requested a review from a team December 11, 2024 08:27
Copy link

mergify bot commented Jan 10, 2025

This pull request has been stale for more than 30 days! Could someone please take a look at it @opengovsg/isomer-engineers

@adriangohjw
Copy link
Contributor Author

too stale - will create it in another PR subsequently

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

Successfully merging this pull request may close these issues.

2 participants