|
| 1 | +import { Label } from "@pluralsight/ps-design-system-text"; |
| 2 | +import { Below } from "@pluralsight/ps-design-system-position"; |
| 3 | + |
| 4 | +import React, { useContext, useEffect, useState } from "react"; |
| 5 | +import Button from "@pluralsight/ps-design-system-button"; |
| 6 | +import { StoreConfigInnerDialog } from "./StoreConfigDialog"; |
| 7 | +import Dialog from "@pluralsight/ps-design-system-dialog"; |
| 8 | +import useFetch from "use-http"; |
| 9 | +import { WebhookStoreUrlContext } from "../WebhookStoreUrl/WebhookStoreUrl.context"; |
| 10 | +import { ACCESS_TOKEN_KEY, IDENTITY_TOKEN_KEY } from "../../local-storage"; |
| 11 | +import { decodeJWT } from "../../utils/decode-jwt"; |
| 12 | + |
| 13 | +export type AuthMetadata = |
| 14 | + | { protected: true; protectionRule: "hostname webhook.store" } |
| 15 | + | { protected: true; protectionRule: "github-org"; ghOrg: string } |
| 16 | + | { protected: false }; |
| 17 | + |
| 18 | +export const StoreConfigNavItem = () => { |
| 19 | + const [isClicked, setClicked] = useState<boolean>(false); |
| 20 | + |
| 21 | + const [authConfig, setAuthConfig] = useState<AuthMetadata>({ |
| 22 | + protected: false, |
| 23 | + }); |
| 24 | + const [storeConfig, setStoreConfig] = useState<{ |
| 25 | + maxNumberOfWebhookPerHost?: number; |
| 26 | + defaultTarget?: string[]; |
| 27 | + userHasAccessToStore: boolean; |
| 28 | + }>({ userHasAccessToStore: false }); |
| 29 | + |
| 30 | + const { value: webhookStoreUrl } = useContext(WebhookStoreUrlContext); |
| 31 | + const accessToken = localStorage.getItem(ACCESS_TOKEN_KEY); |
| 32 | + const { get, response } = useFetch(webhookStoreUrl, { |
| 33 | + headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : {}, |
| 34 | + }); |
| 35 | + const idToken = localStorage.getItem(IDENTITY_TOKEN_KEY); |
| 36 | + const identityToken = |
| 37 | + idToken && |
| 38 | + decodeJWT<{ name: string; ghOrganisations: string[] }, any>(idToken); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + getConfigs(); |
| 42 | + }, []); |
| 43 | + |
| 44 | + async function getConfigs() { |
| 45 | + const initialiseAuthConfig = await get("auth-metadata"); |
| 46 | + if (response.ok) setAuthConfig(initialiseAuthConfig); |
| 47 | + const initialiseStoreConfig = await get("store-metadata"); |
| 48 | + if (response.ok) |
| 49 | + setStoreConfig({ ...initialiseStoreConfig, userHasAccessToStore: true }); |
| 50 | + } |
| 51 | + |
| 52 | + const accessConfig = describeAccessFromAuthConfig( |
| 53 | + authConfig, |
| 54 | + webhookStoreUrl |
| 55 | + ); |
| 56 | + const availableStores = identityToken |
| 57 | + ? [ |
| 58 | + { |
| 59 | + url: `https://${identityToken.payload.name}.github-org.webhook.store/?access_token=${idToken}`, |
| 60 | + display: `${identityToken.payload.name}.github-org.webhook.store`, |
| 61 | + }, |
| 62 | + ...identityToken.payload.ghOrganisations.map((orgName) => ({ |
| 63 | + url: `https://${orgName}.github.webhook.store/?access_token=${idToken}`, |
| 64 | + display: `${orgName}.github-org.webhook.store`, |
| 65 | + })), |
| 66 | + ] |
| 67 | + : [ |
| 68 | + { |
| 69 | + url: "https://github.webhook.store", |
| 70 | + display: "github.webhook.store", |
| 71 | + }, |
| 72 | + ]; |
| 73 | + const defaultTargets = storeConfig.defaultTarget; |
| 74 | + const storageLimit = storeConfig.maxNumberOfWebhookPerHost; |
| 75 | + |
| 76 | + return ( |
| 77 | + <Below |
| 78 | + show={ |
| 79 | + <Dialog> |
| 80 | + <StoreConfigInnerDialog |
| 81 | + accessConfig={accessConfig} |
| 82 | + availableStores={availableStores} |
| 83 | + defaultTargets={defaultTargets} |
| 84 | + storageLimit={storageLimit} |
| 85 | + userHasAccessToStore={storeConfig.userHasAccessToStore} |
| 86 | + /> |
| 87 | + </Dialog> |
| 88 | + } |
| 89 | + when={isClicked} |
| 90 | + > |
| 91 | + <Button |
| 92 | + appearance={Button.appearances.flat} |
| 93 | + onClick={(_) => setClicked(!isClicked)} |
| 94 | + > |
| 95 | + <Label size={Label.sizes.xSmall}> |
| 96 | + Store Config {authConfig.protected ? null : "⚠️"} |
| 97 | + </Label> |
| 98 | + </Button> |
| 99 | + </Below> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +const describeAccessFromAuthConfig = ( |
| 104 | + authConfig: AuthMetadata, |
| 105 | + webhookStoreUrl: string |
| 106 | +): { type: "public" | "private"; sublabel: string } => { |
| 107 | + if (!authConfig.protected) { |
| 108 | + return { type: "public", sublabel: "Anyone with the link" }; |
| 109 | + } |
| 110 | + |
| 111 | + if (authConfig.protectionRule === "github-org") { |
| 112 | + return { |
| 113 | + type: "private", |
| 114 | + sublabel: `Only members of ${authConfig.ghOrg} on GitHub`, |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + if (authConfig.protectionRule === "hostname webhook.store") { |
| 119 | + const webhookStoreDomain = new URL(webhookStoreUrl).hostname; |
| 120 | + if (webhookStoreDomain.endsWith(".github.webhook.store")) { |
| 121 | + const githubUserName = |
| 122 | + webhookStoreDomain.split(".")[webhookStoreDomain.split(".").length - 4]; |
| 123 | + return { |
| 124 | + type: "private", |
| 125 | + sublabel: `Only Github user ${githubUserName}`, |
| 126 | + }; |
| 127 | + } |
| 128 | + if (webhookStoreDomain.endsWith(".github-org.webhook.store")) { |
| 129 | + const githubOrgaName = |
| 130 | + webhookStoreDomain.split(".")[webhookStoreDomain.split(".").length - 4]; |
| 131 | + return { |
| 132 | + type: "private", |
| 133 | + sublabel: `Only members of ${githubOrgaName} on GitHub`, |
| 134 | + }; |
| 135 | + } |
| 136 | + return { type: "public", sublabel: "Anyone with the link" }; |
| 137 | + } |
| 138 | + |
| 139 | + return { type: "public", sublabel: "Anyone with the link" }; |
| 140 | +}; |
0 commit comments