Skip to content

Commit bc438a9

Browse files
committed
[optimize] simplify AI codes
1 parent a5e8e6c commit bc438a9

6 files changed

Lines changed: 43 additions & 63 deletions

File tree

components/Activity/HeroCarousel.tsx

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { I18nContext } from '../../models/Translation';
88
import { LarkImage } from '../LarkImage';
99
import styles from './HeroCarousel.module.less';
1010

11-
export const HERO_CAROUSEL_ITEMS = 3;
12-
1311
const timestampOf = (value: unknown) => {
1412
if (typeof value === 'number') return value;
1513
if (typeof value === 'string') {
@@ -24,12 +22,12 @@ const timestampOf = (value: unknown) => {
2422
const formatDateLabel = (value: unknown, locale: string) => {
2523
const timestamp = timestampOf(value);
2624

27-
if (!timestamp) return '';
28-
29-
return new Intl.DateTimeFormat(locale, {
30-
month: 'short',
31-
day: 'numeric',
32-
}).format(timestamp);
25+
return !timestamp
26+
? ''
27+
: new Intl.DateTimeFormat(locale, {
28+
month: 'short',
29+
day: 'numeric',
30+
}).format(timestamp);
3331
};
3432

3533
const locationTextOf = ({ city, location }: Activity) =>
@@ -64,26 +62,20 @@ export const HeroCarousel: FC<{ activities: Activity[] }> = ({ activities }) =>
6462
}, []);
6563

6664
useEffect(() => {
67-
const syncDescriptionRows = () => {
68-
setDescriptionRows(window.innerWidth <= 767.98 ? 4 : 3);
69-
};
70-
65+
const syncDescriptionRows = () => setDescriptionRows(window.innerWidth <= 767.98 ? 4 : 3);
7166
syncDescriptionRows();
72-
window.addEventListener('resize', syncDescriptionRows);
7367

74-
return () => {
75-
window.removeEventListener('resize', syncDescriptionRows);
76-
};
77-
}, []);
68+
const observer = new ResizeObserver(syncDescriptionRows);
69+
observer.observe(document.body);
7870

79-
if (!activities.length) return null;
71+
return () => observer.disconnect();
72+
}, []);
8073

