-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement 'Add to workspace' in repo pages (#3282)
- Loading branch information
Showing
3 changed files
with
286 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { MdWorkspaces } from "react-icons/md"; | ||
import { useEffect, useState } from "react"; | ||
import { useRouter } from "next/router"; | ||
import { BsGithub } from "react-icons/bs"; | ||
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"; | ||
|
||
export default function AddToWorkspaceDrawer({ repository }: { repository: string }) { | ||
const router = useRouter(); | ||
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 () => { | ||
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" }); | ||
} | ||
}; | ||
return ( | ||
<Drawer | ||
title="Add repository to Workspace" | ||
description="Create a new workspace or add to an existing one." | ||
showCloseButton | ||
trigger={ | ||
<Button variant="primary" className="shrink-0 items-center gap-3 w-fit"> | ||
<MdWorkspaces /> | ||
Add to Workspace | ||
</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"> | ||
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: `${host}/workspaces/new?repos=${JSON.stringify([repository])}` }, | ||
}); | ||
}} | ||
> | ||
<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"} | ||
placeholder="Select a workspace" | ||
onValueChange={(value) => { | ||
setWorkspaceId(value); | ||
}} | ||
/> | ||
)} | ||
<Button onClick={addRepositoryToWorkspace} variant="primary" className="w-full !grid"> | ||
Confirm | ||
</Button> | ||
</> | ||
)} | ||
</Drawer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useRouter } from "next/router"; | ||
import { BsGithub } from "react-icons/bs"; | ||
import Card from "components/atoms/Card/card"; | ||
import SingleSelect from "components/atoms/Select/single-select"; | ||
import { Dialog, DialogContent } from "components/molecules/Dialog/dialog"; | ||
import Button from "components/shared/Button/button"; | ||
import { fetchApiData } from "helpers/fetchApiData"; | ||
import useWorkspaces from "lib/hooks/api/useWorkspaces"; | ||
import useSupabaseAuth from "lib/hooks/useSupabaseAuth"; | ||
import { useToast } from "lib/hooks/useToast"; | ||
import Text from "components/atoms/Typography/text"; | ||
|
||
type AddToWorkspaceModalProps = { | ||
repository: string; | ||
isOpen: boolean; | ||
onCloseModal: () => void; | ||
}; | ||
|
||
export default function AddToWorkspaceModal({ repository, isOpen, onCloseModal }: AddToWorkspaceModalProps) { | ||
const { toast } = useToast(); | ||
const router = useRouter(); | ||
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 () => { | ||
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" }); | ||
onCloseModal(); | ||
} | ||
}; | ||
|
||
return ( | ||
<Dialog open={isOpen}> | ||
<DialogContent autoStyle={false} onEscapeKeyDown={onCloseModal} onInteractOutside={onCloseModal}> | ||
<Card heading={<h1 className="text-xl font-semibold">Add to workspace</h1>}> | ||
<div className="flex flex-col gap-4 w-[32rem] h-full px-8 py-4"> | ||
{!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"> | ||
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: `${host}/workspaces/new?repos=${JSON.stringify([repository])}` }, | ||
}); | ||
}} | ||
> | ||
<BsGithub className="w-5 h-5" /> | ||
Connect with GitHub | ||
</Button> | ||
</div> | ||
) : ( | ||
<> | ||
<p>Create a new workspace or add to an existing one.</p> | ||
{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"} | ||
placeholder="Select a workspace" | ||
onValueChange={(value) => { | ||
setWorkspaceId(value); | ||
}} | ||
/> | ||
)} | ||
<Button onClick={addRepositoryToWorkspace} variant="primary" className="w-fit self-end"> | ||
Confirm | ||
</Button> | ||
</> | ||
)} | ||
</div> | ||
</Card> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters