Skip to content

Commit 1c7d4d8

Browse files
committed
refactor: simplify hackathon detail countdown
1 parent 8ed3e06 commit 1c7d4d8

1 file changed

Lines changed: 20 additions & 29 deletions

File tree

pages/hackathon/[id].tsx

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BiTableSchema, TableCellLocation, TableFormView } from 'mobx-lark';
1+
import { TableCellLocation, TableFormView } from 'mobx-lark';
22
import { observer } from 'mobx-react';
33
import { cache, compose, errorLogger } from 'next-ssr-middleware';
44
import { FC, useContext } from 'react';
@@ -60,6 +60,7 @@ import {
6060
isPublicForm,
6161
normalizeAgendaType,
6262
previewText,
63+
resolveCountdownState,
6364
} from '../../components/Activity/Hackathon/utility';
6465

6566
interface HackathonDetailProps {
@@ -81,7 +82,7 @@ export const getServerSideProps = compose<{ id: string }>(
8182
async ({ params }) => {
8283
const activity = await new ActivityModel().getOne(params!.id);
8384

84-
const { appId, tableIdMap } = (activity.databaseSchema || {}) as BiTableSchema;
85+
const { appId, tableIdMap } = activity.databaseSchema;
8586

8687
if (!appId || !tableIdMap) return { notFound: true, props: {} };
8788

@@ -110,18 +111,18 @@ const HackathonDetail: FC<HackathonDetailProps> = observer(({ activity, hackatho
110111
const i18n = useContext(I18nContext);
111112
const { t } = i18n;
112113
const {
113-
name,
114-
summary,
115-
location,
116-
startTime,
117-
endTime,
118-
databaseSchema,
119-
host,
120-
image,
121-
type: activityType,
122-
} = activity,
114+
name,
115+
summary,
116+
location,
117+
startTime,
118+
endTime,
119+
databaseSchema,
120+
host,
121+
image,
122+
type: activityType,
123+
} = activity,
123124
{ people, organizations, agenda, prizes, templates, projects } = hackathon;
124-
const { forms } = (databaseSchema || {}) as BiTableSchema;
125+
const { forms } = databaseSchema;
125126
const formMap = (forms || {}) as Partial<Record<FormGroupKey, TableFormView[]>>;
126127
const summaryText = (summary as string) || '';
127128
const agendaItems = [...agenda].sort(
@@ -184,22 +185,12 @@ const HackathonDetail: FC<HackathonDetailProps> = observer(({ activity, hackatho
184185
};
185186
})
186187
.filter(({ date, label }) => Boolean(date && label));
187-
const now = renderedAt;
188-
const nextAgendaItem = agendaItems.find(({ startedAt, endedAt }) => {
189-
const started = new Date((startedAt as string) || 0).getTime();
190-
const ended = new Date((endedAt as string) || 0).getTime();
191-
192-
return Number.isFinite(started) && Number.isFinite(ended) && now <= ended;
193-
});
194-
const nextAgendaStarted = nextAgendaItem?.startedAt as string | undefined;
195-
const nextAgendaEnded = nextAgendaItem?.endedAt as string | undefined;
196-
const countdownTo =
197-
(nextAgendaStarted && new Date(nextAgendaStarted).getTime() > now
198-
? nextAgendaStarted
199-
: nextAgendaEnded) ||
200-
((startTime as string | undefined) && new Date(startTime as string).getTime() > now
201-
? (startTime as string)
202-
: (endTime as string | undefined));
188+
const { nextItem: nextAgendaItem, countdownTo } = resolveCountdownState(
189+
agendaItems,
190+
renderedAt,
191+
startTime,
192+
endTime,
193+
);
203194
const countdownLabel = nextAgendaItem
204195
? agendaTypeLabelOf(nextAgendaItem.type, t, t('agenda'))
205196
: t('event_duration');

0 commit comments

Comments
 (0)