-
Notifications
You must be signed in to change notification settings - Fork 13.5k
feat(calendar): create generic useCalendarList hook #39934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VedantGupta-DTU
wants to merge
1
commit into
RocketChat:develop
Choose a base branch
from
VedantGupta-DTU:feat/generic-useCalendarList-hook
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
apps/meteor/client/views/calendar/hooks/useCalendarList.ts
This file contains hidden or 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,31 @@ | ||
| import { useEndpoint } from '@rocket.chat/ui-contexts'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
|
|
||
| /** | ||
| * Generic hook to fetch calendar events for a given date. | ||
| * | ||
| * This hook calls the provider-agnostic `/v1/calendar-events.list` endpoint, | ||
| * so it works regardless of the calendar source (Outlook, Google, CalDAV, etc.). | ||
| * | ||
| * @param date - The date for which to fetch calendar events. | ||
| * @returns A React Query result containing the list of calendar events. | ||
| */ | ||
| export const useCalendarList = (date: Date) => { | ||
| const calendarData = useEndpoint('GET', '/v1/calendar-events.list'); | ||
|
|
||
| return useQuery({ | ||
| queryKey: ['calendar', 'list', date.toISOString()], | ||
|
|
||
| queryFn: async () => { | ||
| const { data } = await calendarData({ date: date.toISOString() }); | ||
| return data; | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Convenience wrapper that fetches calendar events for today's date. | ||
| */ | ||
| export const useCalendarListForToday = () => { | ||
| return useCalendarList(new Date()); | ||
| }; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unstable query key due to
new Date()called on every render.new Date()generates a different timestamp (including milliseconds) on each render, producing a differenttoISOString()value for the query key. This causes React Query to treat every render as a new query, leading to cache misses and potential excessive API calls.Memoize the date to ensure stability across renders:
🐛 Proposed fix using useMemo
Alternatively, if the date should update when the calendar day changes (e.g., app open past midnight), consider a more sophisticated approach that only changes when the calendar day itself changes.
📝 Committable suggestion
🤖 Prompt for AI Agents