From f8ec306ee97198d25f96ecc8b232bdae196c33e2 Mon Sep 17 00:00:00 2001 From: mantikoros <95266179+mantikoros@users.noreply.github.com> Date: Wed, 5 Oct 2022 18:20:40 -0500 Subject: [PATCH] Map fixes (#1011) * usa map: fix sizing * useSetIframeBackbroundColor * preload contracts * seo --- web/components/usa-map/state-election-map.tsx | 37 +++++----- web/components/usa-map/usa-map.tsx | 67 +++++++++---------- web/pages/midterms.tsx | 53 ++++++++++++++- 3 files changed, 99 insertions(+), 58 deletions(-) diff --git a/web/components/usa-map/state-election-map.tsx b/web/components/usa-map/state-election-map.tsx index 5e5a9a64..ac642fa0 100644 --- a/web/components/usa-map/state-election-map.tsx +++ b/web/components/usa-map/state-election-map.tsx @@ -3,12 +3,9 @@ import Router from 'next/router' import { useEffect, useState } from 'react' import { getProbability } from 'common/calculate' -import { Contract, CPMMBinaryContract } from 'common/contract' +import { CPMMBinaryContract } from 'common/contract' import { Customize, USAMap } from './usa-map' -import { - getContractFromSlug, - listenForContract, -} from 'web/lib/firebase/contracts' +import { listenForContract } from 'web/lib/firebase/contracts' import { interpolateColor } from 'common/util/color' export interface StateElectionMarket { @@ -18,10 +15,14 @@ export interface StateElectionMarket { state: string } -export function StateElectionMap(props: { markets: StateElectionMarket[] }) { +export function StateElectionMap(props: { + markets: StateElectionMarket[] + contracts: CPMMBinaryContract[] +}) { const { markets } = props + const [contracts, setContracts] = useState(props.contracts) + useUpdateContracts(contracts, setContracts) - const contracts = useContracts(markets.map((m) => m.slug)) const probs = contracts.map((c) => c ? getProbability(c as CPMMBinaryContract) : 0.5 ) @@ -50,29 +51,23 @@ const probToColor = (prob: number, isWinRepublican: boolean) => { return interpolateColor('#ebe4ec', color, Math.abs(p - 0.5) * 2) } -const useContracts = (slugs: string[]) => { - const [contracts, setContracts] = useState<(Contract | undefined)[]>( - slugs.map(() => undefined) - ) - - useEffect(() => { - Promise.all(slugs.map((slug) => getContractFromSlug(slug))).then( - (contracts) => setContracts(contracts) - ) - }, [slugs]) - +const useUpdateContracts = ( + contracts: CPMMBinaryContract[], + setContracts: (newContracts: CPMMBinaryContract[]) => void +) => { useEffect(() => { if (contracts.some((c) => c === undefined)) return // listen to contract updates - const unsubs = (contracts as Contract[]).map((c, i) => + const unsubs = contracts.map((c, i) => listenForContract( c.id, - (newC) => newC && setContracts(setAt(contracts, i, newC)) + (newC) => + newC && setContracts(setAt(contracts, i, newC as CPMMBinaryContract)) ) ) return () => unsubs.forEach((u) => u()) - }, [contracts]) + }, [contracts, setContracts]) return contracts } diff --git a/web/components/usa-map/usa-map.tsx b/web/components/usa-map/usa-map.tsx index c372397e..d8d0128b 100644 --- a/web/components/usa-map/usa-map.tsx +++ b/web/components/usa-map/usa-map.tsx @@ -1,6 +1,7 @@ // https://github.com/jb-1980/usa-map-react // MIT License +import clsx from 'clsx' import { DATA } from './data' import { USAState } from './usa-state' @@ -65,40 +66,38 @@ export const USAMap = ({ const stateClickHandler = (state: string) => customize?.[state]?.clickHandler return ( -