Skip to content

Commit bcba864

Browse files
committed
Extract getAllEvents to a new file
1 parent 1dd567d commit bcba864

File tree

2 files changed

+94
-84
lines changed

2 files changed

+94
-84
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { join } from "node:path"
2+
import { readFile } from "node:fs/promises"
3+
4+
import { meetups } from "@/components/meetups"
5+
6+
import type { WorkingGroupMeeting } from "@/../scripts/sync-working-groups/sync-working-groups"
7+
8+
import { events, type Event, type Meetup } from "./events"
9+
10+
type AnyEvent = Event | Meetup | WorkingGroupMeeting
11+
12+
const WORKING_GROUP_MEETINGS_FILE = join(
13+
process.cwd(),
14+
"scripts/sync-working-groups/working-group-events.ndjson",
15+
)
16+
17+
export async function getAllEvents() {
18+
const workingGroupMeetings = await loadWorkingGroupMeetings()
19+
20+
let {
21+
pastEvents,
22+
upcomingEvents,
23+
}: { pastEvents: AnyEvent[]; upcomingEvents: AnyEvent[] } = events.reduce(
24+
(acc, event) => {
25+
const now = new Date()
26+
const date = new Date(event.date)
27+
if (date < now) {
28+
acc.pastEvents.push(event)
29+
} else {
30+
acc.upcomingEvents.push(event)
31+
}
32+
return acc
33+
},
34+
{ pastEvents: [], upcomingEvents: [] } as {
35+
pastEvents: Event[]
36+
upcomingEvents: Event[]
37+
},
38+
)
39+
40+
const now = new Date()
41+
42+
for (const meeting of workingGroupMeetings) {
43+
if (meeting.start && new Date(meeting.start) < now) {
44+
pastEvents.push(meeting)
45+
} else upcomingEvents.push(meeting)
46+
}
47+
48+
for (const meetup of meetups) {
49+
const { next, prev } = meetup.node
50+
if (next && new Date(next) < now) {
51+
pastEvents.push(meetup)
52+
} else {
53+
upcomingEvents.push(meetup)
54+
pastEvents.push({
55+
...meetup,
56+
date: prev,
57+
})
58+
}
59+
}
60+
61+
const getDate = (event: AnyEvent) => {
62+
if ("date" in event) return new Date(event.date)
63+
if ("node" in event) return new Date(event.node.next || event.node.prev)
64+
return new Date(event.start)
65+
}
66+
67+
function sortByDate(a: AnyEvent, b: AnyEvent) {
68+
const aDate = getDate(a)
69+
const bDate = getDate(b)
70+
return bDate.getTime() - aDate.getTime()
71+
}
72+
73+
pastEvents = pastEvents.sort(sortByDate)
74+
upcomingEvents = upcomingEvents.sort(sortByDate)
75+
76+
return { pastEvents, upcomingEvents }
77+
}
78+
79+
async function loadWorkingGroupMeetings(): Promise<WorkingGroupMeeting[]> {
80+
try {
81+
const raw = (await readFile(WORKING_GROUP_MEETINGS_FILE, "utf8")).trim()
82+
if (!raw) return []
83+
return raw
84+
.split("\n")
85+
.filter(Boolean)
86+
.map(line => JSON.parse(line) as WorkingGroupMeeting)
87+
} catch (error) {
88+
console.error("Failed to read working group meetings", error)
89+
return []
90+
}
91+
}
Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import { readFile } from "node:fs/promises"
2-
import path from "node:path"
1+
import dynamic from "next/dynamic"
32

43
import { Breadcrumbs } from "@/_design-system/breadcrumbs"
5-
import { meetups } from "@/components/meetups"
64
import { Button } from "@/app/conf/_design-system/button"
75

86
import { MeetupsMap } from "./meetups-map"
97
import { EventsList } from "./events-list"
10-
import { events, type Event, type Meetup } from "./events"
118
import { BenefitsSection } from "./benefits-section"
129
import { GetYourMeetupNoticedSection } from "./get-your-meetup-noticed-section"
1310
import { BringGraphQLToYourCommunity } from "./bring-graphql-to-your-community"
14-
import type { WorkingGroupMeeting } from "../../../../../scripts/sync-working-groups/sync-working-groups"
15-
import dynamic from "next/dynamic"
16-
17-
type AnyEvent = Event | Meetup | WorkingGroupMeeting
11+
import { getAllEvents } from "./get-all-events"
1812

1913
const ISSUE_TEMPLATE_LINK =
2014
"https://github.com/graphql/community-wg/issues/new?assignees=&labels=event&template=event-submission.yml"
@@ -27,69 +21,8 @@ const GalleryStrip = dynamic(
2721
{ ssr: false },
2822
)
2923

30-
const WORKING_GROUP_MEETINGS_FILE = path.join(
31-
process.cwd(),
32-
"scripts/sync-working-groups/working-group-events.ndjson",
33-
)
34-
3524
export default async function EventsPage() {
36-
const workingGroupMeetings = await loadWorkingGroupMeetings()
37-
38-
let {
39-
pastEvents,
40-
upcomingEvents,
41-
}: { pastEvents: AnyEvent[]; upcomingEvents: AnyEvent[] } = events.reduce(
42-
(acc, event) => {
43-
const now = new Date()
44-
const date = new Date(event.date)
45-
if (date < now) {
46-
acc.pastEvents.push(event)
47-
} else {
48-
acc.upcomingEvents.push(event)
49-
}
50-
return acc
51-
},
52-
{ pastEvents: [], upcomingEvents: [] } as {
53-
pastEvents: Event[]
54-
upcomingEvents: Event[]
55-
},
56-
)
57-
58-
const now = new Date()
59-
60-
for (const meeting of workingGroupMeetings) {
61-
if (meeting.start && new Date(meeting.start) < now) {
62-
pastEvents.push(meeting)
63-
} else upcomingEvents.push(meeting)
64-
}
65-
66-
for (const meetup of meetups) {
67-
const { next, prev } = meetup.node
68-
if (next && new Date(next) < now) {
69-
pastEvents.push(meetup)
70-
} else {
71-
upcomingEvents.push(meetup)
72-
pastEvents.push({
73-
...meetup,
74-
date: prev,
75-
})
76-
}
77-
}
78-
79-
const getDate = (event: AnyEvent) => {
80-
if ("date" in event) return new Date(event.date)
81-
if ("node" in event) return new Date(event.node.next || event.node.prev)
82-
return new Date(event.start)
83-
}
84-
85-
function sortByDate(a: AnyEvent, b: AnyEvent) {
86-
const aDate = getDate(a)
87-
const bDate = getDate(b)
88-
return bDate.getTime() - aDate.getTime()
89-
}
90-
91-
pastEvents = pastEvents.sort(sortByDate)
92-
upcomingEvents = upcomingEvents.sort(sortByDate)
25+
const { upcomingEvents, pastEvents } = await getAllEvents()
9326

9427
return (
9528
<div className="gql-container">
@@ -169,17 +102,3 @@ export default async function EventsPage() {
169102
</div>
170103
)
171104
}
172-
173-
async function loadWorkingGroupMeetings(): Promise<WorkingGroupMeeting[]> {
174-
try {
175-
const raw = (await readFile(WORKING_GROUP_MEETINGS_FILE, "utf8")).trim()
176-
if (!raw) return []
177-
return raw
178-
.split("\n")
179-
.filter(Boolean)
180-
.map(line => JSON.parse(line) as WorkingGroupMeeting)
181-
} catch (error) {
182-
console.error("Failed to read working group meetings", error)
183-
return []
184-
}
185-
}

0 commit comments

Comments
 (0)