-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
base: master
Are you sure you want to change the base?
Changes from all commits
9147c13
ba90d9f
9cba663
76153c5
c297822
1c99c74
13e3d78
38e3b0e
eed21ab
ad26753
716d217
cff7db2
e008c01
0525951
3118f95
6371971
7ba93e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,159 @@ | ||
import { useContext, useMemo } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { Box, SxProps, Theme, Typography } from "@mui/material"; | ||
import { toProperCase } from "../../utils"; | ||
import { RouteListEntry } from "hk-bus-eta"; | ||
import useLanguage from "../../hooks/useTranslation"; | ||
import DbContext from "../../context/DbContext"; | ||
|
||
interface RouteTerminus { | ||
terminus: RouteListEntry; | ||
} | ||
|
||
const RouteTerminus = ({ terminus }: RouteTerminus) => { | ||
const { t } = useTranslation(); | ||
const language = useLanguage(); | ||
const { t, i18n } = useTranslation(); | ||
const { | ||
db: { routeList, stopList }, | ||
} = useContext(DbContext); | ||
const { route, co, orig, dest, stops, bound, gtfsId } = terminus; | ||
|
||
const firstLastDiff = (arr: string[]) => { | ||
if (arr.length < 2) return arr; | ||
return [arr[0], arr[arr.length - 1]]; | ||
}; | ||
const diffConsecutive = (array: string[], sequence: string[]) => { | ||
for (let i = 0; i <= array.length - sequence.length; i++) { | ||
let j; | ||
for (j = 0; j < sequence.length; j++) { | ||
if (array[i + j] !== sequence[j]) { | ||
break; | ||
} | ||
} | ||
if (j === sequence.length) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
let routeRemark = useMemo(() => { | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address the issue where 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 ToolsGitHub Check: build (21.x)
GitHub Check: build (20.x)
|
||
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, | ||
]); | ||
Comment on lines
+38
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The memoization of 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
|
||
|
||
return ( | ||
<Box sx={rootSx}> | ||
<Box sx={fromToWrapperSx}> | ||
<span>{`${t("往")} `}</span> | ||
<Typography component="h3" variant="h6" sx={destinationSx}> | ||
{toProperCase(terminus.dest[language])} | ||
<Typography | ||
component="h3" | ||
variant="h6" | ||
sx={destinationSx} | ||
textOverflow="ellipsis" | ||
overflow="hidden" | ||
> | ||
{toProperCase(terminus.dest[i18n.language])} | ||
</Typography> | ||
</Box> | ||
<Box sx={fromWrapperSx}> | ||
<Typography variant="body2"> | ||
{toProperCase(terminus.orig[language])} | ||
<Typography variant="body2" textOverflow="ellipsis" overflow="hidden"> | ||
{routeRemark} | ||
</Typography> | ||
</Box> | ||
</Box> | ||
|
There was a problem hiding this comment.
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.
Ensure that the code does not throw runtime errors when the language key is not found in the
dest
ororig
objects.Also applies to: 60-60, 70-70, 73-73