Skip to content

Commit 0b0f7b1

Browse files
committed
feature: display detail for webhook.store urls
1 parent dd5c5be commit 0b0f7b1

2 files changed

Lines changed: 74 additions & 15 deletions

File tree

src/NavBar/StoreConfig/StoreConfigDialog.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export const StoreConfigInnerDialog = ({
88
storageLimit,
99
defaultTargets,
1010
accessConfig,
11+
userHasAccessToStore,
1112
}: {
1213
availableStores: { url: string; display: string }[];
1314
accessConfig: { type: "public" | "private"; sublabel: string };
1415
storageLimit?: number;
1516
defaultTargets?: string[];
17+
userHasAccessToStore: boolean;
1618
}) => {
1719
return (
1820
<>
@@ -37,6 +39,10 @@ export const StoreConfigInnerDialog = ({
3739
)}
3840
</div>
3941

42+
{userHasAccessToStore
43+
? "You have access to this store"
44+
: "You don't have access to this store"}
45+
4046
<Heading>
4147
<h1>Your private webhooks stores</h1>
4248
</Heading>
@@ -60,6 +66,14 @@ export const StoreConfigInnerDialog = ({
6066
</a>
6167
</Link>
6268
</P>
69+
70+
<P>
71+
<Link>
72+
<a href="https://www.openwebhook.io/docs/intro/#%EF%B8%8F-public-organisation-membership">
73+
You don't see your organisation here?
74+
</a>
75+
</Link>
76+
</P>
6377
</>
6478
);
6579
};

src/NavBar/StoreConfig/StoreConfigNavItem.tsx

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ import { WebhookStoreUrlContext } from "../WebhookStoreUrl/WebhookStoreUrl.conte
1010
import { ACCESS_TOKEN_KEY, IDENTITY_TOKEN_KEY } from "../../local-storage";
1111
import { decodeJWT } from "../../utils/decode-jwt";
1212

13+
export type AuthMetadata =
14+
| { protected: true; protectionRule: "hostname webhook.store" }
15+
| { protected: true; protectionRule: "github-org"; ghOrg: string }
16+
| { protected: false };
17+
1318
export const StoreConfigNavItem = () => {
1419
const [isClicked, setClicked] = useState<boolean>(false);
1520

16-
const [authConfig, setAuthConfig] = useState<{ protected: boolean }>({
21+
const [authConfig, setAuthConfig] = useState<AuthMetadata>({
1722
protected: false,
1823
});
1924
const [storeConfig, setStoreConfig] = useState<{
2025
maxNumberOfWebhookPerHost?: number;
2126
defaultTarget?: string[];
22-
}>({});
27+
userHasAccessToStore: boolean;
28+
}>({ userHasAccessToStore: false });
2329

2430
const { value: webhookStoreUrl } = useContext(WebhookStoreUrlContext);
2531
const accessToken = localStorage.getItem(ACCESS_TOKEN_KEY);
@@ -32,32 +38,29 @@ export const StoreConfigNavItem = () => {
3238
decodeJWT<{ name: string; ghOrganisations: string[] }, any>(idToken);
3339

3440
useEffect(() => {
35-
getAuthConfig();
36-
getStoreConfig();
41+
getConfigs();
3742
}, []);
3843

39-
async function getAuthConfig() {
44+
async function getConfigs() {
4045
const initialiseAuthConfig = await get("auth-metadata");
4146
if (response.ok) setAuthConfig(initialiseAuthConfig);
42-
}
43-
44-
async function getStoreConfig() {
4547
const initialiseStoreConfig = await get("store-metadata");
46-
if (response.ok) setStoreConfig(initialiseStoreConfig);
48+
if (response.ok)
49+
setStoreConfig({ ...initialiseStoreConfig, userHasAccessToStore: true });
4750
}
4851

49-
const accessConfig = {
50-
type: authConfig.protected ? "private" : "public",
51-
sublabel: authConfig.protected ? "Only you" : "Anyone with the link",
52-
} as const;
52+
const accessConfig = describeAccessFromAuthConfig(
53+
authConfig,
54+
webhookStoreUrl
55+
);
5356
const availableStores = identityToken
5457
? [
5558
{
5659
url: `https://${identityToken.payload.name}.github-org.webhook.store/?access_token=${idToken}`,
5760
display: `${identityToken.payload.name}.github-org.webhook.store`,
5861
},
5962
...identityToken.payload.ghOrganisations.map((orgName) => ({
60-
url: `https://${orgName}.github-org.webhook.store/?access_token=${idToken}`,
63+
url: `https://${orgName}.github.webhook.store/?access_token=${idToken}`,
6164
display: `${orgName}.github-org.webhook.store`,
6265
})),
6366
]
@@ -79,6 +82,7 @@ export const StoreConfigNavItem = () => {
7982
availableStores={availableStores}
8083
defaultTargets={defaultTargets}
8184
storageLimit={storageLimit}
85+
userHasAccessToStore={storeConfig.userHasAccessToStore}
8286
/>
8387
</Dialog>
8488
}
@@ -88,8 +92,49 @@ export const StoreConfigNavItem = () => {
8892
appearance={Button.appearances.flat}
8993
onClick={(_) => setClicked(!isClicked)}
9094
>
91-
<Label size={Label.sizes.xSmall}>Store Config ⚠️</Label>
95+
<Label size={Label.sizes.xSmall}>
96+
Store Config {authConfig.protected ? null : "⚠️"}
97+
</Label>
9298
</Button>
9399
</Below>
94100
);
95101
};
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

Comments
 (0)