|
| 1 | +import React, { useEffect, useState, useTransition } from "react"; |
| 2 | + |
| 3 | +import _map from "lodash/map"; |
| 4 | +import _debounce from "lodash/debounce"; |
| 5 | + |
| 6 | +import { Charts, Clock, Options } from "./components"; |
| 7 | + |
| 8 | +import { getStreamData } from "./utils"; |
| 9 | +import { OPTIONS, INITIAL_STATE, STRATEGY, QUESTION_MARK } from "./constants"; |
| 10 | + |
| 11 | +import "./index.css"; |
| 12 | + |
| 13 | +let _ignoreClick = null; |
| 14 | + |
| 15 | +const App = () => { |
| 16 | + const [showDemo, setShowDemo] = useState(INITIAL_STATE.showDemo); |
| 17 | + const [strategy, setStrategy] = useState(INITIAL_STATE.strategy); |
| 18 | + const [value, setValue] = useState(INITIAL_STATE.value); |
| 19 | + const [showClock, setShowClock] = useState(INITIAL_STATE.showClock); |
| 20 | + const [isPending, startTransition] = useTransition(); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + window.addEventListener("keydown", showClockEventHandler); |
| 24 | + return () => { |
| 25 | + window.removeEventListener("keydown", showClockEventHandler); |
| 26 | + }; |
| 27 | + }, []); |
| 28 | + |
| 29 | + const showClockEventHandler = (e) => { |
| 30 | + if (e.key.toLowerCase() === QUESTION_MARK) { |
| 31 | + e.preventDefault(); |
| 32 | + setShowClock((prev) => !prev); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + const handleChartClick = (e) => { |
| 37 | + if (showDemo) { |
| 38 | + if (e.shiftKey) { |
| 39 | + setShowDemo(false); |
| 40 | + } |
| 41 | + return; |
| 42 | + } |
| 43 | + if (strategy !== STRATEGY.ASYNC) { |
| 44 | + setShowDemo((prev) => !prev); |
| 45 | + return; |
| 46 | + } |
| 47 | + if (_ignoreClick) { |
| 48 | + return; |
| 49 | + } |
| 50 | + _ignoreClick = true; |
| 51 | + |
| 52 | + startTransition(() => { |
| 53 | + setShowDemo(true); |
| 54 | + _ignoreClick = false; |
| 55 | + }); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleChange = (e) => { |
| 59 | + const val = e.target.value; |
| 60 | + switch (strategy) { |
| 61 | + case STRATEGY.SYNC: |
| 62 | + setValue(val); |
| 63 | + break; |
| 64 | + case STRATEGY.DEBOUNCED: |
| 65 | + debouncedHandleChange(val); |
| 66 | + break; |
| 67 | + case STRATEGY.ASYNC: |
| 68 | + startTransition(() => { |
| 69 | + setValue(val); |
| 70 | + }); |
| 71 | + break; |
| 72 | + default: |
| 73 | + break; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + const debouncedHandleChange = _debounce((val) => { |
| 78 | + if (strategy === STRATEGY.DEBOUNCED) { |
| 79 | + setValue(val); |
| 80 | + } |
| 81 | + }, 1000); |
| 82 | + |
| 83 | + return ( |
| 84 | + <div className="container"> |
| 85 | + <div className="rendering"> |
| 86 | + {_map(OPTIONS, (option) => ( |
| 87 | + <Options |
| 88 | + {...option} |
| 89 | + key={option.strategy} |
| 90 | + setStrategy={setStrategy} |
| 91 | + currentStrategy={strategy} |
| 92 | + /> |
| 93 | + ))} |
| 94 | + </div> |
| 95 | + <input |
| 96 | + className={"input " + strategy} |
| 97 | + id="input" |
| 98 | + placeholder="longer input → more components and DOM nodes" |
| 99 | + defaultValue={value} |
| 100 | + onChange={handleChange} |
| 101 | + /> |
| 102 | + <div className="demo" onClick={handleChartClick}> |
| 103 | + {showDemo && ( |
| 104 | + <Charts data={getStreamData(value)} isPending={isPending} /> |
| 105 | + )} |
| 106 | + {showClock && <Clock />} |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + ); |
| 110 | +}; |
| 111 | + |
| 112 | +export default App; |
0 commit comments