mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
fix: use get profile api v2
This commit is contained in:
1 parent
e83ddb92ae
commit
eb057cb19b
1 file changed
+19
-11
@@ -4,7 +4,7 @@ import { API_URL } from 'lib/constants'
|
||||
import { useCallback } from 'react'
|
||||
import { profileKeys } from './keys'
|
||||
import Telemetry from 'lib/telemetry'
|
||||
import { useRouter } from 'next/router'
|
||||
import { NextRouter, useRouter } from 'next/router'
|
||||
import { useTelemetryProps } from 'common'
|
||||
|
||||
export type Profile = {
|
||||
@@ -26,10 +26,7 @@ export type ProfileResponse = Profile
|
||||
* Create profile for new user
|
||||
* This will also need to send a sign_up event
|
||||
*/
|
||||
async function createProfile() {
|
||||
const router = useRouter()
|
||||
const telemetryProps = useTelemetryProps()
|
||||
|
||||
async function createProfile(router: NextRouter, telemetryProps: any) {
|
||||
const response = await post(`${API_URL}/profile`, {})
|
||||
if (response.error) {
|
||||
throw response.error
|
||||
@@ -45,14 +42,17 @@ async function createProfile() {
|
||||
return response
|
||||
}
|
||||
|
||||
export async function getProfile(signal?: AbortSignal) {
|
||||
export async function getProfile(router: NextRouter, telemetryProps: any, signal?: AbortSignal) {
|
||||
const response = await get(`${API_URL}/profile`, {
|
||||
signal,
|
||||
headers: {
|
||||
Version: '2',
|
||||
},
|
||||
})
|
||||
if (response.error) {
|
||||
// if response.error is not found
|
||||
// we need to send request to create new user profile
|
||||
if (response.error.code === 404) return await createProfile()
|
||||
if (response.error.code === 404) return await createProfile(router, telemetryProps)
|
||||
throw response.error
|
||||
}
|
||||
|
||||
@@ -65,20 +65,28 @@ export type ProfileError = unknown
|
||||
export const useProfileQuery = <TData = ProfileData>({
|
||||
enabled = true,
|
||||
...options
|
||||
}: UseQueryOptions<ProfileData, ProfileError, TData> = {}) =>
|
||||
useQuery<ProfileData, ProfileError, TData>(
|
||||
}: UseQueryOptions<ProfileData, ProfileError, TData> = {}) => {
|
||||
const router = useRouter()
|
||||
const telemetryProps = useTelemetryProps()
|
||||
|
||||
return useQuery<ProfileData, ProfileError, TData>(
|
||||
profileKeys.profile(),
|
||||
({ signal }) => getProfile(signal),
|
||||
({ signal }) => getProfile(router, telemetryProps, signal),
|
||||
{
|
||||
staleTime: 1000 * 60 * 30, // default good for 30 mins
|
||||
...options,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const useProfilePrefetch = () => {
|
||||
const client = useQueryClient()
|
||||
const router = useRouter()
|
||||
const telemetryProps = useTelemetryProps()
|
||||
|
||||
return useCallback(() => {
|
||||
client.prefetchQuery(profileKeys.profile(), ({ signal }) => getProfile(signal))
|
||||
client.prefetchQuery(profileKeys.profile(), ({ signal }) =>
|
||||
getProfile(router, telemetryProps, signal)
|
||||
)
|
||||
}, [])
|
||||
}
|
||||
Reference in new issue
Block a user