Skip to content

Commit 1d4fea5

Browse files
committed
Refactor And Fix Some Issue
- Refactor DotAnimation - Fix Shuttle to sync with timetable - Fix FullTime and Shuttle to account for simultaneous shuttle bus arrivals
1 parent 93fc657 commit 1d4fea5

File tree

5 files changed

+73
-52
lines changed

5 files changed

+73
-52
lines changed

src/app/components/fulltime/FullTime.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ const MinuteContainer = styled.div`
6767
${tw`self-center text-left ml-3 col-span-4`}
6868
`
6969

70-
const DirectMinuteContainer = styled(MinuteContainer)`
71-
${tw`hm:leading-none`}
72-
`
73-
7470
const FullTimeDocument = styled.div`
7571
${tw`px-5 font-Ptd text-center mx-auto select-none bg-theme-main text-theme-text max-w-7xl`}
7672
`
@@ -182,16 +178,7 @@ const TimeBox = (props: OrganizedTimetables) => {
182178
>
183179
{t('direct')}
184180
</Chip>
185-
<DirectMinuteContainer>
186-
{props.direct.map((time, idx) => {
187-
const isExist = props.directY.some((ytime) => time === ytime)
188-
return isExist ? null : (
189-
<React.Fragment key={idx}>
190-
<span>{time} </span>
191-
</React.Fragment>
192-
)
193-
})}
194-
</DirectMinuteContainer>
181+
<MinuteContainer>{props.direct.join(' ')}</MinuteContainer>
195182
</TimeBoxBodyGrid>
196183
</TimeBoxBody>
197184
</TimeBoxInner>

src/app/components/routemap/DotAnimation.tsx

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ const PingDot = styled.div<{ $on: boolean }>`
1414
${(props) => (props.$on ? tw`visible` : tw`invisible`)}
1515
`
1616

17+
/**
18+
* This function applies the animation flag to the flagArray based on the config.
19+
* As a result, it controls the animation by passing the boolean values of the flagArray to each element accordingly.
20+
* @param config is the configuration object containing the animation starting index (index) and route type (dotType)
21+
* @param flagArray is the current animation flag array
22+
* @returns RouteAnimationFlag : Updated animation flag array
23+
*
24+
* @example If flagArray is all false and config is {dotType: 'direct', index: 2},
25+
* it should originally set true for indices 2 and 3 in the 'direct' array of flagArray,
26+
* but since there is no index 3 in dotIndex in RouteVisual, it sets true for indices 2 and 4 and returns it.
27+
*/
28+
1729
export const applyDotAnimationFlag = (
1830
config: DotAnimationConfig,
1931
flagArray: RouteAnimationFlag,
@@ -29,7 +41,7 @@ export const applyDotAnimationFlag = (
2941
newFlagArray.direct[i >= 3 ? i + 1 : i] = true
3042
if (config.dotType === 'cycle') newFlagArray.cycle[i] = true
3143
if (config.dotType === 'yesulin')
32-
newFlagArray.yesulin[i === 2 ? i + 1 : i] = true
44+
newFlagArray.yesulin[i === 2 ? 3 : i] = true
3345
if (config.dotType === 'jungang') newFlagArray.jungang[i] = true
3446
}
3547
return newFlagArray
@@ -43,18 +55,29 @@ const getDotAniTimetableConfig = (type: string, index: number) => {
4355
else return { dotType: 'direct', index: index }
4456
}
4557

58+
/**
59+
* This function determines which route type and starting position the animation should use based on the tab and timetable type
60+
* @param tab is shuttle bus tabs such as 'shuttlecoke_o', 'subway'
61+
* @param currTimetable is current shuttle bus type and departure time from the timetable
62+
* @returns DotAnimationConfig[] : Array of configuration objects,
63+
* each containing animation starting index (index) and route type (dotType)
64+
*
65+
* @example If the tab is 'shuttlecoke_o' and the current timetable is {type: 'C', time: '~~:~~'},
66+
* then the function will return [{dotType: 'cycle', index: 1}]
67+
*/
68+
4669
export const getDotAnimationConfig = (
4770
tab: string,
48-
timetable: SingleShuttleSchedule,
71+
currTimetable: SingleShuttleSchedule,
4972
) => {
50-
if (timetable?.time !== '') {
73+
if (currTimetable !== undefined) {
5174
switch (tab) {
5275
case 'shuttlecoke_o':
53-
return [getDotAniTimetableConfig(timetable.type, 1)]
76+
return [getDotAniTimetableConfig(currTimetable.type, 1)]
5477
case 'subway':
55-
return [getDotAniTimetableConfig(timetable.type, 2)]
78+
return [getDotAniTimetableConfig(currTimetable.type, 2)]
5679
case 'yesulin':
57-
return [getDotAniTimetableConfig(timetable.type, 3)]
80+
return [getDotAniTimetableConfig(currTimetable.type, 3)]
5881
case 'jungang':
5982
return [
6083
{
@@ -63,7 +86,7 @@ export const getDotAnimationConfig = (
6386
},
6487
]
6588
case 'shuttlecoke_i':
66-
if (timetable.type === 'NA') return []
89+
if (currTimetable.type === 'NA') return []
6790

6891
return [
6992
{
@@ -80,50 +103,53 @@ export const getDotAnimationConfig = (
80103
},
81104
{
82105
dotType: 'yesulin',
83-
index: 3,
106+
index: 4,
84107
},
85108
]
86109
default:
87-
return [getDotAniTimetableConfig(timetable.type, 0)]
110+
return [getDotAniTimetableConfig(currTimetable.type, 0)]
88111
}
89112
}
90113
return []
91114
}
92115

93116
export const useDotAnimation = (tab: string) => {
94117
const [flagTable, setFlagTable] = React.useState<RouteAnimationFlag>({
95-
direct: [false, false, false, false, false],
118+
direct: [false, false, false, false, false, false],
96119
cycle: [false, false, false, false, false, false],
97-
yesulin: [false, false, false, false, false],
120+
yesulin: [false, false, false, false, false, false],
98121
jungang: [false, false, false, false, false, false],
99122
})
100-
const timetable = useTimeTableContext().timetable
101-
const checkTimetable = React.useRef<SingleShuttleSchedule>()
123+
const currTimetableArray = useTimeTableContext().currTimetable
124+
const checkCurrTimetable = React.useRef<SingleShuttleSchedule>()
102125

103126
React.useEffect(() => {
104-
checkTimetable.current = undefined
127+
checkCurrTimetable.current = undefined
105128
}, [])
106129

107130
useEffect(() => {
108131
let tempFlagTable = {
109-
direct: [false, false, false, false, false],
132+
direct: [false, false, false, false, false, false],
110133
cycle: [false, false, false, false, false, false],
111-
yesulin: [false, false, false, false, false],
134+
yesulin: [false, false, false, false, false, false],
112135
jungang: [false, false, false, false, false, false],
113136
}
114137
setFlagTable(tempFlagTable)
115-
if (checkTimetable.current === timetable) return
116138

117-
checkTimetable.current = timetable
139+
if (checkCurrTimetable.current === currTimetableArray[0]) return
140+
141+
checkCurrTimetable.current = currTimetableArray[0]
118142

119-
const dotAnimationConfig = getDotAnimationConfig(tab, timetable)
143+
currTimetableArray.map((currTimetable: SingleShuttleSchedule) => {
144+
const dotAnimationConfig = getDotAnimationConfig(tab, currTimetable)
120145

121-
dotAnimationConfig.forEach((config) => {
122-
tempFlagTable = applyDotAnimationFlag(config, tempFlagTable)
146+
dotAnimationConfig.forEach((config) => {
147+
tempFlagTable = applyDotAnimationFlag(config, tempFlagTable)
148+
})
123149
})
124150

125151
setFlagTable(tempFlagTable)
126-
}, [timetable, tab])
152+
}, [currTimetableArray, tab])
127153

128154
return flagTable
129155
}

src/app/components/routemap/RouteVisual.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import DotAnimation, {
88
} from '@/components/routemap/DotAnimation'
99
import { RouteAnimationFlag } from '@/data'
1010

11-
const RouteLine = styled.div<{ isHalfWidth: boolean }>`
11+
const RouteLine = styled.div<{ $ishalfwidth: boolean }>`
1212
${tw`absolute transition duration-150 ease-in-out z-0 h-[0.2rem] top-1 rt1:top-[0.2rem] rt1:h-[0.16rem] left-[0.6rem] max-w-[13.125rem]`}
13-
${(props) => (props.isHalfWidth ? tw`w-[7.8vw]` : tw`w-[15.6vw]`)}
13+
${(props) => (props.$ishalfwidth ? tw`w-[7.8vw]` : tw`w-[15.6vw]`)}
1414
`
1515

1616
const Dot = styled.span`
@@ -86,7 +86,7 @@ const AddLines = (props: {
8686
const index = lineIndex[props.rootStatus][props.index]
8787
return (
8888
<RouteLine
89-
isHalfWidth={
89+
$ishalfwidth={
9090
props.rootStatus !== 'direct' && (index === 2 || index === 3)
9191
}
9292
className={

src/app/components/shuttle/Shuttle.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export const Shuttle = ({ location }: ShuttleStop) => {
350350
staleTime: 5 * 60 * 1000,
351351
})
352352

353-
const { setTimetable } = useTimeTableContext()
353+
const { currTimetable, setCurrTimetable } = useTimeTableContext()
354354

355355
const [season, week] =
356356
setting.data !== undefined ? getSeason(setting.data) : [null, null]
@@ -393,16 +393,13 @@ export const Shuttle = ({ location }: ShuttleStop) => {
393393
if (
394394
timetable.data?.length === 0 ||
395395
timetable.status !== 'success' ||
396-
filtered === undefined ||
397-
filtered.length === 0
396+
filtered?.length === 0
398397
) {
399-
setTimetable({ time: '', type: 'NA' })
400398
setTimetableAlive(false)
401399
} else {
402-
setTimetable(filtered[0])
403400
setTimetableAlive(true)
404401
}
405-
}, [timetable.data, timetable.status, setTimetable, currentTime])
402+
}, [timetable.data, timetable.status])
406403

407404
// Set week and season to localStorage
408405
useEffect(() => {
@@ -485,6 +482,13 @@ export const Shuttle = ({ location }: ShuttleStop) => {
485482
}
486483

487484
const filtered = timetable.data.filter((val) => isAfterCurrentTime(val))
485+
486+
if (filtered[0] !== currTimetable[0]) {
487+
if (filtered.length >= 2 && filtered[0].time === filtered[1].time)
488+
setCurrTimetable([filtered[0], filtered[1]])
489+
else setCurrTimetable([filtered[0]])
490+
}
491+
488492
const reverted = filtered.map((val) => convertUnixToTime(val))
489493
if (filtered.length === 0) {
490494
// Buses are done for today. User should refresh after midnight.

src/app/context/TimeTableContext.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import React, { useState } from 'react'
33
import { SingleShuttleSchedule } from '@/data'
44

55
interface TimeTableContextProps {
6-
timetable: SingleShuttleSchedule
7-
setTimetable: React.Dispatch<React.SetStateAction<SingleShuttleSchedule>>
6+
currTimetable: Array<SingleShuttleSchedule>
7+
setCurrTimetable: React.Dispatch<
8+
React.SetStateAction<Array<SingleShuttleSchedule>>
9+
>
810
}
911

1012
const TimeTableContext = React.createContext<TimeTableContextProps | null>(null)
@@ -18,12 +20,14 @@ export const useTimeTableContext = () => {
1820
export const TimeTableContextProvider = ({
1921
children,
2022
}: React.PropsWithChildren) => {
21-
const [timetable, setTimetable] = useState<SingleShuttleSchedule>({
22-
time: '',
23-
type: 'NA',
24-
})
23+
const [currTimetable, setCurrTimetable] = useState<
24+
Array<SingleShuttleSchedule>
25+
>([])
2526

26-
const value = React.useMemo(() => ({ timetable, setTimetable }), [timetable])
27+
const value = React.useMemo(
28+
() => ({ currTimetable, setCurrTimetable }),
29+
[currTimetable],
30+
)
2731

2832
return (
2933
<TimeTableContext.Provider value={value}>

0 commit comments

Comments
 (0)