This repository was archived by the owner on Jun 19, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathAddToWorkspaceDrawer.tsx
More file actions
154 lines (144 loc) · 5.21 KB
/
AddToWorkspaceDrawer.tsx
File metadata and controls
154 lines (144 loc) · 5.21 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
import { MdWorkspaces } from "react-icons/md";
import { useEffect, useState } from "react";
import { useRouter } from "next/router";
import { BsGithub } from "react-icons/bs";
import { usePostHog } from "posthog-js/react";
import Button from "components/shared/Button/button";
import { Drawer } from "components/shared/Drawer";
import { useToast } from "lib/hooks/useToast";
import useSupabaseAuth from "lib/hooks/useSupabaseAuth";
import useWorkspaces from "lib/hooks/api/useWorkspaces";
import { fetchApiData } from "helpers/fetchApiData";
import SingleSelect from "components/atoms/Select/single-select";
import Text from "components/atoms/Typography/text";
interface AddToWorkspaceDrawerProps {
repository: string;
type?: "repo" | "sbom";
}
export default function AddToWorkspaceDrawer({ repository, type = "repo" }: AddToWorkspaceDrawerProps) {
const router = useRouter();
const posthog = usePostHog();
const { toast } = useToast();
const { signIn, user, sessionToken } = useSupabaseAuth();
const [workspaceId, setWorkspaceId] = useState("new");
const { data: workspaces, isLoading: workspacesLoading, mutate } = useWorkspaces({ load: !!user, limit: 100 });
const [host, setHost] = useState("");
useEffect(() => {
if (typeof window !== "undefined") {
setHost(window.location.origin as string);
}
}, []);
const addRepositoryToWorkspace = async () => {
posthog.capture(`Repo Pages: added to ${workspaceId === "new" ? "a new" : "existing"} workspace`, {
repository,
workspaceId,
});
if (workspaceId === "new") {
router.push(`/workspaces/new?repos=${JSON.stringify([repository])}`);
return;
}
const { data, error } = await fetchApiData<Workspace>({
method: "POST",
path: `workspaces/${workspaceId}/repos/${repository}`,
body: {}, // empty body needed to avoid error
bearerToken: sessionToken!,
pathValidator: () => true,
});
if (error) {
toast({ description: `Error adding repository to the workspace. Please try again`, variant: "danger" });
} else {
toast({ description: `Added repository successfully`, variant: "success" });
}
};
const params = new URLSearchParams();
if (type === "sbom") {
params.set("sbom", "true");
params.set("repo", repository);
} else {
params.set("repos", JSON.stringify([repository]));
}
const url = `/workspaces/new?${params}`;
const redirectTo = new URL(url, window.location.origin).toString();
const title = type === "repo" ? "Add repository to Workspace" : "Add repository SBOM to Workspace";
return (
<Drawer
title={title}
description="Create a new workspace or add to an existing one."
showCloseButton
trigger={
<Button
variant={type === "repo" ? "primary" : "dark"}
className="shrink-0 items-center gap-3 w-full"
onClick={(event) => {
if (user && type === "sbom") {
event.preventDefault();
router.push(url);
}
}}
>
<MdWorkspaces />
{type === "repo" ? "Add to Workspace" : "Workspace from SBOM"}
</Button>
}
>
{!user ? (
<div className="flex flex-col gap-4 text-center">
<img
src="/assets/workspace_overview.png"
alt="Workspace screenshot from documentation"
className="border-2 border-light-orange-9 shadow-md rounded-lg"
/>
<Text>
Keep track of repositories and contributors easily with our new feature
<span className="font-semibold"> Workspaces!</span> If you've used OpenSauced before, your insights and
lists are now part of your personal workspace.
</Text>
<p className="font-medium text-light-orange-10">
{type === "sbom"
? "Create a new workspace with this repository's SBOM and explore open source like never before!"
: "Create a new workspace with this repository and explore open source like never before!"}
</p>
<Button
variant="primary"
className="w-fit gap-2 self-center"
onClick={() => {
signIn({
provider: "github",
options: { redirectTo },
});
}}
>
<BsGithub className="w-5 h-5" />
Connect with GitHub
</Button>
</div>
) : (
<>
{workspacesLoading ? (
<p>Loading...</p>
) : (
<SingleSelect
options={[
{ label: "Create new workspace...", value: "new" },
...workspaces.map(({ id, name }) => ({
label: name,
value: id,
})),
]}
position="popper"
value={workspaceId ?? "new"}
labelText="Select a workspace"
placeholder="Select a workspace"
onValueChange={(value) => {
setWorkspaceId(value);
}}
/>
)}
<Button onClick={addRepositoryToWorkspace} variant="primary" className="w-full !grid">
Confirm
</Button>
</>
)}
</Drawer>
);
}