| 31 | } |
| 32 | |
| 33 | const useUserInfoForm = (): UseUserInfoForm => { |
| 34 | const qc = useQueryClient(); |
| 35 | const { user, updateUser } = useContext(AuthContext); |
| 36 | const { logEvent } = useLogContext(); |
| 37 | const { displayToast } = useToastNotification(); |
| 38 | const router = useRouter(); |
| 39 | const userId = user?.id ?? ''; |
| 40 | |
| 41 | // Fetch full profile via GraphQL to get socialLinks (boot endpoint doesn't include them) |
| 42 | const userQueryKey = generateQueryKey(RequestKey.Profile, user, { |
| 43 | id: userId, |
| 44 | }); |
| 45 | const { data: fullProfile, isError: isProfileError } = useQuery({ |
| 46 | queryKey: userQueryKey, |
| 47 | queryFn: () => getProfile(userId), |
| 48 | ...disabledRefetch, |
| 49 | staleTime: StaleTime.OneHour, |
| 50 | enabled: !!userId, |
| 51 | }); |
| 52 | |
| 53 | const hasInitializedSocialLinks = |
| 54 | !!fullProfile || Array.isArray(user?.socialLinks); |
| 55 | |
| 56 | useEffect(() => { |
| 57 | const searchParams = new URLSearchParams(window?.location?.search); |
| 58 | const field = searchParams?.get('field'); |
| 59 | if (field) { |
| 60 | const element = document?.getElementsByName(field)[0]; |
| 61 | if (element) { |
| 62 | element.focus(); |
| 63 | } |
| 64 | } |
| 65 | }, []); |
| 66 | |
| 67 | const methods = useForm<UserProfile>({ |
| 68 | defaultValues: { |
| 69 | name: user?.name, |
| 70 | username: user?.username, |
| 71 | image: user?.image, |
| 72 | cover: user?.cover, |
| 73 | bio: user?.bio, |
| 74 | externalLocationId: user?.location?.externalId, |
| 75 | experienceLevel: user?.experienceLevel, |
| 76 | hideExperience: user?.hideExperience, |
| 77 | readme: user?.readme || '', |
| 78 | socialLinks: user?.socialLinks || [], |
| 79 | }, |
| 80 | }); |
| 81 | |
| 82 | useEffect(() => { |
| 83 | if (!fullProfile) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | const serverLinks = fullProfile.socialLinks || []; |
| 88 | |
| 89 | if (!methods.getFieldState('socialLinks').isDirty) { |
| 90 | methods.resetField('socialLinks', { defaultValue: serverLinks }); |