Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Use notification v1 #60

Merged
merged 7 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openfin-react-hooks",
"version": "2.0.2",
"version": "2.0.3",
"description": "A collection of React Hooks built on top of the Openfin API",
"main": "dist/index.js",
"scripts": {
Expand Down
39 changes: 23 additions & 16 deletions src/useChildWindow.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { _Window } from "openfin/_v2/api/window/window";
import { WindowOption } from "openfin/_v2/api/window/windowOption";
import { useCallback, useEffect, useReducer, useState } from "react";
import ReactDOM from "react-dom";

import { IUseChildWindowOptions } from "../index";
import createWindow from "./utils/helpers/createWindow";
import getChildWindows from "./utils/helpers/getChildWindows";
import { injectNode, injectNodes } from "./utils/helpers/inject";
import { isWindowV1, isWindowV2 } from "./utils/helpers/isWindow";
import reducer, { INITIAL_WINDOW_STATE } from "./utils/reducers/WindowReducer";
Expand Down Expand Up @@ -42,10 +44,12 @@ export default ({
}
}, [parentDocument, injectNodes, htmlDocument]);

const reset = () => {
dispatch({ type: WINDOW_ACTION.RESET });
setHtmlDocument(null);
};
const reset = async () =>
new Promise((resolve) => {
dispatch({ type: WINDOW_ACTION.RESET });
setHtmlDocument(null);
resolve();
});

