Skip to content

Commit

Permalink
Improve window.location.hash change performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pindaroso committed Oct 16, 2024
1 parent 7210ead commit f9b6c7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/app/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useTheme } from 'next-themes'
import { Sun, Moon } from 'lucide-react'
import toast from 'react-hot-toast'
import Link from 'next/link'
import { useParams } from 'next/navigation'

import {
Tooltip,
Expand Down Expand Up @@ -46,6 +47,7 @@ export default function Home() {
const { publicKey, disconnect } = useWallet()
const { setVisible } = useWalletModal()
const { setTheme, theme } = useTheme()
const params = useParams()

const { endpoint, setEndpoint } = useWalletContext()

Expand All @@ -54,6 +56,7 @@ export default function Home() {
const [network, setNetwork] = useState(defaultNetwork)
const [networkActive, setNetworkActive] = useState(false)
const [walletConnected, setWalletConnected] = useState(false)
const [hash, setHash] = useState(window.location.hash)

useEffect(() => {
if (publicKey && !walletConnected) {
Expand Down Expand Up @@ -91,6 +94,10 @@ export default function Home() {
}
}, [publicKey, endpoint]) // Run effect when publicKey or endpoint changes

useEffect(() => {
setHash(window.location.hash)
}, [params])

return (
<div className="flex flex-col min-h-screen">
<nav className="p-4">
Expand Down Expand Up @@ -222,27 +229,27 @@ export default function Home() {
</nav>
<main className="flex-grow">
<div className="flex flex-col max-w-md mx-auto p-4 gap-2">
{window.location.hash === '#/cost' && (
{hash === '#/cost' && (
<CalculateCostButton className="flex flex-col mt-2" />
)}
{window.location.hash === '#/faucet' && (
{hash === '#/faucet' && (
<AirdropButton className="flex flex-col mt-2" />
)}
{window.location.hash === '#/mint' && (
{hash === '#/mint' && (
<>
<CreateMintButton className="flex flex-col mt-2" />
<SendButton className="flex flex-col mt-2" />
</>
)}
{window.location.hash === '#/counter' && (
{hash === '#/counter' && (
<>
<CreateCounterButton className="flex flex-col mt-2" />
<IncrementCounterButton className="flex flex-col mt-2" />
<DecrementCounterButton className="flex flex-col mt-2" />
<DeleteCounterButton className="flex flex-col mt-2" />
</>
)}
{window.location.hash === '' && (
{hash === '' && (
<div className="flex flex-col mt-2">
<h1 className="text-primary text-xl text-center">
Solana ZK Starter
Expand Down
1 change: 1 addition & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9b6c7e

Please sign in to comment.