import { Input, MetaButton, MetaHeading, useToast } from '@metafam/ds'; import { FlexContainer } from 'components/Container'; import { useSetupFlow } from 'contexts/SetupContext'; import { useUpdatePlayerUsernameMutation } from 'graphql/autogen/types'; import { useUser } from 'lib/hooks'; import React, { useState } from 'react'; export type SetupUsernameProps = { username: string | undefined; setUsername: React.Dispatch>; }; export const SetupUsername: React.FC = ({ username, setUsername, }) => { const { onNextPress, nextButtonLabel } = useSetupFlow(); const { user } = useUser(); const toast = useToast(); const [updateUsernameRes, updateUsername] = useUpdatePlayerUsernameMutation(); const [loading, setLoading] = useState(false); const handleNextPress = async () => { if (!user) return; setLoading(true); const { error } = await updateUsername({ playerId: user.id, username: username ?? '', }); if (error) { let errorDetail = 'The octo is sad 😢'; if (error.message.includes('Uniqueness violation')) { errorDetail = 'This username is already taken 😢'; } else if (error.message.includes('username_is_valid')) { errorDetail = 'A username can only contain lowercase letters, numbers, and dashes.'; } toast({ title: 'Error', description: `Unable to update Player Username. ${errorDetail}`, status: 'error', isClosable: true, }); setLoading(false); return; } onNextPress(); }; return ( What username would you like? setUsername(value)} w="auto" /> {nextButtonLabel} ); };