use hook for profile details

This commit is contained in:
saml33 2023-11-09 15:24:44 +11:00
parent 8d45805136
commit 923cbebcef
6 changed files with 49 additions and 46 deletions

View File

@ -8,6 +8,7 @@ import { bs58 } from '@project-serum/anchor/dist/cjs/utils/bytes'
import { notify } from 'utils/notifications' import { notify } from 'utils/notifications'
import { MANGO_DATA_API_URL } from 'utils/constants' import { MANGO_DATA_API_URL } from 'utils/constants'
import { ImgWithLoader } from '@components/ImgWithLoader' import { ImgWithLoader } from '@components/ImgWithLoader'
import useProfileDetails from 'hooks/useProfileDetails'
const EditNftProfilePic = ({ onClose }: { onClose: () => void }) => { const EditNftProfilePic = ({ onClose }: { onClose: () => void }) => {
const { t } = useTranslation(['common', 'profile']) const { t } = useTranslation(['common', 'profile'])
@ -15,8 +16,8 @@ const EditNftProfilePic = ({ onClose }: { onClose: () => void }) => {
const nfts = mangoStore((s) => s.wallet.nfts.data) const nfts = mangoStore((s) => s.wallet.nfts.data)
const nftsLoading = mangoStore((s) => s.wallet.nfts.loading) const nftsLoading = mangoStore((s) => s.wallet.nfts.loading)
const [selectedProfile, setSelectedProfile] = useState<string>('') const [selectedProfile, setSelectedProfile] = useState<string>('')
const actions = mangoStore.getState().actions const { refetch: refetchProfileDetails } = useProfileDetails()
const profile = mangoStore((s) => s.profile.details) const { data: profile } = useProfileDetails()
useEffect(() => { useEffect(() => {
if (profile?.profile_image_url) { if (profile?.profile_image_url) {
@ -54,7 +55,7 @@ const EditNftProfilePic = ({ onClose }: { onClose: () => void }) => {
requestOptions, requestOptions,
) )
if (response.status === 200) { if (response.status === 200) {
await actions.fetchProfileDetails(publicKey.toString()) await refetchProfileDetails()
notify({ notify({
type: 'success', type: 'success',
title: t('profile:profile-pic-success'), title: t('profile:profile-pic-success'),
@ -100,7 +101,7 @@ const EditNftProfilePic = ({ onClose }: { onClose: () => void }) => {
requestOptions, requestOptions,
) )
if (response.status === 200) { if (response.status === 200) {
await actions.fetchProfileDetails(publicKey.toString()) await refetchProfileDetails()
notify({ notify({
type: 'success', type: 'success',
title: t('profile:profile-pic-remove-success'), title: t('profile:profile-pic-remove-success'),

View File

@ -10,13 +10,13 @@ import {
} from '@heroicons/react/20/solid' } from '@heroicons/react/20/solid'
import { bs58 } from '@project-serum/anchor/dist/cjs/utils/bytes' import { bs58 } from '@project-serum/anchor/dist/cjs/utils/bytes'
import { useWallet } from '@solana/wallet-adapter-react' import { useWallet } from '@solana/wallet-adapter-react'
import mangoStore from '@store/mangoStore'
import startCase from 'lodash/startCase' import startCase from 'lodash/startCase'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
import { ChangeEvent, useState } from 'react' import { ChangeEvent, useState } from 'react'
import { MANGO_DATA_API_URL } from 'utils/constants' import { MANGO_DATA_API_URL } from 'utils/constants'
import { notify } from 'utils/notifications' import { notify } from 'utils/notifications'
import ProfileImage from './ProfileImage' import ProfileImage from './ProfileImage'
import useProfileDetails from 'hooks/useProfileDetails'
const EditProfileForm = ({ const EditProfileForm = ({
onFinish, onFinish,
@ -28,7 +28,7 @@ const EditProfileForm = ({
onboarding?: boolean onboarding?: boolean
}) => { }) => {
const { t } = useTranslation(['profile', 'onboarding']) const { t } = useTranslation(['profile', 'onboarding'])
const profile = mangoStore((s) => s.profile.details) const { data: profile, refetch: refetchProfileDetails } = useProfileDetails()
const { publicKey, signMessage } = useWallet() const { publicKey, signMessage } = useWallet()
const [profileName, setProfileName] = useState( const [profileName, setProfileName] = useState(
startCase(profile?.profile_name) || '', startCase(profile?.profile_name) || '',
@ -37,7 +37,6 @@ const EditProfileForm = ({
const [loadUniquenessCheck, setLoadUniquenessCheck] = useState(false) const [loadUniquenessCheck, setLoadUniquenessCheck] = useState(false)
const [loadUpdateProfile, setLoadUpdateProfile] = useState(false) const [loadUpdateProfile, setLoadUpdateProfile] = useState(false)
const [updateError, setUpdateError] = useState('') const [updateError, setUpdateError] = useState('')
const actions = mangoStore.getState().actions
const validateProfileNameUniqueness = async (name: string) => { const validateProfileNameUniqueness = async (name: string) => {
try { try {
@ -109,7 +108,7 @@ const EditProfileForm = ({
) )
if (response.status === 200) { if (response.status === 200) {
setLoadUpdateProfile(false) setLoadUpdateProfile(false)
await actions.fetchProfileDetails(publicKey.toString()) await refetchProfileDetails()
onFinish() onFinish()
notify({ notify({
type: 'success', type: 'success',

View File

@ -1,4 +1,4 @@
import mangoStore from '../../store/mangoStore' import useProfileDetails from 'hooks/useProfileDetails'
import ProfileIcon from '../icons/ProfileIcon' import ProfileIcon from '../icons/ProfileIcon'
const ProfileImage = ({ const ProfileImage = ({
@ -12,7 +12,7 @@ const ProfileImage = ({
imageUrl?: string | null imageUrl?: string | null
isOwnerProfile?: boolean isOwnerProfile?: boolean
}) => { }) => {
const profile = mangoStore((s) => s.profile.details) const { data: profile } = useProfileDetails()
return imageUrl || (isOwnerProfile && profile?.profile_image_url) ? ( return imageUrl || (isOwnerProfile && profile?.profile_image_url) ? (
<img <img

View File

@ -18,6 +18,7 @@ import { TV_USER_ID_KEY } from 'utils/constants'
import useLocalStorageState from 'hooks/useLocalStorageState' import useLocalStorageState from 'hooks/useLocalStorageState'
import Loading from '@components/shared/Loading' import Loading from '@components/shared/Loading'
import SheenLoader from '@components/shared/SheenLoader' import SheenLoader from '@components/shared/SheenLoader'
import useProfileDetails from 'hooks/useProfileDetails'
const set = mangoStore.getState().set const set = mangoStore.getState().set
const actions = mangoStore.getState().actions const actions = mangoStore.getState().actions
@ -29,9 +30,11 @@ const ConnectedMenu = () => {
const [tvUserId, setTvUserId] = useLocalStorageState(TV_USER_ID_KEY, '') const [tvUserId, setTvUserId] = useLocalStorageState(TV_USER_ID_KEY, '')
const [showEditProfileModal, setShowEditProfileModal] = useState(false) const [showEditProfileModal, setShowEditProfileModal] = useState(false)
const [showMangoAccountsModal, setShowMangoAccountsModal] = useState(false) const [showMangoAccountsModal, setShowMangoAccountsModal] = useState(false)
const {
const profileDetails = mangoStore((s) => s.profile.details) data: profileDetails,
const loadProfileDetails = mangoStore((s) => s.profile.loadDetails) isInitialLoading: loadProfileDetails,
refetch: refetchProfileDetails,
} = useProfileDetails()
const groupLoaded = mangoStore((s) => s.groupLoaded) const groupLoaded = mangoStore((s) => s.groupLoaded)
const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad) const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad)
@ -54,7 +57,7 @@ const ConnectedMenu = () => {
if (publicKey && wallet && groupLoaded) { if (publicKey && wallet && groupLoaded) {
actions.connectMangoClientWithWallet(wallet) actions.connectMangoClientWithWallet(wallet)
actions.fetchMangoAccounts(publicKey) actions.fetchMangoAccounts(publicKey)
actions.fetchProfileDetails(publicKey.toString()) refetchProfileDetails()
actions.fetchWalletTokens(publicKey) actions.fetchWalletTokens(publicKey)
if (!tvUserId) { if (!tvUserId) {
setTvUserId(publicKey.toString()) setTvUserId(publicKey.toString())

View File

@ -0,0 +1,31 @@
import { useWallet } from '@solana/wallet-adapter-react'
import { useQuery } from '@tanstack/react-query'
import { MANGO_DATA_API_URL } from 'utils/constants'
const fetchProfileDetails = async (walletPk: string | undefined) => {
try {
const response = await fetch(
`${MANGO_DATA_API_URL}/user-data/profile-details?wallet-pk=${walletPk}`,
)
const data = await response.json()
return data
} catch (e) {
console.error('failed to fetch profile details', e)
}
}
export default function useProfileDetails() {
const { publicKey } = useWallet()
const { data, isInitialLoading, refetch } = useQuery(
['profile-details', publicKey],
() => fetchProfileDetails(publicKey?.toString()),
{
cacheTime: 1000 * 60 * 10,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: false,
enabled: !!publicKey,
},
)
return { data, isInitialLoading, refetch }
}

View File

@ -63,7 +63,6 @@ import {
TokenStatsItem, TokenStatsItem,
NFT, NFT,
TourSettings, TourSettings,
ProfileDetails,
MangoTokenStatsItem, MangoTokenStatsItem,
ThemeData, ThemeData,
PositionStat, PositionStat,
@ -87,7 +86,7 @@ import mapValues from 'lodash/mapValues'
import groupBy from 'lodash/groupBy' import groupBy from 'lodash/groupBy'
import sampleSize from 'lodash/sampleSize' import sampleSize from 'lodash/sampleSize'
import { fetchTokenStatsData, processTokenStatsData } from 'apis/mngo' import { fetchTokenStatsData, processTokenStatsData } from 'apis/mngo'
import { OrderTypes, TriggerOrderTypes } from 'utils/tradeForm' import { OrderTypes } from 'utils/tradeForm'
const GROUP = new PublicKey('78b8f4cGCwmZ9ysPFMWLaLTkkaYnUjwMJYStWe5RTSSX') const GROUP = new PublicKey('78b8f4cGCwmZ9ysPFMWLaLTkkaYnUjwMJYStWe5RTSSX')
@ -199,10 +198,6 @@ export type MangoStore = {
} }
} }
orderbookTooltip: OrderbookTooltip | undefined orderbookTooltip: OrderbookTooltip | undefined
profile: {
details: ProfileDetails | null
loadDetails: boolean
}
prependedGlobalAdditionalInstructions: TransactionInstruction[] prependedGlobalAdditionalInstructions: TransactionInstruction[]
priorityFee: number priorityFee: number
selectedMarket: { selectedMarket: {
@ -283,7 +278,6 @@ export type MangoStore = {
fetchOpenOrders: (refetchMangoAccount?: boolean) => Promise<void> fetchOpenOrders: (refetchMangoAccount?: boolean) => Promise<void>
fetchPerpStats: () => void fetchPerpStats: () => void
fetchPositionsStats: () => void fetchPositionsStats: () => void
fetchProfileDetails: (walletPk: string) => void
fetchSwapHistory: ( fetchSwapHistory: (
mangoAccountPk: string, mangoAccountPk: string,
timeout?: number, timeout?: number,
@ -368,10 +362,6 @@ const mangoStore = create<MangoStore>()(
}, },
}, },
orderbookTooltip: undefined, orderbookTooltip: undefined,
profile: {
loadDetails: false,
details: { profile_name: '', trader_category: '', wallet_pk: '' },
},
priorityFee: DEFAULT_PRIORITY_FEE, priorityFee: DEFAULT_PRIORITY_FEE,
prependedGlobalAdditionalInstructions: [], prependedGlobalAdditionalInstructions: [],
selectedMarket: { selectedMarket: {
@ -1006,27 +996,6 @@ const mangoStore = create<MangoStore>()(
s.prependedGlobalAdditionalInstructions = instructions s.prependedGlobalAdditionalInstructions = instructions
}) })
}, },
async fetchProfileDetails(walletPk: string) {
const set = get().set
set((state) => {
state.profile.loadDetails = true
})
try {
const response = await fetch(
`${MANGO_DATA_API_URL}/user-data/profile-details?wallet-pk=${walletPk}`,
)
const data = await response.json()
set((state) => {
state.profile.details = data
state.profile.loadDetails = false
})
} catch (e) {
console.error(e)
set((state) => {
state.profile.loadDetails = false
})
}
},
async fetchTourSettings(walletPk: string) { async fetchTourSettings(walletPk: string) {
const set = get().set const set = get().set
set((state) => { set((state) => {