|
1 | 1 | import { Label } from "@pluralsight/ps-design-system-text"; |
2 | 2 | import { Below } from "@pluralsight/ps-design-system-position"; |
3 | 3 |
|
4 | | -import React, { useState } from "react"; |
| 4 | +import React, { useContext, useEffect, useState } from "react"; |
5 | 5 | import Button from "@pluralsight/ps-design-system-button"; |
6 | 6 | import { StoreConfigInnerDialog } from "./StoreConfigDialog"; |
7 | 7 | import Dialog from "@pluralsight/ps-design-system-dialog"; |
| 8 | +import useFetch from "use-http"; |
| 9 | +import { WebhookStoreUrlContext } from "../WebhookStoreUrl/WebhookStoreUrl.context"; |
| 10 | +import { IDENTITY_TOKEN_KEY } from "../../local-storage"; |
| 11 | +import { decodeJWT } from "../../utils/decode-jwt"; |
8 | 12 |
|
9 | 13 | export const StoreConfigNavItem = () => { |
10 | 14 | const [isClicked, setClicked] = useState<boolean>(false); |
11 | 15 |
|
12 | | - const accessConfig = { type: "public" } as const; |
13 | | - const availableStores = [{ url: "https://google.com", display: "Google" }]; |
| 16 | + const [authConfig, setAuthConfig] = useState<{ protected: boolean }>({ |
| 17 | + protected: false, |
| 18 | + }); |
| 19 | + |
| 20 | + const { value: webhookStoreUrl } = useContext(WebhookStoreUrlContext); |
| 21 | + const { get, response } = useFetch(webhookStoreUrl); |
| 22 | + const idToken = localStorage.getItem(IDENTITY_TOKEN_KEY); |
| 23 | + const identityToken = |
| 24 | + idToken && |
| 25 | + decodeJWT<{ name: string; ghOrganisations: string[] }, any>(idToken); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + getAuthConfig(); |
| 29 | + }, []); |
| 30 | + |
| 31 | + async function getAuthConfig() { |
| 32 | + const initialiseAuthConfig = await get("auth-metadata"); |
| 33 | + if (response.ok) setAuthConfig(initialiseAuthConfig); |
| 34 | + } |
| 35 | + |
| 36 | + const accessConfig = { |
| 37 | + type: authConfig.protected ? "private" : "public", |
| 38 | + sublabel: authConfig.protected ? "Only you" : "Anyone with the link", |
| 39 | + } as const; |
| 40 | + const availableStores = identityToken |
| 41 | + ? [ |
| 42 | + { |
| 43 | + url: `https://${identityToken.payload.name}.github-org.webhook.store/?access_token=${idToken}`, |
| 44 | + display: `${identityToken.payload.name}.github-org.webhook.store`, |
| 45 | + }, |
| 46 | + ...identityToken.payload.ghOrganisations.map((orgName) => ({ |
| 47 | + url: `https://${orgName}.github-org.webhook.store/?access_token=${idToken}`, |
| 48 | + display: `${orgName}.github-org.webhook.store`, |
| 49 | + })), |
| 50 | + ] |
| 51 | + : [ |
| 52 | + { |
| 53 | + url: "https://github.webhook.store", |
| 54 | + display: "github.webhook.store", |
| 55 | + }, |
| 56 | + ]; |
14 | 57 | const defaultTargets = ["https://google.com", "https://google.com"]; |
15 | 58 | const storageLimit = 100; |
16 | 59 |
|
|
0 commit comments