-
Notifications
You must be signed in to change notification settings - Fork 40
/
PackageSourceButton.tsx
65 lines (58 loc) · 2.24 KB
/
PackageSourceButton.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
import { Button, ButtonGroup, ButtonProps, Icon, Link, Spinner, Tag, Tooltip } from "@chakra-ui/react";
import { PropsWithChildren } from "react";
import { IoLogoGithub } from "react-icons/io";
import { useKurtosisPackageIndexerClient } from "../client/packageIndexer/KurtosisPackageIndexerClientContext";
import { isDefined, wrapResult } from "../utils";
type EnclaveSourceProps = PropsWithChildren<
ButtonProps & {
source: "loading" | string | null;
}
>;
export const PackageSourceButton = ({ source, children, ...buttonProps }: EnclaveSourceProps) => {
const kurtosisIndexer = useKurtosisPackageIndexerClient();
if (!isDefined(source)) {
return <Tag>Unknown</Tag>;
}
if (source === "loading") {
return <Spinner size={"xs"} />;
}
let button = (
<Link href={`https://${source}`} target="_blank" rel="noopener noreferrer" w={buttonProps.w || buttonProps.width}>
<Button variant={"ghost"} size={"xs"} {...buttonProps}>
{children || source}
</Button>
</Link>
);
if (source.startsWith("github.com/")) {
const repositoryResult = wrapResult(() => kurtosisIndexer.parsePackageUrl(source));
if (repositoryResult.isOk) {
const repository = repositoryResult.value;
const url = `https://${repository.baseUrl}/${repository.owner}/${repository.name}${
isDefined(repository.rootPath) && repository.rootPath !== "/" ? "/tree/main/" + repository.rootPath : ""
}`;
button = (
<Link href={url} target="_blank" rel="noopener noreferrer" w={buttonProps.w || buttonProps.width}>
<Button
leftIcon={<Icon as={IoLogoGithub} color={"gray.400"} />}
variant={"ghost"}
size={"xs"}
{...buttonProps}
>
{children || source.replace("github.com/", "")}
</Button>
</Link>
);
} else {
button = (
<Tooltip shouldWrapChildren label={repositoryResult.error}>
<a href={`https://${source}`} target="_blank" rel="noopener noreferrer">
<Button variant={"ghost"} size={"xs"} {...buttonProps} colorScheme={"red"}>
{children || source}
</Button>
</a>
</Tooltip>
);
}
}
return <ButtonGroup>{button}</ButtonGroup>;
};