useEffect(() => {
if (childWindow.windowRef && isWindowV1(childWindow.windowRef)) {
Expand All @@ -59,7 +63,10 @@ export default ({

return () => {
if (childWindow.windowRef && isWindowV1(childWindow.windowRef)) {
childWindow.windowRef.getNativeWindow().onclose = null;
const nativeWindow = childWindow.windowRef.getNativeWindow();
if (nativeWindow) {
nativeWindow.onclose = null;
}
} else if (childWindow.windowRef && isWindowV2(childWindow.windowRef)) {
childWindow.windowRef.removeListener("closed", reset);
}
Expand Down Expand Up @@ -96,16 +103,16 @@ export default ({
}, [childWindow.state]);

const closeExistingWindow = useCallback(async () => {
const application = await fin.Application.getCurrent();
const childWindows = await application.getChildWindows();

await Promise.all(
childWindows.map((win) =>
win.identity.name && win.identity.name === name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this name check?

? win.close()
: Promise.resolve(),
),
);
const windowsToClose: Array<fin.OpenFinWindow | _Window> = await getChildWindows(version);
if (version === OpenFinJavaScriptAPIVersion.ONE) {
await Promise.all(
windowsToClose.map((windowToClose) =>
new Promise((resolve, reject) =>
windowToClose.close(true, resolve, reject))));
} else if (version === OpenFinJavaScriptAPIVersion.TWO) {
await Promise.all(
windowsToClose.map((windowToClose) => windowToClose.close()));
}
}, [name]);

const dispatchError = (error: string) => {
Expand Down Expand Up @@ -175,7 +182,7 @@ export default ({
if (childWindow.windowRef) {
await childWindow.windowRef.close();
}
reset();
await reset();
} catch (error) {
dispatchError(error);
}
Expand Down
72 changes: 47 additions & 25 deletions src/useNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
} from "react";
import ReactDOM from "react-dom";
import { IUseNotificationOptions } from "../index";
import getChildWindows from "./utils/helpers/getChildWindows";
import { injectNode, injectNodes } from "./utils/helpers/inject";
import { isWindowV1, isWindowV2 } from "./utils/helpers/isWindow";
import reducer, { INITIAL_WINDOW_STATE } from "./utils/reducers/WindowReducer";
import WINDOW_ACTION from "./utils/types/enums/WindowAction";
import WINDOW_STATE from "./utils/types/enums/WindowState";
Expand All @@ -34,7 +36,9 @@ export default ({
shouldInheritCss,
shouldInheritScripts,
}: IUseNotificationOptions) => {
const [name, setName] = useState<string | null>(null);
const version = fin.Window.getCurrentSync().getWebWindow ?
OpenFinJavaScriptAPIVersion.TWO : OpenFinJavaScriptAPIVersion.ONE;
const [windowOptions, setWindowOptions] = useState<WindowOption | null>(null);
const [htmlDocument, setHtmlDocument] = useState<HTMLDocument | null>(null);
const [populateJsx, setPopulateJsx] = useState<JSX.Element | null>(null);
const [ref, setRef] = useState<IOpenFinNotification | null>(null);
Expand All @@ -57,37 +61,53 @@ export default ({
}, [parentDocument, injectNodes, htmlDocument]);

useEffect(() => {
if (notificationWindow.windowRef) {
if (notificationWindow.windowRef && isWindowV1(notificationWindow.windowRef)) {
setHtmlDocument(
(notificationWindow.windowRef as _Window).getWebWindow().document,
notificationWindow.windowRef.getNativeWindow().document,
);
} else if (notificationWindow.windowRef && isWindowV2(notificationWindow.windowRef)) {
setHtmlDocument(
notificationWindow.windowRef.getWebWindow().document,
);
}
}, [notificationWindow.windowRef]);

useEffect(() => {
if (
name &&
windowOptions &&
ref &&
notificationWindow.state === WINDOW_STATE.LAUNCHING
) {
fin.Application.getCurrent().then(async (application) => {
const childWindows = await application.getChildWindows();
childWindows.map((win) => {
if (win.identity.name && win.identity.name === name) {
dispatch({
payload: win,
type: WINDOW_ACTION.SET_WINDOW,
});
}
});
dispatchNewState(WINDOW_STATE.LAUNCHED);
});
getChildWindows(version).then((childWindows: any[]) => {
let childWindow: _Window | fin.OpenFinWindow | null = null;
if (version === OpenFinJavaScriptAPIVersion.ONE) {
childWindow = childWindows
// A "queueCounter" window needs to be filtered out (it has the same name/uuid):
.filter((win: fin.OpenFinWindow) => win.getNativeWindow())
// This includes only notification windows.
// There doesn't seem to be a better way of differentiating child windows from notifications:
.filter((win: fin.OpenFinWindow) => win.name.includes("newNotifications"))
.find((win) => (win.uuid && win.uuid === windowOptions.uuid));
} else {
childWindow = childWindows.find((win) =>
win.identity.name && win.identity.name === windowOptions.name);
}
if (childWindow) {
dispatch({
payload: childWindow,
type: WINDOW_ACTION.SET_WINDOW,
});
dispatchNewState(WINDOW_STATE.LAUNCHED);
} else {
dispatchError("Failed to get notification window");
}
}).catch((error: Error) => { throw error; });
}
}, [name, notificationWindow.state, ref]);
}, [windowOptions, notificationWindow.state, ref]);

useEffect(() => {
if (ref && ref.noteWin) {
setName(ref.noteWin.windowOpts.name || null);
setWindowOptions(ref.noteWin.windowOpts || null);
}
}, [ref]);

Expand All @@ -100,7 +120,7 @@ export default ({
) {
populate(jsxElement);
}
}, [jsx, populateJsx, name, notificationWindow]);
}, [jsx, populateJsx, windowOptions, notificationWindow]);

useEffect(() => {
if (shouldInheritCss) {
Expand Down Expand Up @@ -153,18 +173,20 @@ export default ({
});
};

const reset = () => {
dispatch({ type: WINDOW_ACTION.RESET });
setHtmlDocument(null);
setRef(null);
};
const reset = async () =>
new Promise((resolve) => {
dispatch({ type: WINDOW_ACTION.RESET });
setHtmlDocument(null);
setRef(null);
resolve();
});

const close = useCallback(async () => {
try {
if (ref) {
await ref.close();
}
reset();
await reset();
} catch (error) {
throw new Error(error);
}
Expand Down
20 changes: 20 additions & 0 deletions src/utils/helpers/getChildWindows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { _Window } from "openfin/_v2/api/window/window";

const getChildWindowsV1 = (): Promise<fin.OpenFinWindow[]> =>
new Promise((resolve, reject) => {
const application = fin.desktop.Application.getCurrent();
application.getChildWindows(resolve, reject);
});

const getChildWindowsV2 = (): Promise<_Window[]> =>
new Promise((resolve, reject) => {
fin.Application.getCurrent().then(async (application) => {
const childWindows = await application.getChildWindows();
resolve(childWindows);
}).catch(reject);
});

export default (version: OpenFinJavaScriptAPIVersion): Promise<fin.OpenFinWindow[] | _Window[]> =>
version === OpenFinJavaScriptAPIVersion.ONE ?
getChildWindowsV1() :
getChildWindowsV2();