(props: Props)
| 30 | |
| 31 | /** A diff between two images. */ |
| 32 | export function ImageDiff(props: Props) { |
| 33 | const [shrinkToFit, setShrinkToFit] = React.useState(true); |
| 34 | |
| 35 | const toggleShrinkToFit: React.ChangeEventHandler<HTMLInputElement> = e => { |
| 36 | setShrinkToFit(e.target.checked); |
| 37 | }; |
| 38 | |
| 39 | const {changePDiffMode, pdiffMode, changeImageDiffMode} = props; |
| 40 | let {imageDiffMode} = props; |
| 41 | const pair = props.filePair; |
| 42 | if (isOneSided(pair)) { |
| 43 | imageDiffMode = 'side-by-side'; // Only one that makes sense for one-sided diffs. |
| 44 | } |
| 45 | |
| 46 | const [, forceUpdate] = React.useState(0); |
| 47 | const computePerceptualDiffBox = (fp: ImageFilePair) => { |
| 48 | if (!isSameSizeImagePair(fp)) return; |
| 49 | // TODO(danvk): restructure this, it's a mess |
| 50 | (async () => { |
| 51 | const response = await fetch(`/pdiffbbox/${fp.idx}`); |
| 52 | const bbox = (await response.json()) as DiffBox; |
| 53 | const {diffData} = fp; |
| 54 | // XXX are there other fields? |
| 55 | fp.diffData = { |
| 56 | ...diffData, |
| 57 | diffBounds: bbox, |
| 58 | }; |
| 59 | console.log('forcing update'); |
| 60 | forceUpdate(n => n + 1); // tell react about this change |
| 61 | })().catch((error: unknown) => { |
| 62 | console.error(error); |
| 63 | }); |
| 64 | }; |
| 65 | |
| 66 | if (pdiffMode === 'bbox' && !pair.diffData) { |
| 67 | // XXX this might shoot off unnecessary XHRs--use a Promise! |
| 68 | computePerceptualDiffBox(pair); |
| 69 | } |
| 70 | |
| 71 | React.useEffect(() => { |
| 72 | const handleResize = () => { |
| 73 | if (shrinkToFit) forceUpdate(n => n + 1); |
| 74 | }; |
| 75 | window.addEventListener('resize', handleResize); |
| 76 | return () => { |
| 77 | window.removeEventListener('resize', handleResize); |
| 78 | }; |
| 79 | }, [shrinkToFit, forceUpdate]); |
| 80 | |
| 81 | // TODO: switch to useKey() or some such |
| 82 | React.useEffect(() => { |
| 83 | const handleKeydown = (e: KeyboardEvent) => { |
| 84 | if (!isLegitKeypress(e)) return; |
| 85 | if (e.code == 'KeyS') { |
| 86 | changeImageDiffMode('side-by-side'); |
| 87 | } else if (e.code == 'KeyB') { |
| 88 | changeImageDiffMode('blink'); |
| 89 | } else if (e.code == 'KeyP') { |
nothing calls this directly
no test coverage detected