2021-07-24 03:21:15 +00:00
|
|
|
import React, { useState, useRef, useContext } from "react";
|
2021-09-01 09:33:42 +00:00
|
|
|
import { isAddress } from "@ethersproject/address";
|
2021-07-01 18:21:40 +00:00
|
|
|
import { Link, useHistory } from "react-router-dom";
|
2021-09-01 09:33:42 +00:00
|
|
|
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";
|
2021-09-01 20:07:51 +00:00
|
|
|
import { Dialog } from "@headlessui/react";
|
2021-07-01 18:21:40 +00:00
|
|
|
import useKeyboardShortcut from "use-keyboard-shortcut";
|
2021-07-13 21:12:49 +00:00
|
|
|
import PriceBox from "./PriceBox";
|
2021-07-24 03:21:15 +00:00
|
|
|
import { RuntimeContext } from "./useRuntime";
|
2021-07-01 18:21:40 +00:00
|
|
|
|
|
|
|
const Title: React.FC = () => {
|
2021-07-24 03:21:15 +00:00
|
|
|
const { provider } = useContext(RuntimeContext);
|
2021-07-01 18:21:40 +00:00
|
|
|
const [search, setSearch] = useState<string>();
|
|
|
|
const [canSubmit, setCanSubmit] = useState<boolean>(false);
|
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
|
|
setCanSubmit(e.target.value.trim().length > 0);
|
|
|
|
setSearch(e.target.value.trim());
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSubmit: React.FormEventHandler<HTMLFormElement> = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
if (!canSubmit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
history.push(`/search?q=${search}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const searchRef = useRef<HTMLInputElement>(null);
|
|
|
|
useKeyboardShortcut(["/"], () => {
|
|
|
|
searchRef.current?.focus();
|
|
|
|
});
|
|
|
|
|
2021-09-01 20:07:51 +00:00
|
|
|
const [isScanning, setScanning] = useState<boolean>(false);
|
2021-09-01 09:33:42 +00:00
|
|
|
const onScan = () => {
|
2021-09-01 20:07:51 +00:00
|
|
|
setScanning(true);
|
2021-09-01 09:33:42 +00:00
|
|
|
};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:07:51 +00:00
|
|
|
history.push(`/search?q=${text}`);
|
2021-09-01 09:33:42 +00:00
|
|
|
setScanning(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-01 18:21:40 +00:00
|
|
|
return (
|
2021-09-01 20:07:51 +00:00
|
|
|
<>
|
|
|
|
<Dialog
|
|
|
|
className="fixed z-10 inset-0 overflow-y-auto"
|
|
|
|
open={isScanning}
|
|
|
|
onClose={() => setScanning(false)}
|
|
|
|
>
|
|
|
|
<div className="flex items-center justify-center min-h-screen">
|
|
|
|
<Dialog.Overlay className="fixed inset-0 bg-black opacity-30" />
|
|
|
|
<Dialog.Title className="absolute top-0 w-full text-center bg-white text-lg">
|
|
|
|
Point an ETH address QR code to camera
|
|
|
|
</Dialog.Title>
|
|
|
|
<div className="absolute inset-0 bg-transparent rounded min-w-max max-w-3xl w-full h-screen max-h-screen m-auto">
|
2021-09-01 09:33:42 +00:00
|
|
|
<QrReader
|
2021-09-01 20:07:51 +00:00
|
|
|
className="m-auto"
|
2021-09-01 09:33:42 +00:00
|
|
|
constraints={{}}
|
|
|
|
onResult={evaluateScan}
|
|
|
|
/>
|
2021-09-01 20:07:51 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
<div className="px-9 py-2 flex justify-between items-baseline">
|
|
|
|
<Link className="self-center" to="/">
|
|
|
|
<div className="text-2xl text-link-blue font-title font-bold flex items-center space-x-2">
|
|
|
|
<img
|
|
|
|
className="rounded-full"
|
|
|
|
src="/otter.jpg"
|
|
|
|
width={32}
|
|
|
|
height={32}
|
|
|
|
alt="An otter scanning"
|
|
|
|
title="An otter scanning"
|
|
|
|
/>
|
|
|
|
<span>Otterscan</span>
|
|
|
|
</div>
|
|
|
|
</Link>
|
|
|
|
<div className="flex items-baseline space-x-3">
|
|
|
|
{provider?.network.chainId === 1 && <PriceBox />}
|
|
|
|
<form
|
|
|
|
className="flex"
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
autoComplete="off"
|
|
|
|
spellCheck={false}
|
2021-07-13 21:12:49 +00:00
|
|
|
>
|
2021-09-01 20:07:51 +00:00
|
|
|
<input
|
|
|
|
className="w-full border-t border-b border-l rounded-l focus:outline-none px-2 py-1 text-sm"
|
|
|
|
type="text"
|
|
|
|
size={60}
|
|
|
|
placeholder='Type "/" to search by address / txn hash / block number / ENS name'
|
|
|
|
onChange={handleChange}
|
|
|
|
ref={searchRef}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
className="border bg-gray-100 hover:bg-gray-200 focus:outline-none px-2 py-1 text-sm text-gray-500"
|
|
|
|
type="button"
|
|
|
|
onClick={onScan}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faQrcode} />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className="rounded-r border-t border-b border-r bg-gray-100 hover:bg-gray-200 focus:outline-none px-2 py-1 text-sm text-gray-500"
|
|
|
|
type="submit"
|
|
|
|
>
|
|
|
|
Search
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
2021-07-13 21:12:49 +00:00
|
|
|
</div>
|
2021-09-01 20:07:51 +00:00
|
|
|
</>
|
2021-07-01 18:21:40 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(Title);
|