-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathWorkflowSelector.tsx
More file actions
186 lines (173 loc) · 5.11 KB
/
WorkflowSelector.tsx
File metadata and controls
186 lines (173 loc) · 5.11 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {
Checkbox,
Chip,
FormControl,
InputLabel,
ListItemText,
MenuItem,
OutlinedInput,
Select,
CircularProgress
} from '@mui/material';
import { union } from 'ramda';
import { FC, useCallback, useMemo } from 'react';
import { handleApi } from '@/api-helpers/axios-api-instance';
import { Line } from '@/components/Text';
import { track } from '@/constants/events';
import { useAuth } from '@/hooks/useAuth';
import { useEasyState, useBoolState } from '@/hooks/useEasyState';
import {
BaseRepo,
RepoWorkflowResponse,
RepoWorkflow
} from '@/types/resources';
import { depFn } from '@/utils/fn';
import { AsyncSelectOptions } from './AsyncSelect';
import { FlexBox } from './FlexBox';
import { useTeamCRUD } from './Teams/useTeamsConfig';
export const DeploymentWorkflowSelector: FC<{ repo: BaseRepo }> = ({
repo
}) => {
const { orgId } = useAuth();
const options = useEasyState<AsyncSelectOptions>([]);
const nextPageToken = useEasyState<string | null>('');
const loading = useBoolState();
const { updateWorkflowsForTeam } = useTeamCRUD();
const provider = repo.provider;
const selectedOptions = useMemo(
() =>
repo.repo_workflows?.map((val) => ({
label: val.name,
value: val.value,
provider: val.provider
})) || [],
[repo.repo_workflows]
);
const loadWorkflows = useCallback(async () => {
if (!orgId) return;
track('VIEW_WORKFLOWS_FOR_REPO', { repo });
const workflowsResponse: RepoWorkflowResponse = await depFn(
loading.trackAsync,
() =>
handleApi<RepoWorkflow[]>(`/internal/${orgId}/integrations/workflows`, {
params: {
provider: provider,
org_name: repo.parent,
repo_name: repo.name,
repo_slug: repo.slug,
next_page_token: nextPageToken.value
}
})
);
const workflows = workflowsResponse.workflows;
nextPageToken.set(workflowsResponse.next_page_token);
const workflowOpts = workflows.map((w: RepoWorkflow) => ({
renderLabel: (
<FlexBox
fullWidth
justifyBetween
alignCenter
sx={{
fontSize: '12px'
}}
>
{w.name}
<Chip
label="Github Actions"
size="small"
color="default"
sx={{
fontSize: '8px'
}}
/>
</FlexBox>
),
label: w.name,
value: w.provider_workflow_id,
provider: w.provider
}));
depFn(options.set, (prev) => union(prev, workflowOpts));
}, [orgId, repo, loading.trackAsync, nextPageToken, options.set, provider]);
const alreadySelectedWorkflowIds = useMemo(
() => selectedOptions.map((o) => o.value),
[selectedOptions]
);
return (
<FormControl sx={{ width: 250 }}>
<InputLabel
sx={{
top: '-6px',
'&.MuiInputLabel-shrink': {
top: '0px'
}
}}
>
Choose Workflow
</InputLabel>
<Select
multiple
value={selectedOptions}
onOpen={loadWorkflows}
MenuProps={{
MenuListProps: {
'aria-labelledby': 'simple-menu',
disablePadding: true,
sx: {
padding: 0,
maxHeight: '350px',
overflowY: 'auto',
msOverflowStyle: 'none',
scrollbarWidth: 'none',
'&::-webkit-scrollbar': { display: 'none' }
}
}
}}
input={<OutlinedInput label="Choose workflow" margin="dense" />}
renderValue={(selected) => selected.map((w) => w.label).join(', ')}
size="small"
sx={{ textAlign: 'start' }}
>
{loading.value ? (
<FlexBox alignCenter gap2>
<CircularProgress size="20px" />
<Line>Loading...</Line>
</FlexBox>
) : (
options.value.map((o) => {
return (
<MenuItem key={o.value}>
<Checkbox
checked={alreadySelectedWorkflowIds.includes(String(o.value))}
onChange={(e) => {
const isChecked = e.target.checked;
const updatedOptions = isChecked
? [
...selectedOptions,
{
label: o.label,
value: String(o.value),
provider: o.provider
}
]
: selectedOptions.filter(
(w) => w.value !== String(o.value)
);
updateWorkflowsForTeam(
repo,
updatedOptions.map((rw) => ({
name: rw.label,
value: rw.value,
provider: rw.provider
}))
);
}}
/>
<ListItemText primary={o.label} />
</MenuItem>
);
})
)}
</Select>
</FormControl>
);
};