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

Better Route Search Remark #182

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

Conversation

LOOHP
Copy link
Contributor

@LOOHP LOOHP commented May 15, 2024

Changes the "route remark" underneath each route in the search screen from the first stop to the difference between itself and the main line.

Continuation of #142.

Summary by CodeRabbit

  • New Features

    • Enhanced multi-language support with updated translations for key phrases.
    • Improved language-specific rendering in route headers and destination/origin text components.
  • Refactor

    • Optimized route remark calculation with memoization for better performance.
  • Bug Fixes

    • Corrected language handling for better accuracy in multi-language displays.
    • Adjusted translations for consistency and readability.

@LOOHP LOOHP mentioned this pull request May 15, 2024
@thomas930
Copy link
Contributor

It's great if screenshot can be shown.

@chunlaw
Copy link
Member

chunlaw commented May 20, 2024

image

Some routes are now missing the origin, would it be better to add them back?

@LOOHP
Copy link
Contributor Author

LOOHP commented May 21, 2024

The idea of this change is to only display differences between special routes with their main routes. Since the main route doesn't have any differences with itself, there is no remark.
It could be changed to fallback to displaying the origin if there are no remarks if it is decided that that is better.

@chunlaw
Copy link
Member

chunlaw commented May 29, 2024

The idea of this change is to only display differences between special routes with their main routes. Since the main route doesn't have any differences with itself, there is no remark. It could be changed to fallback to displaying the origin if there are no remarks if it is decided that that is better.

Might you please apply the fallback?

Copy link

coderabbitai bot commented Jun 15, 2024

Warning

Rate limit exceeded

@github-actions[bot] has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 41 minutes and 18 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 0525951 and 7ba93e9.

Walkthrough

The recent updates focus on enhancing internationalization and optimizing component logic within specified files. Key changes include incorporating the i18n object from the useTranslation hook for language-specific rendering, introducing memoization for efficiency, and refining logic in text components for context-specific language handling.

Changes

Files Change Summaries
.../route-board/RouteTerminus.tsx Imported useContext, updated useTranslation with i18n, added DbContext, introduced memoization, refactored routeRemark logic, updated text rendering logic.
.../route-eta/RouteHeader.tsx Included i18n in useTranslation, modified text rendering in Typography for language-specific output.
src/i18n/translation.js Adjusted translations and added new phrases for better localization.
src/utils.ts Introduced langSpace function for language-aware spacing.

Poem

🐇
Routes now speak in many tongues,
Translating for all, where it belongs.
With memos here and spaces there,
A journey smooth, handled with care.
🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Outside diff range and nitpick comments (4)
src/utils.ts (4)

14-16: Consider adding a comment to explain the purpose of langSpace.

Although the function is simple, a brief comment explaining its purpose (returning a space character for English language settings) could improve code readability and maintainability.


Line range hint 55-59: Replace isFinite with Number.isFinite for type safety.

- if (typeof position.lat === "number" && isFinite(position.lat) && typeof position.lng === "number" && isFinite(position.lng))
+ if (typeof position.lat === "number" && Number.isFinite(position.lat) && typeof position.lng === "number" && Number.isFinite(position.lng))

This change ensures that the type coercion pitfalls of isFinite are avoided, which enhances the type safety of the function.


Line range hint 310-311: Use the exponentiation operator instead of Math.pow.

- return Math.floor(((lon + 180) / 360) * Math.pow(2, zoom));
+ return Math.floor(((lon + 180) / 360) * (2 ** zoom));

The exponentiation operator (**) is more modern and concise than Math.pow, and it's generally recommended for better readability and performance.


Line range hint 369-370: This else clause can be omitted for clarity.

- } else {
-   // Chrome
- }

Removing this empty else clause will clean up the code and reduce unnecessary lines.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 62ce1d2 and 13e3d78.

Files selected for processing (4)
  • src/components/route-board/RouteTerminus.tsx (1 hunks)
  • src/components/route-eta/RouteHeader.tsx (3 hunks)
  • src/i18n/translation.js (1 hunks)
  • src/utils.ts (1 hunks)
Additional context used
GitHub Check: build (21.x)
src/components/route-board/RouteTerminus.tsx

