From 64182452a20d8ab7ddbb28692065fbd9ed9f185b Mon Sep 17 00:00:00 2001 From: Jeremy Wiebe Date: Fri, 14 Jul 2023 09:08:23 -0700 Subject: [PATCH] Defer calculating picUrl to avoid accessing dependencies too early --- .../src/widgets/plotter-editor.tsx | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/perseus-editor/src/widgets/plotter-editor.tsx b/packages/perseus-editor/src/widgets/plotter-editor.tsx index cf63c2aba7..ee50a440ed 100644 --- a/packages/perseus-editor/src/widgets/plotter-editor.tsx +++ b/packages/perseus-editor/src/widgets/plotter-editor.tsx @@ -84,14 +84,11 @@ class PlotterEditor extends React.Component { plotDimensions: [275, 200], labelInterval: 1, - get picUrl() { - const staticUrl = Dependencies.getDependencies().staticUrl; - if (staticUrl) { - return staticUrl("/images/badges/earth-small.png"); - } - - return null; - }, + // CAUTION(jeremy): picUrl used to be a getter here. Please do not + // restore this getter as it used getDependencies(). That is not + // reliable as sometimes that the dependencies are not provided by the + // host application at this point causing an error. + // get picUrl() {} }; state: State = { @@ -118,18 +115,19 @@ class PlotterEditor extends React.Component { } } - fetchPic: (arg1: string) => any = (url) => { - if (this.state.loadedUrl !== url) { + fetchPic(url: string) { + const staticUrl = Dependencies.getDependencies().staticUrl(url); + if (this.state.loadedUrl !== staticUrl) { const pic = new Image(); - pic.src = url; + pic.src = staticUrl; pic.onload = () => { this.setState({ pic: pic, - loadedUrl: url, + loadedUrl: staticUrl, }); }; } - }; + } render(): React.ReactNode { const setFromScale = _.contains(