mirror of
https://github.com/supabase/supabase.git
synced 2026-07-07 07:30:21 +08:00
37 lines
839 B
TypeScript
37 lines
839 B
TypeScript
import useSWR, { mutate } from 'swr'
|
|
import { get } from 'lib/common/fetch'
|
|
import { API_URL } from 'lib/constants'
|
|
import { User } from 'types'
|
|
|
|
export function useProfile(returning?: 'minimal') {
|
|
let url = `${API_URL}/profile`
|
|
|
|
if (returning) {
|
|
const query = new URLSearchParams({ returning }).toString()
|
|
url = `${url}?${query}`
|
|
}
|
|
|
|
const { data, error } = useSWR<any>(url, get, {
|
|
loadingTimeout: 10000,
|
|
})
|
|
const anyError = data?.error || error
|
|
|
|
function mutateProfile(updatedUser: User, revalidate?: boolean) {
|
|
mutate(
|
|
url,
|
|
{
|
|
first_name: updatedUser.first_name,
|
|
last_name: updatedUser.last_name,
|
|
},
|
|
revalidate ?? true
|
|
)
|
|
}
|
|
|
|
return {
|
|
profile: anyError ? undefined : data,
|
|
isLoading: !anyError && !data,
|
|
isError: !!anyError,
|
|
mutateProfile,
|
|
}
|
|
}
|