1- import { useState , useEffect , useRef } from "react" ;
1+ import { useState , useRef } from "react" ;
22import { styled } from "styled-components" ;
33
44import type {
@@ -16,6 +16,7 @@ import CompareTable from "./CompareTable";
1616
1717import "../results/resultsView.css" ;
1818import { assertNever } from "../../common/helpers-pure" ;
19+ import { useMessageFromExtension } from "../common/useMessageFromExtension" ;
1920
2021const Header = styled . div `
2122 display: flex;
@@ -50,115 +51,101 @@ export function Compare(_: Record<string, never>): React.JSX.Element {
5051 comparison ?. result &&
5152 ( comparison . result . to . length || comparison . result . from . length ) ;
5253
53- useEffect ( ( ) => {
54- const listener = ( evt : MessageEvent ) => {
55- if ( evt . origin === window . origin ) {
56- const msg : ToCompareViewMessage = evt . data ;
57- switch ( msg . t ) {
58- case "setComparisonQueryInfo" :
59- setQueryInfo ( msg ) ;
60- break ;
61- case "setComparisons" :
62- setComparison ( msg ) ;
63- break ;
64- case "streamingComparisonSetup" :
65- setComparison ( null ) ;
66- streamingComparisonRef . current = msg ;
67- break ;
68- case "streamingComparisonAddResults" : {
69- const prev = streamingComparisonRef . current ;
70- if ( prev === null ) {
71- console . warn (
72- 'Received "streamingComparisonAddResults" before "streamingComparisonSetup"' ,
73- ) ;
74- break ;
75- }
54+ useMessageFromExtension < ToCompareViewMessage > ( ( msg ) => {
55+ switch ( msg . t ) {
56+ case "setComparisonQueryInfo" :
57+ setQueryInfo ( msg ) ;
58+ break ;
59+ case "setComparisons" :
60+ setComparison ( msg ) ;
61+ break ;
62+ case "streamingComparisonSetup" :
63+ setComparison ( null ) ;
64+ streamingComparisonRef . current = msg ;
65+ break ;
66+ case "streamingComparisonAddResults" : {
67+ const prev = streamingComparisonRef . current ;
68+ if ( prev === null ) {
69+ console . warn (
70+ 'Received "streamingComparisonAddResults" before "streamingComparisonSetup"' ,
71+ ) ;
72+ break ;
73+ }
7674
77- if ( prev . id !== msg . id ) {
78- console . warn (
79- 'Received "streamingComparisonAddResults" with different id, ignoring' ,
80- ) ;
81- break ;
82- }
75+ if ( prev . id !== msg . id ) {
76+ console . warn (
77+ 'Received "streamingComparisonAddResults" with different id, ignoring' ,
78+ ) ;
79+ break ;
80+ }
8381
84- let result : QueryCompareResult ;
85- switch ( prev . result . kind ) {
86- case "raw" :
87- if ( msg . result . kind !== "raw" ) {
88- throw new Error (
89- "Streaming comparison: expected raw results, got interpreted results" ,
90- ) ;
91- }
92-
93- result = {
94- ...prev . result ,
95- from : [ ...prev . result . from , ...msg . result . from ] ,
96- to : [ ...prev . result . to , ...msg . result . to ] ,
97- } ;
98- break ;
99- case "interpreted" :
100- if ( msg . result . kind !== "interpreted" ) {
101- throw new Error (
102- "Streaming comparison: expected interpreted results, got raw results" ,
103- ) ;
104- }
105-
106- result = {
107- ...prev . result ,
108- from : [ ...prev . result . from , ...msg . result . from ] ,
109- to : [ ...prev . result . to , ...msg . result . to ] ,
110- } ;
111- break ;
112- default :
113- throw new Error ( "Unexpected comparison result kind" ) ;
82+ let result : QueryCompareResult ;
83+ switch ( prev . result . kind ) {
84+ case "raw" :
85+ if ( msg . result . kind !== "raw" ) {
86+ throw new Error (
87+ "Streaming comparison: expected raw results, got interpreted results" ,
88+ ) ;
11489 }
11590
116- streamingComparisonRef . current = {
117- ...prev ,
118- result,
91+ result = {
92+ ...prev . result ,
93+ from : [ ...prev . result . from , ...msg . result . from ] ,
94+ to : [ ...prev . result . to , ...msg . result . to ] ,
11995 } ;
120-
12196 break ;
122- }
123- case "streamingComparisonComplete" :
124- if ( streamingComparisonRef . current === null ) {
125- console . warn (
126- 'Received "streamingComparisonComplete" before "streamingComparisonSetup"' ,
97+ case "interpreted" :
98+ if ( msg . result . kind !== "interpreted" ) {
99+ throw new Error (
100+ "Streaming comparison: expected interpreted results, got raw results" ,
127101 ) ;
128- setComparison ( null ) ;
129- break ;
130102 }
131103
132- if ( streamingComparisonRef . current . id !== msg . id ) {
133- console . warn (
134- 'Received "streamingComparisonComplete" with different id, ignoring' ,
135- ) ;
136- break ;
137- }
138-
139- setComparison ( {
140- ...streamingComparisonRef . current ,
141- t : "setComparisons" ,
142- } ) ;
143- streamingComparisonRef . current = null ;
144- break ;
145- case "setUserSettings" :
146- setUserSettings ( msg . userSettings ) ;
104+ result = {
105+ ...prev . result ,
106+ from : [ ...prev . result . from , ...msg . result . from ] ,
107+ to : [ ...prev . result . to , ...msg . result . to ] ,
108+ } ;
147109 break ;
148110 default :
149- assertNever ( msg ) ;
111+ throw new Error ( "Unexpected comparison result kind" ) ;
150112 }
151- } else {
152- // sanitize origin
153- const origin = evt . origin . replace ( / \n | \r / g, "" ) ;
154- console . error ( `Invalid event origin ${ origin } ` ) ;
113+
114+ streamingComparisonRef . current = {
115+ ...prev ,
116+ result,
117+ } ;
118+
119+ break ;
155120 }
156- } ;
157- window . addEventListener ( "message" , listener ) ;
121+ case "streamingComparisonComplete" :
122+ if ( streamingComparisonRef . current === null ) {
123+ console . warn (
124+ 'Received "streamingComparisonComplete" before "streamingComparisonSetup"' ,
125+ ) ;
126+ setComparison ( null ) ;
127+ break ;
128+ }
129+
130+ if ( streamingComparisonRef . current . id !== msg . id ) {
131+ console . warn (
132+ 'Received "streamingComparisonComplete" with different id, ignoring' ,
133+ ) ;
134+ break ;
135+ }
158136
159- return ( ) => {
160- window . removeEventListener ( "message" , listener ) ;
161- } ;
137+ setComparison ( {
138+ ...streamingComparisonRef . current ,
139+ t : "setComparisons" ,
140+ } ) ;
141+ streamingComparisonRef . current = null ;
142+ break ;
143+ case "setUserSettings" :
144+ setUserSettings ( msg . userSettings ) ;
145+ break ;
146+ default :
147+ assertNever ( msg ) ;
148+ }
162149 } , [ ] ) ;
163150
164151 if ( ! queryInfo || ! comparison ) {
0 commit comments