mango-v4-ui/components/tours/CustomTooltip.tsx

119 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-09-22 21:43:55 -07:00
import Loading from '@components/shared/Loading'
2022-09-21 21:25:24 -07:00
import { XMarkIcon } from '@heroicons/react/20/solid'
2022-09-22 21:00:42 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
import mangoStore from '@store/mangoStore'
2022-09-22 21:43:55 -07:00
import { useState } from 'react'
import { MANGO_DATA_API_URL } from 'utils/constants'
2022-09-21 21:25:24 -07:00
import { WalktourLogic } from 'walktour'
const CustomTooltip = ({
customOnClose,
2022-09-22 21:00:42 -07:00
hasSeenKey,
tourLogic,
2022-09-21 21:25:24 -07:00
}: {
customOnClose?: () => void
2022-09-22 21:00:42 -07:00
hasSeenKey: 'account_tour_seen' | 'swap_tour_seen' | 'trade_tour_seen'
tourLogic: WalktourLogic | undefined
2022-09-21 21:25:24 -07:00
}) => {
const { title, description } = tourLogic!.stepContent
const { next, prev, close, allSteps, stepIndex } = tourLogic!
2022-09-22 21:00:42 -07:00
const { publicKey } = useWallet()
2023-01-03 14:20:00 -08:00
const actions = mangoStore.getState().actions
2022-09-22 21:00:42 -07:00
const tourSettings = mangoStore((s) => s.settings.tours)
2022-09-22 21:43:55 -07:00
const [loading, setLoading] = useState(false)
2022-09-22 21:00:42 -07:00
const onClose = async () => {
if (!publicKey) return
if (tourSettings) {
setLoading(true)
try {
const settings = {
...tourSettings,
}
settings[hasSeenKey] = true
const message = JSON.stringify(settings)
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: message,
}
const response = await fetch(
`${MANGO_DATA_API_URL}/user-data/settings-unsigned`,
requestOptions
)
if (response.status === 200) {
await actions.fetchTourSettings(publicKey.toString())
}
} catch (e) {
2022-11-01 11:13:02 -07:00
console.error(e)
} finally {
if (customOnClose) {
customOnClose()
}
setLoading(false)
close()
2022-09-22 21:00:42 -07:00
}
} else close()
2022-09-21 21:25:24 -07:00
}
return (
2022-10-25 17:04:09 -07:00
<div className="relative w-72 rounded-lg bg-th-bkg-2 p-4">
2022-09-22 21:43:55 -07:00
{!loading ? (
<>
2022-09-21 21:25:24 -07:00
<button
onClick={onClose}
2022-12-15 19:16:05 -08:00
className={`absolute right-4 top-4 z-40 text-th-fgd-4 focus:outline-none md:right-2 md:top-2 md:hover:text-th-active`}
2022-09-21 21:25:24 -07:00
>
2022-09-22 21:43:55 -07:00
<XMarkIcon className={`h-5 w-5`} />
2022-09-21 21:25:24 -07:00
</button>
2022-10-25 17:04:09 -07:00
<h3 className="mb-1 text-base text-th-fgd-1">{title}</h3>
<p className="text-sm text-th-fgd-3">{description}</p>
2022-09-22 21:43:55 -07:00
<div className="mt-4 flex items-center justify-between">
{stepIndex !== 0 ? (
<button
2022-10-25 17:04:09 -07:00
className="default-transition h-8 rounded-md border border-th-fgd-4 px-3 font-bold text-th-fgd-3 focus:outline-none md:hover:border-th-fgd-3 md:hover:text-th-fgd-2"
2022-09-22 21:43:55 -07:00
onClick={() => prev()}
>
Back
</button>
) : (
<div className="h-8 w-[58.25px]" />
)}
<div className="flex space-x-1.5">
{allSteps.map((s, i) => (
<div
className={`h-1 w-1 rounded-full ${
2022-11-30 19:32:32 -08:00
i === stepIndex ? 'bg-th-active' : 'bg-th-bkg-4'
2022-09-22 21:43:55 -07:00
}`}
key={s.title}
/>
))}
</div>
{stepIndex !== allSteps.length - 1 ? (
<button
2022-10-25 17:04:09 -07:00
className="default-transition h-8 rounded-md bg-th-button px-3 font-bold text-th-fgd-1 focus:outline-none md:hover:bg-th-button-hover"
2022-09-22 21:43:55 -07:00
onClick={() => next()}
>
Next
</button>
) : (
<button
className="default-transition h-8 rounded-md bg-th-button px-3 font-bold text-th-fgd-1 focus:outline-none md:hover:bg-th-button-hover"
2022-09-22 21:43:55 -07:00
onClick={onClose}
>
Finish
</button>
)}
</div>
</>
) : (
<div className="flex h-full items-center justify-center py-12">
<Loading />
</div>
)}
2022-09-21 21:25:24 -07:00
</div>
)
}
export default CustomTooltip