-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
126 lines (116 loc) · 5.37 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, {useCallback, useEffect, useState} from 'react';
import {
accessTokenControlsTagAvailable,
accessTokenControlsTagFetching,
accessTokenControlsTagLoggedIn,
accessTokenControlsTagLoggedOut,
accessTokenControlsTagLoggingIn,
useAccessToken,
useAccessTokenControls
} from "azure/devops/react/authentication/AccessToken";
import {FullscreenCenter} from "./component/layout/FullscreenCenter";
import {ButtonBox} from "./component/layout/ButtonBox";
import {Approval, Data, fetchData} from "./Data";
import {Box} from "./component/layout/Box";
const IntroBox: React.FC<{ triggerLogoutRedirect: () => Promise<void>; }> =
({triggerLogoutRedirect}) => {
return <ButtonBox buttonText={"Logout"} buttonOnClick={() => triggerLogoutRedirect().catch(console.error)}>
<p>Azure DevOps Dashboard</p>
<p><a href="https://github.com/neuland/azure-devops-dashboard"
rel="noreferrer"
target="_blank">GitHub Project</a></p>
</ButtonBox>;
};
const PendingApprovalsBox: React.FC<{ pendingApprovals: ReadonlyArray<Approval>; }> =
({pendingApprovals}) => {
return <Box>
<table>
<caption>Pending Approvals</caption>
<thead>
<tr>
<th>Organization</th>
<th>Project</th>
<th>Pipeline</th>
<th>Build</th>
<th>Stage</th>
<th>Action</th>
</tr>
</thead>
<tbody>{
pendingApprovals.length > 0
? pendingApprovals.map((pendingApproval, index) => {
const href = `https://dev.azure.com/${pendingApproval.organizationName}/${pendingApproval.projectName}/_build/results?buildId=${pendingApproval.buildId}`;
return <tr key={index}>
<td>{pendingApproval.organizationName}</td>
<td>{pendingApproval.projectName}</td>
<td>{pendingApproval.buildDefinitionName}</td>
<td>{pendingApproval.buildId}</td>
<td>{pendingApproval.maybeStage}</td>
<td>
<a className="button" href={href} rel="noreferrer" target="_blank">Open in a new tab</a>
</td>
</tr>
})
: <tr>
<td className="empty" colSpan={6}>No pending approvals found</td>
</tr>
}</tbody>
</table>
</Box>;
};
const Dashboard: React.FC<{ triggerLogoutRedirect: () => Promise<void>; }> =
({triggerLogoutRedirect}) => {
const {accessToken} = useAccessToken();
const [data, setData] = useState<Data | null>(null);
const fetchAndStoreData = useCallback(() => {
fetchData(accessToken).then(setData).catch(console.error);
}, [accessToken]);
useEffect(() => {
if (data === null) {
fetchAndStoreData();
}
const refreshIntervalId = window.setInterval(fetchAndStoreData, 30 * 1000);
return () => window.clearInterval(refreshIntervalId);
}, [data, fetchAndStoreData]);
if (data === null) {
return <ButtonBox buttonText="Logout" buttonOnClick={() => triggerLogoutRedirect().catch(console.error)}>
<p>Fetching data...</p>
</ButtonBox>;
}
return <>
<IntroBox triggerLogoutRedirect={triggerLogoutRedirect}/>
<PendingApprovalsBox pendingApprovals={data.pendingApprovals}/>
</>;
};
export const App: React.FC =
() => {
const accessTokenControls = useAccessTokenControls(true);
switch (accessTokenControls._tag) {
case accessTokenControlsTagLoggedOut:
return <FullscreenCenter>
<ButtonBox buttonText="Login"
buttonOnClick={() => accessTokenControls.triggerLoginRedirect().catch(console.error)}>
<p>Use your Azure DevOps account.</p>
</ButtonBox>
</FullscreenCenter>;
case accessTokenControlsTagLoggingIn:
return <FullscreenCenter>
<ButtonBox buttonText="Logout"
buttonOnClick={() => accessTokenControls.triggerLogoutRedirect().catch(console.error)}>
<p>Logging in…</p>
</ButtonBox>
</FullscreenCenter>;
case accessTokenControlsTagLoggedIn:
case accessTokenControlsTagFetching:
return <FullscreenCenter>
<ButtonBox buttonText="Logout"
buttonOnClick={() => accessTokenControls.triggerLogoutRedirect().catch(console.error)}>
<p>Fetching access token…</p>
</ButtonBox>
</FullscreenCenter>;
case accessTokenControlsTagAvailable:
return <FullscreenCenter>
<Dashboard triggerLogoutRedirect={accessTokenControls.triggerLogoutRedirect}/>
</FullscreenCenter>;
}
};