-
Notifications
You must be signed in to change notification settings - Fork 15
feat: email query param override for chat #1647
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7d30205
feat: pass email query param to API for accountId override
519f15a
fix: handle array-valued query params safely
7cc0ddb
refactor: resolve email → accountId client-side via GET /api/accounts…
sweetmantech 01b529f
refactor: use AccountOverrideSync instead of prop drilling email
sweetmantech dc1b80a
revert: remove unrelated page changes from PR
sweetmantech 1547338
revert: remove formatting-only changes from chat.tsx
sweetmantech e277061
revert: remove formatting-only changes from VercelChatProvider.tsx
sweetmantech 3e7af93
refactor: use tanstack useQuery for AccountOverrideSync
sweetmantech b14f607
refactor: SRP - extract fetchAccountIdByEmail to lib
sweetmantech c8bfa39
feat: add AccountOverrideBadge pill to layout
sweetmantech 0aab24f
refactor: DRY - use shared tanstack query cache for AccountOverrideBadge
sweetmantech b3aa9bd
refactor: DRY - read from session storage set by AccountOverrideSync
sweetmantech 50a8713
refactor: remove useEffect, derive badge visibility from query param
sweetmantech b7d5d4e
fix: high-contrast amber badge for account override
sweetmantech b934e9e
fix: brighter amber-400 badge in dark mode for visibility
sweetmantech bb917e1
refactor: convert to AccountOverrideProvider context
sweetmantech 7cc4134
fix: throw on non-404 errors in fetchAccountIdByEmail
sweetmantech f8bac34
refactor: SRP - extract storage helpers from AccountOverrideProvider
sweetmantech c030a16
refactor: SRP file naming, remove useEffect with useQuery
sweetmantech cfb889e
refactor: move override storage libs to lib/accounts/override/
sweetmantech 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
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
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,28 @@ | ||
| "use client"; | ||
|
|
||
| import { X } from "lucide-react"; | ||
| import { useAccountOverride } from "@/providers/AccountOverrideProvider"; | ||
|
|
||
| /** | ||
| * Displays a pill badge when an account override is active. | ||
| * Reads from AccountOverrideProvider context. | ||
| */ | ||
| export default function AccountOverrideBadge() { | ||
| const { email, accountIdOverride, clear } = useAccountOverride(); | ||
|
|
||
| if (!accountIdOverride || !email) return null; | ||
|
|
||
| return ( | ||
| <div className="fixed top-3 left-1/2 -translate-x-1/2 z-50 flex items-center gap-2 bg-amber-500 dark:bg-amber-400 text-black px-4 py-2 rounded-full shadow-lg text-sm font-medium"> | ||
| <span>Viewing as</span> | ||
| <span className="font-bold">{email}</span> | ||
| <button | ||
| onClick={clear} | ||
| className="ml-1 p-0.5 rounded-full hover:bg-black/10 transition-colors" | ||
| aria-label="Clear account override" | ||
| > | ||
| <X className="h-4 w-4" /> | ||
| </button> | ||
| </div> | ||
| ); | ||
| } |
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
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,25 @@ | ||
| import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; | ||
|
|
||
| /** | ||
| * Fetches an account ID by email address via GET /api/accounts/{email}. | ||
| * | ||
| * @param email - The email address to look up | ||
| * @param accessToken - Bearer token for authentication | ||
| * @returns The account ID, or null if not found | ||
| */ | ||
| export async function fetchAccountIdByEmail( | ||
| email: string, | ||
| accessToken: string, | ||
| ): Promise<string | null> { | ||
| const baseUrl = getClientApiBaseUrl(); | ||
| const response = await fetch( | ||
| `${baseUrl}/api/accounts/${encodeURIComponent(email)}`, | ||
| { headers: { Authorization: `Bearer ${accessToken}` } }, | ||
| ); | ||
|
|
||
| if (response.status === 404) return null; | ||
| if (!response.ok) throw new Error(`Account lookup failed: ${response.status}`); | ||
|
|
||
| const data = await response.json(); | ||
| return data.account?.account_id ?? null; | ||
| } |
|
Collaborator
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. Should these 3 libs be moved to
|
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,9 @@ | ||
| import { ACCOUNT_OVERRIDE_STORAGE_KEY } from "@/lib/consts"; | ||
|
|
||
| /** | ||
| * Removes the account override from session storage. | ||
| */ | ||
| export function clearStoredAccountOverride(): void { | ||
| window.sessionStorage.removeItem(ACCOUNT_OVERRIDE_STORAGE_KEY); | ||
| window.sessionStorage.removeItem(`${ACCOUNT_OVERRIDE_STORAGE_KEY}_email`); | ||
| } |
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,15 @@ | ||
| import { ACCOUNT_OVERRIDE_STORAGE_KEY } from "@/lib/consts"; | ||
|
|
||
| /** | ||
| * Reads the stored account override from session storage. | ||
| */ | ||
| export function getStoredAccountOverride(): { | ||
| accountId: string | null; | ||
| email: string | null; | ||
| } { | ||
| if (typeof window === "undefined") return { accountId: null, email: null }; | ||
| return { | ||
| accountId: window.sessionStorage.getItem(ACCOUNT_OVERRIDE_STORAGE_KEY), | ||
| email: window.sessionStorage.getItem(`${ACCOUNT_OVERRIDE_STORAGE_KEY}_email`), | ||
| }; | ||
| } |
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,12 @@ | ||
| import { ACCOUNT_OVERRIDE_STORAGE_KEY } from "@/lib/consts"; | ||
|
|
||
| /** | ||
| * Persists an account override to session storage. | ||
| */ | ||
| export function setStoredAccountOverride( | ||
| accountId: string, | ||
| email: string, | ||
| ): void { | ||
| window.sessionStorage.setItem(ACCOUNT_OVERRIDE_STORAGE_KEY, accountId); | ||
| window.sessionStorage.setItem(`${ACCOUNT_OVERRIDE_STORAGE_KEY}_email`, email); | ||
| } |
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
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,83 @@ | ||
| "use client"; | ||
|
|
||
| import { createContext, useContext, useState, useCallback, ReactNode } from "react"; | ||
| import { useSearchParams, useRouter } from "next/navigation"; | ||
| import { usePrivy } from "@privy-io/react-auth"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { fetchAccountIdByEmail } from "@/lib/accounts/fetchAccountIdByEmail"; | ||
| import { getStoredAccountOverride } from "@/lib/accounts/override/getStoredAccountOverride"; | ||
| import { setStoredAccountOverride } from "@/lib/accounts/override/setStoredAccountOverride"; | ||
| import { clearStoredAccountOverride } from "@/lib/accounts/override/clearStoredAccountOverride"; | ||
|
|
||
| interface AccountOverrideContextType { | ||
| accountIdOverride: string | null; | ||
| email: string | null; | ||
| clear: () => void; | ||
| } | ||
|
|
||
| const AccountOverrideContext = createContext<AccountOverrideContextType>({ | ||
| accountIdOverride: null, | ||
| email: null, | ||
| clear: () => {}, | ||
| }); | ||
|
|
||
| /** | ||
| * Provider that manages the account override lifecycle. | ||
| * Reads ?email= from URL, resolves to accountId, persists in session storage. | ||
| * Single source of truth for all override consumers. | ||
| * Placed inside PrivyProvider because it needs getAccessToken. | ||
| */ | ||
| export function AccountOverrideProvider({ children }: { children: ReactNode }) { | ||
| const searchParams = useSearchParams(); | ||
| const router = useRouter(); | ||
| const { getAccessToken } = usePrivy(); | ||
| const emailParam = searchParams.get("email"); | ||
|
|
||
| const [stored, setStored] = useState(getStoredAccountOverride); | ||
| const email = emailParam || stored.email; | ||
| const isClear = emailParam === "clear"; | ||
|
|
||
| const { data: resolvedAccountId } = useQuery({ | ||
| queryKey: ["accountOverride", email], | ||
| queryFn: async () => { | ||
| if (isClear) { | ||
| clearStoredAccountOverride(); | ||
| setStored({ accountId: null, email: null }); | ||
| return null; | ||
| } | ||
| const accessToken = await getAccessToken(); | ||
| if (!accessToken) return null; | ||
| const accountId = await fetchAccountIdByEmail(email!, accessToken); | ||
| if (accountId && email) { | ||
| setStoredAccountOverride(accountId, email); | ||
| setStored({ accountId, email }); | ||
| } | ||
| return accountId; | ||
| }, | ||
| enabled: (!!email || isClear) && !stored.accountId, | ||
| staleTime: Infinity, | ||
| }); | ||
|
|
||
| const accountIdOverride = stored.accountId || resolvedAccountId || null; | ||
|
|
||
| const clear = useCallback(() => { | ||
| clearStoredAccountOverride(); | ||
| setStored({ accountId: null, email: null }); | ||
| const params = new URLSearchParams(searchParams.toString()); | ||
| params.delete("email"); | ||
| const newPath = params.toString() | ||
| ? `${window.location.pathname}?${params.toString()}` | ||
| : window.location.pathname; | ||
| router.replace(newPath); | ||
| }, [searchParams, router]); | ||
|
|
||
| return ( | ||
| <AccountOverrideContext.Provider value={{ accountIdOverride, email, clear }}> | ||
| {children} | ||
| </AccountOverrideContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useAccountOverride() { | ||
| return useContext(AccountOverrideContext); | ||
| } | ||
|
Collaborator
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. SRP - create lib files so the provider has less than 100 lines of code. |
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.