chore: code cleanup
Signed-off-by: Benjamin Janssens <benji.janssens@gmail.com>
This commit is contained in:
+8
-6
@@ -34,22 +34,24 @@ export const BitbucketRepoBranchPicker = ({
|
||||
rawErrors: string[];
|
||||
accessToken?: string;
|
||||
}) => {
|
||||
const { host, workspace, repository, branch, availableBranches } = state;
|
||||
|
||||
const scaffolderApi = useApi(scaffolderApiRef);
|
||||
|
||||
useDebounce(
|
||||
() => {
|
||||
const updateAvailableBranches = async () => {
|
||||
if (
|
||||
state.host === 'bitbucket.org' &&
|
||||
host === 'bitbucket.org' &&
|
||||
accessToken &&
|
||||
state.workspace &&
|
||||
state.repository
|
||||
workspace &&
|
||||
repository
|
||||
) {
|
||||
const result = await scaffolderApi.autocomplete(
|
||||
accessToken,
|
||||
'bitbucketCloud',
|
||||
'branches',
|
||||
{ workspace: state.workspace, repository: state.repository },
|
||||
{ workspace, repository },
|
||||
);
|
||||
|
||||
onChange({ availableBranches: result });
|
||||
@@ -70,13 +72,13 @@ export const BitbucketRepoBranchPicker = ({
|
||||
<FormControl
|
||||
margin="normal"
|
||||
required
|
||||
error={rawErrors?.length > 0 && !state.branch}
|
||||
error={rawErrors?.length > 0 && !branch}
|
||||
>
|
||||
<Autocomplete
|
||||
onChange={(_, newValue) => {
|
||||
onChange({ branch: newValue || undefined });
|
||||
}}
|
||||
options={state.availableBranches || []}
|
||||
options={availableBranches || []}
|
||||
renderInput={params => (
|
||||
<TextField {...params} label="Branch" required />
|
||||
)}
|
||||
|
||||
+29
-25
@@ -36,34 +36,15 @@ import { BitbucketRepoBranchPicker } from './BitbucketRepoBranchPicker';
|
||||
*/
|
||||
export const RepoBranchPicker = (props: RepoBranchPickerProps) => {
|
||||
const { uiSchema, onChange, rawErrors, schema, formContext } = props;
|
||||
|
||||
const [state, setState] = useState<RepoBranchPickerState>({});
|
||||
const { host, branch } = state;
|
||||
|
||||
const integrationApi = useApi(scmIntegrationsApiRef);
|
||||
const scmAuthApi = useApi(scmAuthApiRef);
|
||||
|
||||
const { secrets, setSecrets } = useTemplateSecrets();
|
||||
|
||||
useEffect(() => {
|
||||
if (formContext.formData.repoUrl) {
|
||||
const url = new URL(`https://${formContext.formData.repoUrl}`);
|
||||
|
||||
setState({
|
||||
host: url.host,
|
||||
workspace: url.searchParams.get('workspace') || undefined,
|
||||
repository: url.searchParams.get('repo') || undefined,
|
||||
});
|
||||
}
|
||||
}, [formContext]);
|
||||
|
||||
useEffect(() => {
|
||||
onChange(state.branch);
|
||||
}, [state, onChange]);
|
||||
|
||||
const updateLocalState = useCallback(
|
||||
(newState: RepoBranchPickerState) => {
|
||||
setState(prevState => ({ ...prevState, ...newState }));
|
||||
},
|
||||
[setState],
|
||||
);
|
||||
|
||||
useDebounce(
|
||||
async () => {
|
||||
const { requestUserCredentials } = uiSchema?.['ui:options'] ?? {};
|
||||
@@ -96,8 +77,31 @@ export const RepoBranchPicker = (props: RepoBranchPickerProps) => {
|
||||
[state, uiSchema],
|
||||
);
|
||||
|
||||
const hostType =
|
||||
(state.host && integrationApi.byHost(state.host)?.type) ?? null;
|
||||
useEffect(() => {
|
||||
if (formContext.formData.repoUrl) {
|
||||
const url = new URL(`https://${formContext.formData.repoUrl}`);
|
||||
|
||||
setState({
|
||||
host: url.host,
|
||||
workspace: url.searchParams.get('workspace') || undefined,
|
||||
repository: url.searchParams.get('repo') || undefined,
|
||||
});
|
||||
}
|
||||
}, [formContext]);
|
||||
|
||||
useEffect(() => {
|
||||
onChange(branch);
|
||||
}, [branch, onChange]);
|
||||
|
||||
const updateLocalState = useCallback(
|
||||
(newState: RepoBranchPickerState) => {
|
||||
setState(prevState => ({ ...prevState, ...newState }));
|
||||
},
|
||||
[setState],
|
||||
);
|
||||
|
||||
const hostType = (host && integrationApi.byHost(host)?.type) ?? null;
|
||||
|
||||
return (
|
||||
<>
|
||||
{schema.title && (
|
||||
|
||||
Reference in New Issue
Block a user