Skip to content

Commit

Permalink
🚑 4.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wictorwilen committed Jun 28, 2022
1 parent 74bc7c3 commit 2dbb65d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 65 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [*4.0.1*] - <*2022-06-28*>

### Fixes

* Fixed an issue when the `useTeams` hook did not get the context properly

## [*4.0.0*] - <*2022-06-23*>

### Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msteams-react-base-component",
"version": "4.0.0",
"version": "4.0.1-preview",
"description": "React helper components for Microsoft 365 Teams, Office and Outlook apps",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
Expand Down
37 changes: 0 additions & 37 deletions src/useTeams.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("useTeams", () => {
jest.resetAllMocks();
jest.clearAllMocks();

window["microsoftTeams"] = {};
window.history.pushState({}, "", "/");

spyInitialize = jest.spyOn(app, "initialize");
Expand All @@ -46,23 +45,6 @@ describe("useTeams", () => {
});
});

it("Should create the useTeams hook - empty by default", async () => {
const App = () => {
const [{ inTeams }] = useTeams.useTeams({});
return (
<div>{"" + inTeams}</div>
);
};

window["microsoftTeams"] = undefined;

const { container } = render(<App />);
await waitFor(() => {
expect(spyInitialize).toBeCalledTimes(0);
expect(container.textContent).toBe("false");
});
});

it("Should return not in teams - app.initialize rejects", async () => {
spyInitialize.mockImplementation(() => {
return Promise.reject(new Error(""));
Expand Down Expand Up @@ -118,25 +100,6 @@ describe("useTeams", () => {
expect(container.textContent).toBe("true, default");
});

it("Should create the useTeams hook - not in teams", async () => {
const App = () => {
const [{ inTeams, themeString }] = useTeams.useTeams({});
return (
<div><div>{inTeams ? "true" : "false"}</div>,<div> {themeString}</div></div>
);
};

window["microsoftTeams"] = undefined;

const { container } = render(<App />);

await waitFor(() => {
expect(spyInitialize).toBeCalledTimes(0);
});

expect(container.textContent).toBe("false, default");
});

it("Should create the useTeams hook with dark theme", async () => {
const App = () => {
const [{ inTeams, themeString }] = useTeams.useTeams({ initialTheme: "dark" });
Expand Down
41 changes: 14 additions & 27 deletions src/useTeams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ import { unstable_batchedUpdates as batchedUpdates } from "react-dom";
import { app, pages } from "@microsoft/teams-js";
import { teamsDarkTheme, teamsHighContrastTheme, teamsTheme, ThemePrepared } from "@fluentui/react-northstar";

const isTeamsLibraryLoaded = (): boolean => {
// eslint-disable-next-line dot-notation
const microsoftTeamsLib = window["microsoftTeams"];

if (!microsoftTeamsLib) {
return false; // the Microsoft Teams library is for some reason not loaded
}
return true;
};

const getTheme = (): string | undefined => {
const urlParams = new URLSearchParams(window.location.search);
const theme = urlParams.get("theme");
Expand Down Expand Up @@ -74,28 +64,25 @@ export function useTeams(options?: { initialTheme?: string, setThemeHandler?: (t
// set initial theme based on options or query string
overrideThemeHandler(initialTheme);

if (isTeamsLibraryLoaded()) {
app.initialize().then(() => {
app.getContext().then(context => {
batchedUpdates(() => {
setInTeams(true);
setContext(context);
setFullScreen(context.page.isFullScreen);
});
overrideThemeHandler(context.app.theme);
app.registerOnThemeChangeHandler(overrideThemeHandler);
pages.registerFullScreenHandler((isFullScreen) => {
setFullScreen(isFullScreen);
});
}).catch(() => {
setInTeams(false);
app.initialize().then(() => {
app.getContext().then(context => {
batchedUpdates(() => {
setInTeams(true);
setContext(context);
setFullScreen(context.page.isFullScreen);
});
overrideThemeHandler(context.app.theme);
app.registerOnThemeChangeHandler(overrideThemeHandler);
pages.registerFullScreenHandler((isFullScreen) => {
setFullScreen(isFullScreen);
});
}).catch(() => {
setInTeams(false);
});
} else {
}).catch(() => {
setInTeams(false);
}
});

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down

0 comments on commit 2dbb65d

Please sign in to comment.