|
| 1 | +<script setup> |
| 2 | +/** UI */ |
| 3 | +import Button from "@/components/ui/Button.vue" |
| 4 | +import Spinner from "@/components/ui/Spinner.vue" |
| 5 | +import Tooltip from "@/components/ui/Tooltip.vue" |
| 6 | +import { Dropdown, DropdownItem, DropdownDivider } from "@/components/ui/Dropdown" |
| 7 | +
|
| 8 | +/** Services */ |
| 9 | +import { suggestChain, getAccounts, disconnect } from "@/services/keplr" |
| 10 | +import { arabica, mocha } from "@/services/chains" |
| 11 | +
|
| 12 | +/** Store */ |
| 13 | +import { useAppStore } from "@/store/app" |
| 14 | +import { useNotificationsStore } from "@/store/notifications" |
| 15 | +const appStore = useAppStore() |
| 16 | +const notificationsStore = useNotificationsStore() |
| 17 | +
|
| 18 | +const router = useRouter() |
| 19 | +
|
| 20 | +const isWalletAvailable = ref(false) |
| 21 | +const isFetchingAccounts = ref(false) |
| 22 | +const account = ref() |
| 23 | +
|
| 24 | +const { hostname } = useRequestURL() |
| 25 | +
|
| 26 | +switch (hostname) { |
| 27 | + case "dev.celenium.io": |
| 28 | + case "arabica.celenium.io": |
| 29 | + case "celenium.io": |
| 30 | + case "localhost": |
| 31 | + appStore.network = arabica |
| 32 | + break |
| 33 | +
|
| 34 | + case "mocha.celenium.io": |
| 35 | + case "mocha-4.celenium.io": |
| 36 | + appStore.network = mocha |
| 37 | + break |
| 38 | +} |
| 39 | +
|
| 40 | +const getBalance = async () => { |
| 41 | + const key = await window.keplr.getKey(appStore.network.chainId) |
| 42 | +
|
| 43 | + if (key) { |
| 44 | + const uri = `${appStore.network.rest}/cosmos/bank/v1beta1/balances/${key.bech32Address}?pagination.limit=1000` |
| 45 | +
|
| 46 | + const data = await $fetch(uri) |
| 47 | + const celestiaBalance = data.balances.find((balance) => balance.denom === "utia") |
| 48 | +
|
| 49 | + appStore.balance = parseFloat(celestiaBalance.amount / 1_000_000) || 0 |
| 50 | + } |
| 51 | +} |
| 52 | +
|
| 53 | +onMounted(async () => { |
| 54 | + isWalletAvailable.value = !!window.keplr |
| 55 | +}) |
| 56 | +
|
| 57 | +const handleConnect = async () => { |
| 58 | + try { |
| 59 | + await suggestChain(appStore.network) |
| 60 | +
|
| 61 | + isFetchingAccounts.value = true |
| 62 | +
|
| 63 | + const accounts = await getAccounts(appStore.network) |
| 64 | + if (accounts.length) { |
| 65 | + account.value = accounts[0].address |
| 66 | + appStore.address = accounts[0].address |
| 67 | + } |
| 68 | +
|
| 69 | + getBalance() |
| 70 | +
|
| 71 | + isFetchingAccounts.value = false |
| 72 | + } catch (error) { |
| 73 | + switch (error.message) { |
| 74 | + case "Request rejected": |
| 75 | + notificationsStore.create({ |
| 76 | + notification: { |
| 77 | + type: "info", |
| 78 | + icon: "close", |
| 79 | + title: "Request rejected", |
| 80 | + description: "You canceled the Keplr wallet request", |
| 81 | + autoDestroy: true, |
| 82 | + }, |
| 83 | + }) |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +
|
| 89 | +const handleCopy = (target) => { |
| 90 | + window.navigator.clipboard.writeText(target) |
| 91 | +
|
| 92 | + notificationsStore.create({ |
| 93 | + notification: { |
| 94 | + type: "info", |
| 95 | + icon: "check", |
| 96 | + title: "Successfully copied to clipboard", |
| 97 | + autoDestroy: true, |
| 98 | + }, |
| 99 | + }) |
| 100 | +} |
| 101 | +
|
| 102 | +const handleDisconnect = () => { |
| 103 | + disconnect() |
| 104 | +
|
| 105 | + account.value = null |
| 106 | + appStore.address = "" |
| 107 | + appStore.balance = 0 |
| 108 | +
|
| 109 | + notificationsStore.create({ |
| 110 | + notification: { |
| 111 | + type: "info", |
| 112 | + icon: "check", |
| 113 | + title: "Successfully disconnected", |
| 114 | + autoDestroy: true, |
| 115 | + }, |
| 116 | + }) |
| 117 | +} |
| 118 | +</script> |
| 119 | + |
| 120 | +<template> |
| 121 | + <Tooltip v-if="isFetchingAccounts" position="end"> |
| 122 | + <Button type="secondary" size="small" disabled> |
| 123 | + <Spinner size="14" /> |
| 124 | + Fetching |
| 125 | + </Button> |
| 126 | + |
| 127 | + <template #content> |
| 128 | + <Flex direction="column" align="end" gap="6"> |
| 129 | + <Text>Receiving your accounts </Text> |
| 130 | + <Text color="tertiary" height="120" align="right" style="max-width: 200px"> |
| 131 | + It's stuck? Try disabling the connection through your wallet and refresh the page |
| 132 | + </Text> |
| 133 | + <Text color="tertiary" height="120" align="right" style="max-width: 200px"> |
| 134 | + Sometimes the wallet pop-up may hide behind the browser window |
| 135 | + </Text> |
| 136 | + </Flex> |
| 137 | + </template> |
| 138 | + </Tooltip> |
| 139 | + |
| 140 | + <Tooltip v-else-if="!isWalletAvailable" position="end"> |
| 141 | + <Button type="white" size="small" disabled> Connect </Button> |
| 142 | + |
| 143 | + <template #content> Insall Keplr Wallet before connection </template> |
| 144 | + </Tooltip> |
| 145 | + |
| 146 | + <Button v-else-if="!account" @click="handleConnect" type="white" size="small"> Connect </Button> |
| 147 | + |
| 148 | + <Dropdown v-else> |
| 149 | + <Button type="secondary" size="small"> |
| 150 | + <Icon name="address" size="14" color="primary" /> |
| 151 | + {{ appStore.balance }} TIA |
| 152 | + </Button> |
| 153 | + |
| 154 | + <template #popup> |
| 155 | + <DropdownItem @click="router.push(`/address/${appStore.address}`)"> |
| 156 | + <Flex direction="column" gap="6"> |
| 157 | + <Text>Open my address</Text> |
| 158 | + <Text size="12" color="tertiary">celestia...{{ appStore.address.slice(-4) }}</Text> |
| 159 | + </Flex> |
| 160 | + </DropdownItem> |
| 161 | + <DropdownItem @click="handleCopy(appStore.address)"> |
| 162 | + <Text>Copy address</Text> |
| 163 | + </DropdownItem> |
| 164 | + <DropdownDivider /> |
| 165 | + <DropdownItem @click="handleDisconnect">Disconnect</DropdownItem> |
| 166 | + </template> |
| 167 | + </Dropdown> |
| 168 | +</template> |
0 commit comments