import React, {useState} from 'react';
import * as fdc3 from 'openfin-fdc3';
import {InterApplicationBusHooks} from 'openfin-react-hooks';

let latestChartListener;
let currentChartListener;

let latestNewsListener;
let currentNewsListener;

let windows = [];

function App() {
  const [content, setContent] = useState(undefined);
  const [newsContent, setNewsContent] = useState(undefined);

  const createWindow = async (context, windowName) => {
    const parentWindowOptions = await window.fin.Window.getCurrentSync().getOptions();
    const manifestOptions = await fetch(`${parentWindowOptions.customData.apiBaseUrl}/apps/v1/${context.appName}/app.json`, {
      method: 'GET',
    });
    const windowOptions = await manifestOptions.json();
    const winOption = {
      ...windowOptions.startup_app,
      // uuid must match that of the parent window
      uuid: "stockflux-container",
      name: windowName,
      customData: {
        symbol: context.name,
        apiBaseUrl: parentWindowOptions.customData.apiBaseUrl
      }
    };
    return await window.fin.Window.create(winOption);
  }

  const windowHandler = async (context, currentListener, latestListener, isChart) => {
    if (context && currentListener === latestListener) {
      const windowName = context.appName + '-' + context.name;
      if (!windows.find(window => window === windowName)) {
          const window = await createWindow(context, windowName);
          if (window) {

            if (isChart) {
              setContent({
                symbol: context.name,
                name: context.id.default
              });
            } else {
              setNewsContent({
                symbol: context.name,
              });
            }

            windows = [...windows, windowName];
            
            window.addListener("closed", () => {
              if (removeWindow(windowName)) {
                closeParentContainer();
              }
            })
          }
      }
    }
  }
  
  const removeWindow = (windowName) => {
    windows = windows.filter(name => name !== windowName);
    return windows.length === 0;
  }
  
  const closeParentContainer = () => {
    window.fin.Window.getCurrentSync().close(true);
  }

  currentChartListener = fdc3.addIntentListener("ViewChart", context => {
    windowHandler(context, currentChartListener, latestChartListener, true);
  });
  latestChartListener = currentChartListener;
  InterApplicationBusHooks.usePublish('stockFluxChart:' + (content ? content.symbol : ''), content);

  currentNewsListener = fdc3.addIntentListener("ViewNews", context => {
    windowHandler(context, currentNewsListener, latestNewsListener, false);
  });
  latestNewsListener = currentNewsListener;
  InterApplicationBusHooks.usePublish('stockFluxNews:' + (newsContent ? newsContent.symbol : ''), newsContent);

  return (
    <></>
  );
}

export default App;

@@ -74,157 +74,44 @@
 3';%0A
-import %7BInterApplicationBusHooks%7D from 'openfin-react-hooks';%0A%0Alet latestChartListener;%0Alet currentChartListener;%0A%0Alet latestNewsListener;%0Alet curren
+%0Alet latestChartListener;%0A%0Alet lates
 tNew
@@ -273,16 +273,63 @@
 ined);%0A%0A
+  const %7B InterApplicationBus %7D = window.fin;%0A%0A
   const 
@@ -1387,16 +1387,63 @@
 ndow) %7B%0A
+            windows = %5B...windows, windowName%5D;
 %0A       
@@ -1711,70 +1711,8 @@
    %7D
-%0A%0A            windows = %5B...windows, windowName%5D;%0A            
 %0A   
@@ -2141,24 +2141,30 @@
 ue);%0A  %7D%0A%0A  
+const 
 currentChart
@@ -2344,34 +2344,39 @@
 tChartListener;%0A
+%0A
   
+if (
 InterApplication
@@ -2374,34 +2374,82 @@
 plicationBus
-Hooks.useP
+ && content && content.symbol) %7B%0A    InterApplicationBus.p
 ublish('stoc
@@ -2466,19 +2466,8 @@
 :' +
- (content ?
  con
@@ -2477,22 +2477,16 @@
 t.symbol
- : '')
 , conten
@@ -2487,20 +2487,31 @@
 content)
-;%0A
 %0A 
+ %7D%0A  %0A  const
  current
@@ -2701,16 +2701,23 @@
 ener;%0A  
+%0A  if (
 InterApp
@@ -2731,18 +2731,74 @@
 nBus
-Hooks.useP
+ && newsContent && newsContent.symbol) %7B%0A    InterApplicationBus.p
 ubli
@@ -2822,23 +2822,8 @@
 :' +
- (newsContent ?
  new
@@ -2841,14 +2841,8 @@
 mbol
- : '')
 , ne
@@ -2851,17 +2851,20 @@
 Content)
-;
+%0A  %7D
 %0A%0A  retu

Refactor message publishing, fix bug that tried to create duplicate windows