These pages are now client-side rendered: - /home - /leaderboards - /market/... - /fold/...
		
			
				
	
	
		
			38 lines
		
	
	
		
			924 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			924 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { useEffect, useState } from 'react'
 | |
| import {
 | |
|   Contract,
 | |
|   getContractFromSlug,
 | |
|   listenForContract,
 | |
| } from '../lib/firebase/contracts'
 | |
| 
 | |
| export const useContract = (contractId: string) => {
 | |
|   const [contract, setContract] = useState<Contract | null | 'loading'>(
 | |
|     'loading'
 | |
|   )
 | |
| 
 | |
|   useEffect(() => {
 | |
|     if (contractId) return listenForContract(contractId, setContract)
 | |
|   }, [contractId])
 | |
| 
 | |
|   return contract
 | |
| }
 | |
| 
 | |
| export const useContractWithPreload = (
 | |
|   slug: string,
 | |
|   initial: Contract | null
 | |
| ) => {
 | |
|   const [contract, setContract] = useState<Contract | null>(initial)
 | |
|   const [contractId, setContractId] = useState<string | undefined | null>(
 | |
|     initial?.id
 | |
|   )
 | |
| 
 | |
|   useEffect(() => {
 | |
|     if (contractId) return listenForContract(contractId, setContract)
 | |
| 
 | |
|     if (contractId !== null && slug)
 | |
|       getContractFromSlug(slug).then((c) => setContractId(c?.id || null))
 | |
|   }, [contractId, slug])
 | |
| 
 | |
|   return contract
 | |
| }
 |