8174
return (
8275
<Container
8376
as="section"
8477
fluid
8578
className={`${styles.heroCarousel} position-relative`}
86-
aria-label={t('home_hackathon_top_bar_aria_label')}
8779
style={heroStyle}
8880
>
8981
<Carousel
@@ -96,16 +88,18 @@ export const HeroCarousel: FC<{ activities: Activity[] }> = ({ activities }) =>
9688
className={`${styles.carousel} h-100`}
9789
>
9890
{activities.map(activity => {
91+
const { id, type, name, host, startTime, cardImage } = activity;
92+
9993
const href = ActivityModel.getLink(activity);
100-
const hosts = ((activity.host as string[]) || []).slice(0, 2);
94+
const hosts = ((host as string[]) || []).slice(0, 2);
10195
const locationText = locationTextOf(activity);
102-
const dateText = formatDateLabel(activity.startTime, currentLanguage);
103-
const title = (activity.name as string) || t('activity');
96+
const dateText = formatDateLabel(startTime, currentLanguage);
97+
const title = (name as string) || t('activity');
10498
const description = descriptionOf(activity);
105-
const image = activity.cardImage || activity.image;
99+
const image = cardImage || activity.image;
106100

107101
return (
108-
<Carousel.Item key={activity.id as string} className={`${styles.item} h-100`}>
102+
<Carousel.Item key={id as string} className={`${styles.item} h-100`}>
109103
<Card
110104
className={`${styles.slideCard} h-100 rounded-0 border-0 bg-transparent text-white`}
111105
>
@@ -122,20 +116,19 @@ export const HeroCarousel: FC<{ activities: Activity[] }> = ({ activities }) =>
122116
style={infoBodyStyle}
123117
>
124118
<Stack direction="horizontal" gap={2} className="flex-wrap mb-3 mb-md-4">
125-
{(hosts.length
126-
? hosts
127-
: [(activity.type as string) || t('hackathon')]
128-
).map(item => (
129-
<Badge
130-
key={item}
131-
pill
132-
bg="info"
133-
text="dark"
134-
className="px-3 py-2 fw-semibold"
135-
>
136-
{item}
137-
</Badge>
138-
))}
119+
{(hosts.length ? hosts : [(type as string) || t('hackathon')]).map(
120+
item => (
121+
<Badge
122+
key={item}
123+
pill
124+
bg="info"
125+
text="dark"
126+
className="px-3 py-2 fw-semibold"
127+
>
128+
{item}
129+
</Badge>
130+
),
131+
)}
139132
{(dateText || t('event_duration')) && (
140133
<Badge pill bg="light" text="dark" className="px-3 py-2 fw-semibold">
141134
{dateText || t('event_duration')}
@@ -171,7 +164,7 @@ export const HeroCarousel: FC<{ activities: Activity[] }> = ({ activities }) =>
171164
variant="light"
172165
className={`${styles.actionButton} px-4 py-2 fw-semibold text-uppercase`}
173166
>
174-
{t('home_hackathon_top_bar_action')}
167+
{t('hackathon_register_now')}
175168
</Button>
176169
</Stack>
177170
</Card.Body>

models/Activity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class ActivityModel extends BiDataTable<Activity>() {
5555
link,
5656
database,
5757
}: Pick<Activity, 'id' | 'type' | 'alias' | 'link' | 'database'>) =>
58-
database ? `/${type?.toString().toLowerCase() || 'activity'}/${alias || id}` : link?.toString();
58+
database ? `/${type?.toString().toLowerCase() || 'activity'}/${alias || id}` : link + '';
5959

6060
extractFields({
6161
id,

pages/index.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { observer } from 'mobx-react';
2-
import { cache, compose, errorLogger } from 'next-ssr-middleware';
2+
import { GetStaticProps } from 'next';
33
import { FC, useContext } from 'react';
44
import { Card, Col, Row } from 'react-bootstrap';
55
import { renderToStaticMarkup } from 'react-dom/server';
66
import ReactTyped from 'react-typed-component';
7+
import { Day, Second } from 'web-utility';
78

89
import { HeroCarousel } from '../components/Activity/HeroCarousel';
910
import { PageHead } from '../components/Layout/PageHead';
@@ -15,10 +16,14 @@ interface HomePageProps {
1516
activities: Activity[];
1617
}
1718

18-
export const getServerSideProps = compose<{}, HomePageProps>(cache(), errorLogger, async () => {
19+
export const getStaticProps: GetStaticProps<HomePageProps> = async () => {
1920
const activities = await new ActivityModel().getList({}, 1, 3);
20-
return { props: JSON.parse(JSON.stringify({ activities })) };
21-
});
21+
22+
return {
23+
props: JSON.parse(JSON.stringify({ activities })),
24+
revalidate: Day / Second,
25+
};
26+
};
2227

2328
const HomePage: FC<HomePageProps> = observer(({ activities }) => {
2429
const { t } = useContext(I18nContext);
@@ -27,7 +32,7 @@ const HomePage: FC<HomePageProps> = observer(({ activities }) => {
2732
<>
2833
<PageHead />
2934

30-
<HeroCarousel activities={activities} />
35+
{activities[0] && <HeroCarousel activities={activities} />}
3136

3237
<section
3338
className={`flex-fill d-flex flex-column justify-content-center align-items-center bg-secondary bg-gradient text-dark bg-opacity-10 ${styles.main}`}

translation/en-US.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ export default {
3232
'We are organizing the March Open Source Bazaar, welcome to participate!',
3333
welcome_open_collaboration: 'Welcome to participate in',
3434
open_collaboration: 'Open Collaboration',
35-
home_hackathon_top_bar_aria_label: 'Labor AI Hackathon 2026 promotion',
36-
home_hackathon_top_bar_badge: 'Registration open',
37-
home_hackathon_top_bar_title: 'Labor AI Hackathon 2026 registration is open',
38-
home_hackathon_top_bar_description: '48 hours to build the future.',
39-
home_hackathon_top_bar_action: 'Register now',
40-
home_hackathon_top_bar_close: 'Close event promotion bar',
4135

4236
// Volunteer page
4337
volunteer: 'Volunteer',

translation/zh-CN.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ export default {
3131
we_are_organizing_bazaar: '我们正在筹办 3 月开源市集,欢迎参与!',
3232
welcome_open_collaboration: '欢迎参与',
3333
open_collaboration: '开放式协作',
34-
home_hackathon_top_bar_aria_label: 'Labor AI Hackathon 2026 活动宣传',
35-
home_hackathon_top_bar_badge: '活动招募中',
36-
home_hackathon_top_bar_title: '打工人の终极反叛',
37-
home_hackathon_top_bar_description: '48 小时,来这里创造未来',
38-
home_hackathon_top_bar_action: '立刻报名',
39-
home_hackathon_top_bar_close: '关闭活动宣传栏',
4034

4135
// Volunteer page
4236
volunteer: '志愿者',

translation/zh-TW.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ export default {
3131
we_are_organizing_bazaar: '我們正在籌辦 3 月開源市集,歡迎參與!',
3232
welcome_open_collaboration: '歡迎參與',
3333
open_collaboration: '開放式協作',
34-
home_hackathon_top_bar_aria_label: 'Labor AI Hackathon 2026 活動宣傳',
35-
home_hackathon_top_bar_badge: '活動招募中',
36-
home_hackathon_top_bar_title: 'Labor AI Hackathon 2026 報名開啟',
37-
home_hackathon_top_bar_description: '48 小時,來這裡創造未來',
38-
home_hackathon_top_bar_action: '立刻報名',
39-
home_hackathon_top_bar_close: '關閉活動宣傳列',
4034

4135
// Volunteer page
4236
volunteer: '志工',

0 commit comments

Comments
 (0)