import React, { useState, useRef, useContext } from "react"; import { isAddress } from "@ethersproject/address"; import { Link, useHistory } from "react-router-dom"; import { QrReader } from "@blackbox-vision/react-qr-reader"; import { OnResultFunction } from "@blackbox-vision/react-qr-reader/dist-types/types"; import { BarcodeFormat } from "@zxing/library"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faQrcode } from "@fortawesome/free-solid-svg-icons/faQrcode"; import useKeyboardShortcut from "use-keyboard-shortcut"; import PriceBox from "./PriceBox"; import { RuntimeContext } from "./useRuntime"; const Title: React.FC = () => { const { provider } = useContext(RuntimeContext); const [search, setSearch] = useState(); const [canSubmit, setCanSubmit] = useState(false); const history = useHistory(); const handleChange: React.ChangeEventHandler = (e) => { setCanSubmit(e.target.value.trim().length > 0); setSearch(e.target.value.trim()); }; const handleSubmit: React.FormEventHandler = (e) => { e.preventDefault(); if (!canSubmit) { return; } history.push(`/search?q=${search}`); }; const searchRef = useRef(null); useKeyboardShortcut(["/"], () => { searchRef.current?.focus(); }); const [isScanning, setScanning] = useState(); const onScan = () => { setScanning(!isScanning); }; const evaluateScan: OnResultFunction = (result, error, codeReader) => { console.log("scan"); if (!error && result?.getBarcodeFormat() === BarcodeFormat.QR_CODE) { const text = result.getText(); console.log(`Scanned: ${text}`); if (!isAddress(text)) { console.log("Not an ETH address"); return; } if (searchRef.current) { searchRef.current.value = text; searchRef.current.dispatchEvent(new Event("input", { bubbles: true })); } setScanning(false); } }; return (
An otter scanning Otterscan
{provider?.network.chainId === 1 && }
{isScanning && ( )}
); }; export default React.memo(Title);