import { Loading } from "@/components/primitives/Loading"; import { Text } from "@/components/primitives/Text"; import { useFacet } from "@/lib/facet"; import { lighten } from "@/lib/facet/src/lib/colors"; import { registerNewLattice } from "@/lib/utils/gmstn"; import { useOAuthAgentGuaranteed, useOAuthSessionGuaranteed, } from "@/providers/OAuthProvider"; import { useCurrentPalette } from "@/providers/ThemeProvider"; import { useLatticesQuery } from "@/queries/hooks/useLatticesQuery"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import type { Dispatch, SetStateAction } from "react"; import { useState } from "react"; import { Pressable, TextInput, View } from "react-native"; export const RegisterLatticeModalContent = ({ setShowRegisterModal, }: { setShowRegisterModal: Dispatch>; }) => { const { semantic } = useCurrentPalette(); const { atoms, typography } = useFacet(); const [inputText, setInputText] = useState(""); const [registerError, setRegisterError] = useState( undefined, ); const agent = useOAuthAgentGuaranteed(); const session = useOAuthSessionGuaranteed(); const queryClient = useQueryClient(); const { queryKey: latticesQueryKey } = useLatticesQuery(session); const { mutate: newLatticeMutation, isPending: mutationPending } = useMutation({ mutationFn: async () => { const registerResult = await registerNewLattice({ latticeDomain: inputText, agent, }); if (!registerResult.ok) { console.error( "Something went wrong when registering the lattice.", registerResult.error, ); throw new Error( `Something went wrong when registering the lattice. ${registerResult.error}`, ); } setShowRegisterModal(false); }, onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: latticesQueryKey, }); setShowRegisterModal(false); }, onError: (err) => { console.error( "Something went wrong when registering the lattice.", err, ); setRegisterError(err.message); }, }); const readyToSubmit = !!inputText.trim(); return ( Lattice domain: { newLatticeMutation(); }} > {({ hovered }) => mutationPending ? ( ) : ( Add ) } ); };