Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(KNO-7528): add MsTeamsAuthButton #339

Merged
merged 17 commits into from
Jan 10, 2025
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
7 changes: 7 additions & 0 deletions .changeset/khaki-ways-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@knocklabs/react-core": minor
"@knocklabs/client": minor
"@knocklabs/react": minor
---

feat: add MsTeamsAuthButton
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build:packages": "turbo build --filter=\"./packages/*\"",
"dev": "turbo dev",
"dev:next-example": "turbo dev --filter=\"./packages/*\" --filter=nextjs-example",
"dev:packages": "turbo dev --filter=\"./packages/*\"",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personally, I find this yarn run dev:packages command useful, but I’m happy to remove it if others don’t find it useful.

"lint": "turbo lint",
"format": "turbo format",
"format:check": "turbo format:check",
Expand Down
60 changes: 60 additions & 0 deletions packages/client/src/clients/ms-teams/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ApiResponse } from "../../api";
import { AuthCheckInput, RevokeAccessTokenInput } from "../../interfaces";
import Knock from "../../knock";
import { TENANT_OBJECT_COLLECTION } from "../objects/constants";

class MsTeamsClient {
private instance: Knock;

constructor(instance: Knock) {
this.instance = instance;
}

async authCheck({ tenant: tenantId, knockChannelId }: AuthCheckInput) {
const result = await this.instance.client().makeRequest({
method: "GET",
url: `/v1/providers/ms-teams/${knockChannelId}/auth_check`,
params: {
ms_teams_tenant_object: {
object_id: tenantId,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
},
});

return this.handleResponse(result);
}

async revokeAccessToken({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, there’s no way to actually revoke an access token used with the Microsoft Graph API, but calling this function revokeAccessToken keeps the naming consistent with SlackKit.

tenant: tenantId,
knockChannelId,
}: RevokeAccessTokenInput) {
const result = await this.instance.client().makeRequest({
method: "PUT",
url: `/v1/providers/ms-teams/${knockChannelId}/revoke_access`,
params: {
ms_teams_tenant_object: {
object_id: tenantId,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
},
});

return this.handleResponse(result);
}

private handleResponse(response: ApiResponse) {
if (response.statusCode === "error") {
if (response.error?.response?.status < 500) {
return response.error || response.body;
}
throw new Error(response.error || response.body);
}

return response.body;
}
}

export default MsTeamsClient;
17 changes: 6 additions & 11 deletions packages/client/src/clients/slack/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { ApiResponse } from "../../api";
import { AuthCheckInput, RevokeAccessTokenInput } from "../../interfaces";
import Knock from "../../knock";
import { TENANT_OBJECT_COLLECTION } from "../objects/constants";

import {
AuthCheckInput,
GetSlackChannelsInput,
GetSlackChannelsResponse,
RevokeAccessTokenInput,
} from "./interfaces";

const TENANT_COLLECTION = "$tenants";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can replace this with the existing TENANT_OBJECT_COLLECTION constant:

export const TENANT_OBJECT_COLLECTION = "$tenants";

import { GetSlackChannelsInput, GetSlackChannelsResponse } from "./interfaces";

class SlackClient {
private instance: Knock;
Expand All @@ -24,7 +19,7 @@ class SlackClient {
params: {
access_token_object: {
object_id: tenant,
collection: TENANT_COLLECTION,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
},
Expand All @@ -45,7 +40,7 @@ class SlackClient {
params: {
access_token_object: {
object_id: tenant,
collection: TENANT_COLLECTION,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
query_options: {
Expand All @@ -68,7 +63,7 @@ class SlackClient {
params: {
access_token_object: {
object_id: tenant,
collection: TENANT_COLLECTION,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
},
Expand Down
10 changes: 0 additions & 10 deletions packages/client/src/clients/slack/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ export type GetSlackChannelsInput = {
};
};

export type AuthCheckInput = {
tenant: string;
knockChannelId: string;
};

export type RevokeAccessTokenInput = {
tenant: string;
knockChannelId: string;
};
Comment on lines -20 to -28
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m moving these to packages/client/src/interfaces.ts so they can be used by TeamsKit.


export type GetSlackChannelsResponse = {
slack_channels: SlackChannel[];
next_cursor: string | null;
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from "./clients/objects/constants";
export * from "./clients/preferences/interfaces";
export * from "./clients/slack";
export * from "./clients/slack/interfaces";
export * from "./clients/ms-teams";
export * from "./clients/users";
export * from "./clients/users/interfaces";
export * from "./clients/messages";
Expand Down
10 changes: 10 additions & 0 deletions packages/client/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ export interface BulkOperation {
inserted_at: string;
updated_at: string;
}

export type AuthCheckInput = {
tenant: string;
knockChannelId: string;
};

export type RevokeAccessTokenInput = {
tenant: string;
knockChannelId: string;
};
2 changes: 2 additions & 0 deletions packages/client/src/knock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { jwtDecode } from "jwt-decode";
import ApiClient from "./api";
import FeedClient from "./clients/feed";
import MessageClient from "./clients/messages";
import MsTeamsClient from "./clients/ms-teams";
import ObjectClient from "./clients/objects";
import Preferences from "./clients/preferences";
import SlackClient from "./clients/slack";
Expand All @@ -27,6 +28,7 @@ class Knock {
readonly objects = new ObjectClient(this);
readonly preferences = new Preferences(this);
readonly slack = new SlackClient(this);
readonly msTeams = new MsTeamsClient(this);
readonly user = new UserClient(this);
readonly messages = new MessageClient(this);

Expand Down
1 change: 1 addition & 0 deletions packages/react-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./modules/core";
export * from "./modules/feed";
export * from "./modules/ms-teams";
export * from "./modules/slack";
export * from "./modules/i18n";
20 changes: 20 additions & 0 deletions packages/react-core/src/modules/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,23 @@ export function slackProviderKey({
.filter((f) => f !== null && f !== undefined)
.join("-");
}

/*
Used to build a consistent key for the KnockMsTeamsProvider so that React knows when
to trigger a re-render of the context when a key property changes.
*/
export function msTeamsProviderKey({
knockMsTeamsChannelId,
tenantId,
connectionStatus,
errorLabel,
}: {
knockMsTeamsChannelId: string;
tenantId: string;
connectionStatus: string;
errorLabel: string | null;
}) {
return [knockMsTeamsChannelId, tenantId, connectionStatus, errorLabel]
.filter((f) => f !== null && f !== undefined)
.join("-");
}
10 changes: 10 additions & 0 deletions packages/react-core/src/modules/i18n/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ const en: I18nContent = {
unread: "Unread",
read: "Read",
unseen: "Unseen",
msTeamsConnect: "Connect to Microsoft Teams",
msTeamsConnected: "Connected",
msTeamsConnecting: "Connecting to Microsoft Teams…",
msTeamsConnectContainerDescription:
"Connect to get notifications in Microsoft Teams",
msTeamsDisconnect: "Disconnect",
msTeamsDisconnecting: "Disconnecting from Microsoft Teams…",
msTeamsError: "Error",
msTeamsReconnect: "Reconnect",
msTeamsTenantIdNotSet: "Microsoft Teams tenant ID not set.",
slackConnectChannel: "Connect channel",
slackChannelId: "Slack channel ID",
slackConnecting: "Connecting to Slack...",
Expand Down
9 changes: 9 additions & 0 deletions packages/react-core/src/modules/i18n/languages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export interface Translations {
readonly unread: string;
readonly read: string;
readonly unseen: string;
readonly msTeamsConnect: string;
readonly msTeamsConnected: string;
readonly msTeamsConnecting: string;
readonly msTeamsConnectContainerDescription: string;
readonly msTeamsDisconnect: string;
readonly msTeamsDisconnecting: string;
readonly msTeamsError: string;
readonly msTeamsReconnect: string;
readonly msTeamsTenantIdNotSet: string;
readonly slackConnectChannel: string;
readonly slackChannelId: string;
readonly slackConnecting: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as React from "react";
import { PropsWithChildren } from "react";

import { useKnockClient } from "../../core";
import { msTeamsProviderKey } from "../../core/utils";
import { useMsTeamsConnectionStatus } from "../hooks";
import { ConnectionStatus } from "../hooks/useMsTeamsConnectionStatus";

export interface KnockMsTeamsProviderState {
knockMsTeamsChannelId: string;
tenantId: string;
connectionStatus: ConnectionStatus;
setConnectionStatus: (connectionStatus: ConnectionStatus) => void;
errorLabel: string | null;
setErrorLabel: (label: string) => void;
actionLabel: string | null;
setActionLabel: (label: string | null) => void;
}

const MsTeamsProviderStateContext =
React.createContext<KnockMsTeamsProviderState | null>(null);

export interface KnockMsTeamsProviderProps {
knockMsTeamsChannelId: string;
tenantId: string;
}

export const KnockMsTeamsProvider: React.FC<
PropsWithChildren<KnockMsTeamsProviderProps>
> = ({ knockMsTeamsChannelId, tenantId, children }) => {
const knock = useKnockClient();

const {
connectionStatus,
setConnectionStatus,
errorLabel,
setErrorLabel,
actionLabel,
setActionLabel,
} = useMsTeamsConnectionStatus(knock, knockMsTeamsChannelId, tenantId);

return (
<MsTeamsProviderStateContext.Provider
key={msTeamsProviderKey({
knockMsTeamsChannelId,
tenantId,
connectionStatus,
errorLabel,
})}
value={{
connectionStatus,
setConnectionStatus,
errorLabel,
setErrorLabel,
actionLabel,
setActionLabel,
knockMsTeamsChannelId,
tenantId,
}}
>
{children}
</MsTeamsProviderStateContext.Provider>
);
};

export const useKnockMsTeamsClient = (): KnockMsTeamsProviderState => {
const context = React.useContext(MsTeamsProviderStateContext);
if (!context) {
throw new Error(
"useKnockMsTeamsClient must be used within a KnockMsTeamsProvider",
);
}
return context;
};
1 change: 1 addition & 0 deletions packages/react-core/src/modules/ms-teams/context/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./KnockMsTeamsProvider";
2 changes: 2 additions & 0 deletions packages/react-core/src/modules/ms-teams/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as useMsTeamsConnectionStatus } from "./useMsTeamsConnectionStatus";
export { default as useMsTeamsAuth } from "./useMsTeamsAuth";
Loading
Loading