Skip to content

Commit

Permalink
Display red status icon when unable to connect to network
Browse files Browse the repository at this point in the history
  • Loading branch information
pindaroso committed Oct 10, 2024
1 parent 6cc284e commit 74703de
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pnpm build
pnpm validator
```

**Stop the light validator**

```bash
pnpm validator:stop
```

**Deploy the protocol**

```bash
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "pnpm -F=protocol test",
"dev": "pnpm -F=app dev",
"validator": "light test-validator",
"validator:stop": "light test-validator --stop",
"deploy:protocol": "pnpm -F=protocol deploy:protocol",
"sync": "bin/sync.sh",
"ship": "bin/ship.sh",
Expand Down
27 changes: 20 additions & 7 deletions packages/app/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import { Button } from '@/components/ui/button'
import { useWalletContext } from '@/components/providers/wallet'
import { formatAddress } from '@/lib/utils'

const defaultNetwork =
process.env.NODE_ENV === 'development' ? 'localnet' : 'devnet'

export default function Home() {
const { publicKey, disconnect } = useWallet()
const { setVisible } = useWalletModal()
Expand All @@ -38,10 +41,11 @@ export default function Home() {

const [blockNumber, setBlockNumber] = useState<number | null>(null)
const [balance, setBalance] = useState<number | null>(null)
const [network, setNetwork] = useState<string>('localnet')
const [network, setNetwork] = useState<string>(defaultNetwork)
const [networkActive, setNetworkActive] = useState<boolean>(false)

useEffect(() => {
const conn = new Connection(endpoint, 'confirmed') // Use network state
const conn = new Connection(endpoint, 'confirmed')

const fetchBalance = async () => {
if (publicKey) {
Expand All @@ -50,9 +54,10 @@ export default function Home() {
}
}

fetchBalance() // Fetch balance on component mount
fetchBalance()

const subscriptionId = conn.onSlotChange(async (slotInfo) => {
setNetworkActive(true)
const block = await conn.getBlockTime(slotInfo.slot)
setBlockNumber(block)
await fetchBalance() // Update balance on slot change
Expand All @@ -61,7 +66,7 @@ export default function Home() {
return () => {
conn.removeSlotChangeListener(subscriptionId)
}
}, [publicKey, endpoint]) // Re-run effect when publicKey or endpoint changes
}, [publicKey, endpoint]) // Run effect when publicKey or endpoint changes

return (
<div className="flex flex-col min-h-screen">
Expand All @@ -74,7 +79,11 @@ export default function Home() {
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="w-24">
<span className="text-green-400 mr-2">&bull;</span>
{networkActive ? (
<span className="text-green-400 mr-2">&bull;</span>
) : (
<span className="text-red-400 mr-2">&bull;</span>
)}
{network}
</Button>
</DropdownMenuTrigger>
Expand Down Expand Up @@ -195,8 +204,12 @@ export default function Home() {
</div>
</main>
<footer className="p-4">
<div className="container mx-auto text-center text-sm text-green-400">
<code>{blockNumber !== null ? blockNumber : '-'}</code>
<div className="container mx-auto text-center text-sm">
{blockNumber !== null ? (
<code className="text-green-400">{blockNumber}</code>
) : (
<code>-</code>
)}
</div>
</footer>
</div>
Expand Down
9 changes: 7 additions & 2 deletions packages/app/src/components/providers/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ import {
} from '@solana/wallet-adapter-wallets'
import { WalletModalProvider } from '@solana/wallet-adapter-react-ui'

const defaultEndpoint =
process.env.NODE_ENV === 'development'
? 'http://127.0.0.1:8899'
: 'https://api.devnet.solana.com'

const WalletContext = createContext({
endpoint: 'http://127.0.0.1:8899',
endpoint: defaultEndpoint,
setEndpoint: (endpoint: string) => {},
})

export const WalletContextProvider: FC<{ children: ReactNode }> = ({
children,
}) => {
const [endpoint, setEndpoint] = useState('http://127.0.0.1:8899')
const [endpoint, setEndpoint] = useState(defaultEndpoint)

const value = {
endpoint,
Expand Down

0 comments on commit 74703de

Please sign in to comment.