-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1280 from flmel/feat/events-page
feat: add events page
- Loading branch information
Showing
10 changed files
with
617 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
|
||
import { | ||
devhubCommunityCalendarId, | ||
lumaDevHubHacksCalendarId, | ||
lumaNearAICalendarId, | ||
lumaNearCalendarId, | ||
lumaNearHZNCalendarId, | ||
nearTownHallCalendarId, | ||
} from '@/utils/config'; | ||
import { formatEvents, sortEventsByDate } from '@/utils/events'; | ||
import type { FormatedEvent } from '@/utils/types'; | ||
|
||
import { useGoogleEvents } from './useGoogleEvents'; | ||
import { useLumaEvents } from './useLumaEvents'; | ||
|
||
const LIMIT = 6; | ||
|
||
export function useEvents() { | ||
const { lumaEvents: mainEvents } = useLumaEvents([lumaNearCalendarId, lumaDevHubHacksCalendarId], 1); | ||
const { lumaEvents: hackatons } = useLumaEvents([lumaDevHubHacksCalendarId], 12); | ||
const { lumaEvents: otherLumaEvents } = useLumaEvents( | ||
[lumaNearAICalendarId, lumaNearHZNCalendarId, nearTownHallCalendarId], | ||
12, | ||
); | ||
const { googleEvents } = useGoogleEvents(devhubCommunityCalendarId, 100); | ||
|
||
const [lastElements, setLastElements] = useState<FormatedEvent[]>([]); | ||
const [communityEvents, setCommunityEvents] = useState<FormatedEvent[]>([]); | ||
const [allEvents, setAllEvents] = useState<FormatedEvent[]>([]); | ||
|
||
const hasMoreEvents = useMemo( | ||
() => communityEvents.length + lastElements.length < allEvents.length, | ||
[communityEvents, lastElements, allEvents], | ||
); | ||
|
||
const fetchMore = useCallback(() => { | ||
const skipFrom = communityEvents.length + lastElements.length; | ||
const skipTo = skipFrom + LIMIT; | ||
const nextEvents = allEvents.slice(skipFrom, skipTo); | ||
|
||
setCommunityEvents([...communityEvents, ...lastElements]); | ||
setLastElements(nextEvents); | ||
}, [allEvents, communityEvents, lastElements]); | ||
|
||
useEffect(() => { | ||
const sortedEvents = sortEventsByDate([...googleEvents, ...otherLumaEvents]); | ||
const formattedEvents = formatEvents(sortedEvents); | ||
setAllEvents(formattedEvents); | ||
setCommunityEvents(formattedEvents.slice(0, LIMIT)); | ||
setLastElements([]); | ||
}, [googleEvents, otherLumaEvents]); | ||
|
||
const highlightedEvent = mainEvents[0]; | ||
|
||
return { | ||
highlightedEvent, | ||
communityEvents, | ||
hasMoreEvents, | ||
fetchMore, | ||
lastElements, | ||
hackatons, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
import { fetchGoogleCalendarEvents } from '@/utils/events'; | ||
import type { FormatedEvent } from '@/utils/types'; | ||
|
||
export function useGoogleEvents(calendarId: string, limit = 9) { | ||
const [googleEvents, setGoogleEvents] = useState<FormatedEvent[]>([]); | ||
|
||
const startFrom = useMemo(() => { | ||
return new Date().toISOString(); | ||
}, []); | ||
|
||
const loadData = async () => { | ||
try { | ||
const { items } = await fetchGoogleCalendarEvents(calendarId, startFrom, limit, ''); | ||
|
||
const formattedEvents = items.map((event) => { | ||
return { | ||
id: event.id, | ||
title: event.summary, | ||
start: event.start.dateTime, | ||
thumbnail: `https://lh3.googleusercontent.com/d/${event.attachments?.[0]?.fileId}=w1000`, | ||
location: '', | ||
url: event.htmlLink, | ||
}; | ||
}); | ||
|
||
setGoogleEvents(formattedEvents); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
loadData(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return { googleEvents }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { fetchLumaEvents } from '@/utils/events'; | ||
import type { FormatedEvent } from '@/utils/types'; | ||
|
||
export function useLumaEvents(calendarApiIds: string[], limit = 3) { | ||
const aiEventsUrl = 'https://lu.ma'; | ||
const [lumaEvents, setLumaEvents] = useState<FormatedEvent[]>([]); | ||
const [hasMoreEvents, setHasMoreEvents] = useState(false); | ||
|
||
useEffect(() => { | ||
const loadData = async () => { | ||
try { | ||
const allEvents = await Promise.all(calendarApiIds.map((id) => fetchLumaEvents(id, limit, 0))); | ||
|
||
const combinedEvents = allEvents.flatMap((result) => result.entries); | ||
|
||
const sortedEvents = [...combinedEvents] | ||
.sort((a, b) => new Date(a.event.start_at).getTime() - new Date(b.event.start_at).getTime()) | ||
.slice(0, limit); | ||
|
||
const formattedEvents = sortedEvents.map((item) => { | ||
return { | ||
id: item.event.api_id, | ||
start: item.event.start_at, | ||
location: formatLocation(item.event.geo_address_json ?? item.event.geo_address_info ?? ''), | ||
thumbnail: item.event.cover_url, | ||
title: item.event.name, | ||
url: `${aiEventsUrl}/${item.event.url}`, | ||
}; | ||
}); | ||
|
||
setLumaEvents(formattedEvents); | ||
setHasMoreEvents(combinedEvents.length > limit); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
loadData(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [JSON.stringify(calendarApiIds), limit]); | ||
|
||
const formatLocation = (location: any) => { | ||
if (location.city || location.city_state) { | ||
return `${location.city ?? location.city_state}, ${location.country}`; | ||
// Luma API returns "mode" as "obfuscated" when the address is hidden | ||
} else if (location && location?.mode === 'obfuscated') { | ||
return 'Register to See Address'; | ||
} | ||
return location.address; | ||
}; | ||
|
||
return { | ||
lumaEvents, | ||
hasMoreEvents, | ||
}; | ||
} |
Oops, something went wrong.