(email: string)
| 10 | } |
| 11 | |
| 12 | export function useUserMetadata(email: string) { |
| 13 | const { userDirectoryUrl } = useOutletContext<{ userDirectoryUrl?: string }>() |
| 14 | const search = new URLSearchParams({ email }) |
| 15 | |
| 16 | const key = `${userDirectoryUrl}?${search}` |
| 17 | |
| 18 | const initialData: UserMetadata = { |
| 19 | displayName: email, |
| 20 | } |
| 21 | |
| 22 | return useQuery({ |
| 23 | initialData, |
| 24 | queryKey: [key], |
| 25 | queryFn: async ({ queryKey: [key] }) => { |
| 26 | if (userDirectoryUrl === undefined) return Promise.resolve(initialData) |
| 27 | const response = await fetch(key, { credentials: 'include' }) |
| 28 | |
| 29 | if ( |
| 30 | response.headers.get('Content-Type')?.startsWith('application/json') |
| 31 | ) { |
| 32 | const parsedData: UserMetadata = (await response.json()) as any |
| 33 | return { |
| 34 | ...parsedData, |
| 35 | displayName: `${parsedData.firstName} ${parsedData.lastName}`, |
| 36 | } |
| 37 | } |
| 38 | return initialData |
| 39 | }, |
| 40 | }) |
| 41 | } |
no test coverage detected