mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
34 lines
919 B
TypeScript
34 lines
919 B
TypeScript
import useSWR from 'swr';
|
|
import { API } from '~/constants/api';
|
|
import { apiAuthGetUserProfile } from '~/api/auth';
|
|
import { EMPTY_USER } from '~/constants/auth';
|
|
import { useCallback } from 'react';
|
|
import { IUser } from '~/types/auth';
|
|
|
|
const getKey = (username?: string): string | null => {
|
|
return username ? `${API.USER.PROFILE}/${username}` : null;
|
|
};
|
|
|
|
export const useGetProfile = (username?: string) => {
|
|
const { data, isValidating, mutate } = useSWR(
|
|
getKey(username),
|
|
async () => {
|
|
const result = await apiAuthGetUserProfile({ username: username || '' });
|
|
return result.user;
|
|
},
|
|
{
|
|
refreshInterval: 60000,
|
|
}
|
|
);
|
|
|
|
const profile = data || EMPTY_USER;
|
|
|
|
const update = useCallback(
|
|
async (user: Partial<IUser>) => {
|
|
await mutate({ ...profile, ...user });
|
|
},
|
|
[mutate, profile]
|
|
);
|
|
|
|
return { profile, isLoading: !data && isValidating, update };
|
|
};
|