@@ -19,10 +19,8 @@ import "./App.css";
1919import AppFooter from "./components/App/AppFooter" ;
2020import AppHeader from "./components/App/AppHeader" ;
2121import { Container , Section , Bar } from "@column-resizer/react" ;
22- import { RefreshCw } from "react-feather" ;
2322import { decompressSharedTd } from "./share" ;
2423import { editor } from "monaco-editor" ;
25- import BaseButton from "./components/TDViewer/base/BaseButton" ;
2624import ErrorDialog from "./components/Dialogs/ErrorDialog" ;
2725import DialogTemplate from "./components/Dialogs/DialogTemplate" ;
2826
@@ -49,17 +47,12 @@ const BREAKPOINTS = {
4947 SMALL : 850 ,
5048} ;
5149
52- // The useEffect hook for checking the URI was called twice somehow.
53- // This variable prevents the callback from being executed twice.
54- let checkedUrl = false ;
50+ const APP_TMC_UI_ORIGIN = "http://localhost:5175" ;
5551
56- const APP_TMC_UI_ORIGIN = "http://localhost:5174" ;
57-
58- const App : React . FC = ( ) => {
52+ const App = ( ) => {
5953 const context = useContext ( ediTDorContext ) ;
6054
6155 const editorRef = useRef < editor . IStandaloneCodeEditor | null > ( null ) ;
62- const [ doShowJSON , setDoShowJSON ] = useState ( false ) ;
6356 const [ customBreakpointsState , setCustomBreakpointsState ] = useState ( 0 ) ;
6457 const tdViewerRef = useRef < HTMLDivElement > ( null ) ;
6558 const [ pendingTd , setPendingTd ] = useState < string > ( "" ) ;
@@ -90,8 +83,18 @@ const App: React.FC = () => {
9083 }
9184 } ;
9285
93- const handleToggleJSON = ( ) => {
94- setDoShowJSON ( ( prev ) => ! prev ) ;
86+ const isLoadTdMessage = ( value : unknown ) : value is LoadTdMessage => {
87+ if ( typeof value !== "object" || value === null ) {
88+ return false ;
89+ }
90+
91+ const message = value as Record < string , unknown > ;
92+
93+ return (
94+ message . type === "LOAD_TD" &&
95+ typeof message . description === "string" &&
96+ typeof message . payload === "string"
97+ ) ;
9598 } ;
9699
97100 useEffect ( ( ) => {
@@ -109,24 +112,28 @@ const App: React.FC = () => {
109112 processedValue = value + "/" ;
110113 }
111114
112- localStorage . setItem ( param , processedValue ) ;
115+ try {
116+ localStorage . setItem ( param , processedValue ) ;
117+ } catch {
118+ showError ( "Failed to persist URL parameters to local storage." ) ;
119+ }
113120 }
114121 } ) ;
115- } , [ window . location . search ] ) ;
122+ } , [ ] ) ;
116123
117124 useEffect ( ( ) => {
118- if (
119- checkedUrl ||
120- ( window . location . search . indexOf ( "td" ) <= - 1 &&
121- window . location . search . indexOf ( "proxyEndpoint" ) <= - 1 &&
122- window . location . search . indexOf ( "localstorage" ) <= - 1 &&
123- window . location . search . indexOf ( "southboundTdId" ) <= - 1 )
124- ) {
125+ const url = new URL ( window . location . href ) ;
126+
127+ const hasRelevantParam =
128+ url . searchParams . has ( "td" ) ||
129+ url . searchParams . has ( "proxyEndpoint" ) ||
130+ url . searchParams . has ( "localstorage" ) ||
131+ url . searchParams . has ( "southboundTdId" ) ;
132+
133+ if ( ! hasRelevantParam ) {
125134 return ;
126135 }
127- checkedUrl = true ;
128136
129- const url = new URL ( window . location . href ) ;
130137 const compressedTd = url . searchParams . get ( "td" ) ;
131138 if ( compressedTd !== null ) {
132139 const td = decompressSharedTd ( compressedTd ) ;
@@ -141,23 +148,23 @@ const App: React.FC = () => {
141148 }
142149
143150 if ( url . searchParams . has ( "localstorage" ) ) {
144- let td = localStorage . getItem ( "td" ) ;
145- if ( ! td ) {
151+ const storedTd = localStorage . getItem ( "td" ) ;
152+ if ( ! storedTd ) {
146153 showError ( "Request to read TD from local storage failed." ) ;
147154 return ;
148155 }
149156
150157 try {
151- td = JSON . parse ( td ) ;
152- context . updateOfflineTD ( JSON . stringify ( td , null , 2 ) ) ;
153- } catch ( e ) {
154- context . updateOfflineTD ( td ?? "" ) ;
158+ const parsedTd : ThingDescription = JSON . parse ( storedTd ) ;
159+ context . updateOfflineTD ( JSON . stringify ( parsedTd , null , 2 ) ) ;
160+ } catch ( error ) {
161+ context . updateOfflineTD ( storedTd ) ;
155162 showError (
156- `Tried to JSON parse the TD from local storage, but failed: ${ e } `
163+ `Tried to JSON parse the TD from local storage, but failed: ${ error } `
157164 ) ;
158165 }
159166 }
160- } , [ context ] ) ;
167+ } , [ ] ) ;
161168
162169 useEffect ( ( ) => {
163170 if ( ! tdViewerRef . current ) return ;
@@ -189,18 +196,18 @@ const App: React.FC = () => {
189196 return ;
190197 }
191198
192- if ( ( event . data as LoadTdMessage ) . type !== "LOAD_TD" ) {
199+ if ( event . source !== window . opener ) {
193200 return ;
194201 }
195202
196- if ( typeof ( event . data as LoadTdMessage ) . payload !== "string" ) {
203+ if ( ! isLoadTdMessage ( event . data ) ) {
197204 return ;
198205 }
199206
200207 try {
201- JSON . parse ( ( event . data as LoadTdMessage ) . payload ) ;
202- setPendingTitle ( ( event . data as LoadTdMessage ) . description ) ;
203- setPendingTd ( ( event . data as LoadTdMessage ) . payload ) ;
208+ JSON . parse ( event . data . payload ) ;
209+ setPendingTitle ( event . data . description ) ;
210+ setPendingTd ( event . data . payload ) ;
204211 setIsOpen ( true ) ;
205212 } catch {
206213 showError ( "Received invalid JSON from the other application." ) ;
@@ -253,15 +260,6 @@ const App: React.FC = () => {
253260 < Section className = "w-full md:w-5/12" >
254261 < JsonEditor editorRef = { editorRef } />
255262 </ Section >
256-
257- < BaseButton
258- type = "button"
259- className = "fixed bottom-12 right-2 z-10 rounded-full bg-blue-500 p-4"
260- onClick = { handleToggleJSON }
261- variant = "empty"
262- >
263- < RefreshCw color = "white" />
264- </ BaseButton >
265263 </ Container >
266264 </ div >
267265 < div className = "fixed bottom-0 w-screen" >
0 commit comments