[failure] 54-54:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Terminal'.


[failure] 60-60:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Terminal'.


[failure] 63-63:
Type 'Company[]' cannot be used as an index type.


[failure] 65-65:
Type 'Company[]' cannot be used as an index type.


[failure] 65-65:
Type 'Company[]' cannot be used as an index type.


[failure] 66-66:
Type 'Company[]' cannot be used as an index type.


[failure] 67-67:
Type 'Company[]' cannot be used as an index type.


[failure] 67-67:
Type 'Company[]' cannot be used as an index type.


[failure] 70-70:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ en: string; zh: string; }'.


[failure] 73-73:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ en: string; zh: string; }'.

GitHub Check: build (20.x)
src/components/route-board/RouteTerminus.tsx

[failure] 54-54:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Terminal'.


[failure] 60-60:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Terminal'.


[failure] 63-63:
Type 'Company[]' cannot be used as an index type.


[failure] 65-65:
Type 'Company[]' cannot be used as an index type.


[failure] 65-65:
Type 'Company[]' cannot be used as an index type.


[failure] 66-66:
Type 'Company[]' cannot be used as an index type.


[failure] 67-67:
Type 'Company[]' cannot be used as an index type.


[failure] 67-67:
Type 'Company[]' cannot be used as an index type.


[failure] 70-70:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ en: string; zh: string; }'.


[failure] 73-73:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ en: string; zh: string; }'.

Biome
src/utils.ts

[error] 61-65: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 80-91: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 104-107: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 123-130: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 300-301: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 369-370: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 392-393: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 401-403: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 409-410: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 426-430: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 55-56: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead. (lint/suspicious/noGlobalIsFinite)

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.


[error] 58-59: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead. (lint/suspicious/noGlobalIsFinite)

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.


[error] 310-311: Use the '**' operator instead of 'Math.pow'. (lint/style/useExponentiationOperator)

Unsafe fix: Use the '**' operator instead of 'Math.pow'.


[error] 323-324: Use the '**' operator instead of 'Math.pow'. (lint/style/useExponentiationOperator)

Unsafe fix: Use the '**' operator instead of 'Math.pow'.

Additional comments not posted (3)
src/components/route-eta/RouteHeader.tsx (1)

18-18: Ensure useTranslation is properly utilized with both t and i18n being used as intended.

src/components/route-board/RouteTerminus.tsx (1)

13-13: Ensure useTranslation is properly utilized with both t and i18n being used as intended.

src/i18n/translation.js (1)

33-39: Ensure consistency in translation strings.

