-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Outlook threads API #511
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
Outlook threads API #511
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,51 +7,73 @@ import { List } from "@/components/email-list/EmailList"; | |
| import { LoadingContent } from "@/components/LoadingContent"; | ||
| import type { ThreadsQuery } from "@/app/api/google/threads/validation"; | ||
| import type { ThreadsResponse } from "@/app/api/google/threads/controller"; | ||
| import type { OutlookThreadsResponse } from "@/app/api/outlook/threads/controller"; | ||
| import { refetchEmailListAtom } from "@/store/email"; | ||
| import { BetaBanner } from "@/app/(app)/[emailAccountId]/mail/BetaBanner"; | ||
| import { ClientOnly } from "@/components/ClientOnly"; | ||
| import { PermissionsCheck } from "@/app/(app)/[emailAccountId]/PermissionsCheck"; | ||
|
|
||
| // You may get this from props, context, or user/account info | ||
| // For this example, let's assume it's a prop: | ||
| export default function Mail(props: { | ||
| searchParams: Promise<{ type?: string; labelId?: string }>; | ||
| searchParams: Promise<{ | ||
| type?: string; | ||
| labelId?: string; | ||
| folderId?: string; | ||
| provider?: "gmail" | "outlook"; | ||
| }>; | ||
| }) { | ||
| const searchParams = use(props.searchParams); | ||
| const query: ThreadsQuery = {}; | ||
| const provider = searchParams.provider || "gmail"; // default to gmail if not set | ||
|
|
||
| // Handle different query params | ||
| if (searchParams.type === "label" && searchParams.labelId) { | ||
| query.labelId = searchParams.labelId; | ||
| } else if (searchParams.type) { | ||
| query.type = searchParams.type; | ||
| // Build the query object | ||
| const query: ThreadsQuery = {}; | ||
| if (provider === "gmail") { | ||
| if (searchParams.type === "label" && searchParams.labelId) { | ||
| query.labelId = searchParams.labelId; | ||
| } else if (searchParams.type) { | ||
| query.type = searchParams.type; | ||
| } | ||
| } else if (provider === "outlook") { | ||
| if (searchParams.type === "folder" && searchParams.folderId) { | ||
| query.folderId = searchParams.folderId; | ||
| } else if (searchParams.type) { | ||
| query.type = searchParams.type; | ||
| } | ||
| } | ||
|
Comment on lines
+29
to
43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type mismatch: re-using Gmail
🤖 Prompt for AI Agents |
||
|
|
||
| // Build the correct endpoint | ||
| const endpoint = | ||
| provider === "gmail" ? "/api/google/threads" : "/api/outlook/threads"; | ||
|
|
||
| // SWR key builder | ||
| const getKey = ( | ||
| pageIndex: number, | ||
| previousPageData: ThreadsResponse | null, | ||
| previousPageData: ThreadsResponse | OutlookThreadsResponse | null, | ||
| ) => { | ||
| if (previousPageData && !previousPageData.nextPageToken) return null; | ||
| const queryParams = new URLSearchParams(query as Record<string, string>); | ||
| // Append nextPageToken for subsequent pages | ||
| if (pageIndex > 0 && previousPageData?.nextPageToken) { | ||
| queryParams.set("nextPageToken", previousPageData.nextPageToken); | ||
| } | ||
| return `/api/google/threads?${queryParams.toString()}`; | ||
| return `${endpoint}?${queryParams.toString()}`; | ||
| }; | ||
|
|
||
| const { data, size, setSize, isLoading, error, mutate } = | ||
| useSWRInfinite<ThreadsResponse>(getKey, { | ||
| keepPreviousData: true, | ||
| dedupingInterval: 1_000, | ||
| revalidateOnFocus: false, | ||
| }); | ||
| // Use correct response type for SWR | ||
| const { data, size, setSize, isLoading, error, mutate } = useSWRInfinite< | ||
| ThreadsResponse | OutlookThreadsResponse | ||
| >(getKey, { | ||
| keepPreviousData: true, | ||
| dedupingInterval: 1_000, | ||
| revalidateOnFocus: false, | ||
| }); | ||
|
|
||
| const allThreads = data ? data.flatMap((page) => page.threads) : []; | ||
| const isLoadingMore = | ||
| isLoading || (size > 0 && data && typeof data[size - 1] === "undefined"); | ||
| const showLoadMore = data ? !!data[data.length - 1]?.nextPageToken : false; | ||
|
|
||
| // store `refetch` in the atom so we can refresh the list upon archive via command k | ||
| // TODO is this the best way to do this? | ||
| const refetch = useCallback( | ||
| (options?: { removedThreadIds?: string[] }) => { | ||
| mutate( | ||
|
|
@@ -76,7 +98,6 @@ export default function Mail(props: { | |
| [mutate], | ||
| ); | ||
|
|
||
| // Set up the refetch function in the atom store | ||
| const setRefetchEmailList = useSetAtom(refetchEmailListAtom); | ||
| useEffect(() => { | ||
| setRefetchEmailList({ refetch }); | ||
|
|
@@ -88,7 +109,7 @@ export default function Mail(props: { | |
|
|
||
| return ( | ||
| <> | ||
| <PermissionsCheck /> | ||
| {provider !== "outlook" && <PermissionsCheck />} | ||
| <ClientOnly> | ||
| <BetaBanner /> | ||
| </ClientOnly> | ||
|
|
@@ -101,6 +122,7 @@ export default function Mail(props: { | |
| showLoadMore={showLoadMore} | ||
| handleLoadMore={handleLoadMore} | ||
| isLoadingMore={isLoadingMore} | ||
| provider={provider} | ||
| /> | ||
| )} | ||
| </LoadingContent> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||
| import type { ThreadsQuery } from "./validation"; | ||||||||||||||
|
|
||||||||||||||
| export async function getOutlookThreads({ | ||||||||||||||
| accessToken, | ||||||||||||||
| query, | ||||||||||||||
| }: { | ||||||||||||||
| accessToken: string; | ||||||||||||||
| query: ThreadsQuery; | ||||||||||||||
| }) { | ||||||||||||||
| const params = new URLSearchParams(); | ||||||||||||||
| if (query.limit) params.set("$top", String(query.limit)); | ||||||||||||||
| if (query.nextPageToken) params.set("$skip", String(query.nextPageToken)); | ||||||||||||||
|
|
||||||||||||||
| const url = | ||||||||||||||
| "https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages" + | ||||||||||||||
| "?" + | ||||||||||||||
| params.toString() + | ||||||||||||||
| "&$select=id,conversationId,subject,bodyPreview,from,receivedDateTime"; | ||||||||||||||
|
|
||||||||||||||
| const res = await fetch(url, { | ||||||||||||||
| headers: { Authorization: "Bearer ${accessToken}" }, | ||||||||||||||
| }); | ||||||||||||||
|
Comment on lines
+20
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect header interpolation – access token is never injected The string - headers: { Authorization: "Bearer ${accessToken}" },
+ headers: { Authorization: `Bearer ${accessToken}` },📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| if (!res.ok) throw new Error(await res.text()); | ||||||||||||||
| const body = await res.json(); | ||||||||||||||
| return body.value; | ||||||||||||||
| } | ||||||||||||||
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.
🛠️ Refactor suggestion
Commented-out redirect leaves UX edge case unresolved
The “too many redirects” loop is masked by commenting code out, but new users without a rule now silently skip onboarding. Either:
🤖 Prompt for AI Agents