Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/repositories/create/AddRepositoryModal.tsx
2501 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { FC, useCallback, useState } from "react";
8
import Modal, { ModalBody, ModalFooter, ModalFooterAlert, ModalHeader } from "../../components/Modal";
9
import { SuggestedRepository } from "@gitpod/gitpod-protocol";
10
import RepositoryFinder from "../../components/RepositoryFinder";
11
import { InputField } from "../../components/forms/InputField";
12
import { AuthorizeGit, useNeedsGitAuthorization } from "../../components/AuthorizeGit";
13
import { useTemporaryState } from "../../hooks/use-temporary-value";
14
import { CreateConfigurationArgs, useCreateConfiguration } from "../../data/configurations/configuration-queries";
15
import type { Configuration } from "@gitpod/public-api/lib/gitpod/v1/configuration_pb";
16
import { LoadingButton } from "@podkit/buttons/LoadingButton";
17
import { Button } from "@podkit/buttons/Button";
18
19
type Props = {
20
onCreated: (configuration: Configuration) => void;
21
onClose: () => void;
22
};
23
24
export const ImportRepositoryModal: FC<Props> = ({ onClose, onCreated }) => {
25
const needsGitAuth = useNeedsGitAuthorization();
26
const [selectedRepo, setSelectedRepo] = useState<SuggestedRepository>();
27
const createConfiguration = useCreateConfiguration();
28
const [createErrorMsg, setCreateErrorMsg] = useTemporaryState("", 3000);
29
30
const handleSubmit = useCallback(() => {
31
if (!selectedRepo) {
32
setCreateErrorMsg("Please select a repository");
33
return;
34
}
35
36
const newProjectArgs: CreateConfigurationArgs = {
37
// leave the name empty to let the backend generate the name
38
name: "",
39
cloneUrl: selectedRepo.url,
40
};
41
42
createConfiguration.mutate(newProjectArgs, {
43
onSuccess: onCreated,
44
});
45
}, [createConfiguration, onCreated, selectedRepo, setCreateErrorMsg]);
46
47
const errorMessage =
48
createErrorMsg ||
49
(createConfiguration.isError &&
50
(createConfiguration.error?.message ?? "There was a problem importing your repository"));
51
52
return (
53
<Modal visible onClose={onClose} onSubmit={handleSubmit}>
54
<ModalHeader>Add a repository</ModalHeader>
55
<ModalBody>
56
<div className="w-112 max-w-full">
57
{needsGitAuth ? (
58
<AuthorizeGit />
59
) : (
60
<>
61
<InputField className="mb-8 w-full">
62
<RepositoryFinder
63
selectedContextURL={selectedRepo?.url}
64
selectedConfigurationId={selectedRepo?.projectId}
65
onChange={setSelectedRepo}
66
excludeConfigurations
67
/>
68
</InputField>
69
</>
70
)}
71
</div>
72
</ModalBody>
73
<ModalFooter
74
alert={
75
errorMessage && (
76
<ModalFooterAlert type="danger" onClose={() => setCreateErrorMsg("")}>
77
{errorMessage}
78
</ModalFooterAlert>
79
)
80
}
81
>
82
<Button variant="secondary" onClick={onClose}>
83
Cancel
84
</Button>
85
<LoadingButton type="submit" loading={createConfiguration.isLoading}>
86
Add
87
</LoadingButton>
88
</ModalFooter>
89
</Modal>
90
);
91
};
92
93