src/components/route-eta/RouteHeader.tsx Outdated Show resolved Hide resolved
remark = t("從") + orig[i18n.language] + t("開出");
}
} else {
let mainRouteFirstStop = stopList[data.stops[co][0]].name;
Copy link

Choose a reason for hiding this comment

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

Address the issue where Company[] type cannot be used as an index type.

Consider using a different approach to index or retrieve data from these arrays, as direct indexing with a type that is not supported will lead to runtime errors.

Also applies to: 65-65, 66-66, 67-67

Tools
GitHub Check: build (21.x)

[failure] 63-63:
Type 'Company[]' cannot be used as an index type.

GitHub Check: build (20.x)

[failure] 63-63:
Type 'Company[]' cannot be used as an index type.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 13e3d78 and 0525951.

Files selected for processing (2)
  • src/components/route-board/RouteTerminus.tsx (1 hunks)
  • src/components/route-eta/RouteHeader.tsx (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/components/route-eta/RouteHeader.tsx
Additional comments not posted (4)
src/components/route-board/RouteTerminus.tsx (4)

1-1: Import statements have been updated to include DbContext.

Ensure that DbContext is properly set up in the context folder and is providing the necessary data.

Also applies to: 6-6


13-13: Integration of the useTranslation hook and DbContext looks correct.

Verify that all necessary translations and database contexts are correctly configured and accessible.

Also applies to: 16-16


144-150: The Typography component is properly configured to handle text overflow.

This ensures that long destination names are gracefully truncated.


155-156: Good use of Typography for displaying routeRemark.

This maintains consistency in the UI and improves readability.

stopList[data.stops[co[0]][data.stops[co[0]].length - 1]].name
.zh !== stopList[stops[co[0]][stops[co[0]].length - 1]].name.zh
) {
remark = t("開往") + dest[i18n.language];
Copy link

Choose a reason for hiding this comment

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

Use optional chaining to safely access properties.

- remark = t("開往") + dest[i18n.language];
- remark = t("從") + orig[i18n.language] + t("開出");
+ remark = t("開往") + dest?.[i18n.language];
+ remark = t("從") + orig?.[i18n.language] + t("開出");

Ensure that the code does not throw runtime errors when the language key is not found in the dest or orig objects.

Also applies to: 60-60, 70-70, 73-73

Comment on lines +38 to +138
let remark = "";
if (Number(terminus.serviceType) >= 2) {
for (let [, data] of Object.entries(routeList)) {
if (
Number(data.serviceType) === 1 &&
route === data.route &&
JSON.stringify(bound) === JSON.stringify(data.bound) &&
(co[0] === "gmb"
? gtfsId === data.gtfsId
: JSON.stringify(co) === JSON.stringify(data.co))
) {
if (
stopList[data.stops[co[0]][data.stops[co[0]].length - 1]].name
.zh !== stopList[stops[co[0]][stops[co[0]].length - 1]].name.zh
) {
remark = t("開往") + dest[i18n.language];
} else if (
stopList[data.stops[co[0]][0]].name.zh !==
stopList[stops[co[0]][0]].name.zh
) {
if (!terminus.nlbId) {
remark = t("從") + orig[i18n.language] + t("開出");
}
} else {
let mainRouteFirstStop = stopList[data.stops[co][0]].name;
let mainRouteLastStop =
stopList[data.stops[co[0]][data.stops[co[0]].length - 1]].name;
let routeFirstStop = stopList[stops[co[0]][0]].name;
let routeLastStop =
stopList[stops[co[0]][stops[co[0]].length - 1]].name;

if (mainRouteLastStop.zh !== routeLastStop.zh) {
remark = t("開往") + routeLastStop[i18n.language];
} else if (mainRouteFirstStop.zh !== routeFirstStop.zh) {
if (!terminus.nlbId) {
remark = t("從") + routeFirstStop[i18n.language] + t("開出");
}
} else {
let difference = stops[co[0]].filter(
(x) => !data.stops[co[0]].includes(x)
);
let diffName = difference.map(
(x) => stopList[x].name[i18n.language]
);
if (difference.length > 0) {
remark =
t("經") +
firstLastDiff(diffName).join(
t(
diffConsecutive(data.stops[co[0]], difference)
? "至"
: "及"
)
);
} else {
let difference = data.stops[co[0]].filter(
(x) => !stops[co[0]].includes(x)
);
let diffName = difference.map(
(x) => stopList[x].name[i18n.language]
);
if (difference.length > 0) {
remark =
t("不經") +
firstLastDiff(diffName).join(
t(
diffConsecutive(data.stops[co[0]], difference)
? "至"
: "及"
)
);
}
}
}
}
break;
}
}
}
remark =
t("從") +
toProperCase(terminus.orig[i18n.language]) +
t("開出") +
" " +
remark;
return remark;
}, [
terminus,
routeList,
i18n.language,
bound,
co,
dest,
gtfsId,
orig,
route,
stopList,
stops,
t,
]);
Copy link

Choose a reason for hiding this comment

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

The memoization of routeRemark is implemented to enhance performance.

However, the complexity of this function is quite high due to multiple nested conditions and loops. Consider refactoring to simplify logic and improve readability.

- let remark = "";
- // existing complex logic
+ const remark = generateRemark(terminus, routeList, stopList, co, i18n);

Create a separate function generateRemark to handle the remark generation logic.

Committable suggestion was skipped due to low confidence.

@LOOHP
Copy link
Contributor Author

LOOHP commented Jun 15, 2024

I've added the fallback and it works in testing. However, the code standard is getting more strict each time. Looks like I'll have to spend more time figuring it out. If anyone could help to sort out the code formatting that would be nice.

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.

3 participants