-
Notifications
You must be signed in to change notification settings - Fork 201
Organization switching & active org management #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eaf7e68
Create default org on user creation
brendan-kellam d337249
add active org id
brendan-kellam 1fd7730
Add relation between repos and org
brendan-kellam a2947bc
Redirect to root when already logged in
brendan-kellam 2ee875d
Add org switcher
brendan-kellam f37b330
Switch to use Command component
brendan-kellam 9333b7e
Add org icon
brendan-kellam 34ccb93
format
brendan-kellam 192c1be
feedback
brendan-kellam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/db/prisma/migrations/20250120201905_add_role/migration.sql
This file contains hidden or 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,18 @@ | ||
| -- RedefineTables | ||
| PRAGMA defer_foreign_keys=ON; | ||
| PRAGMA foreign_keys=OFF; | ||
| CREATE TABLE "new_UserToOrg" ( | ||
| "joinedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "orgId" INTEGER NOT NULL, | ||
| "userId" TEXT NOT NULL, | ||
| "role" TEXT NOT NULL DEFAULT 'MEMBER', | ||
|
|
||
| PRIMARY KEY ("orgId", "userId"), | ||
| CONSTRAINT "UserToOrg_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "Org" ("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
| CONSTRAINT "UserToOrg_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE | ||
| ); | ||
| INSERT INTO "new_UserToOrg" ("joinedAt", "orgId", "userId") SELECT "joinedAt", "orgId", "userId" FROM "UserToOrg"; | ||
| DROP TABLE "UserToOrg"; | ||
| ALTER TABLE "new_UserToOrg" RENAME TO "UserToOrg"; | ||
| PRAGMA foreign_keys=ON; | ||
| PRAGMA defer_foreign_keys=OFF; |
2 changes: 2 additions & 0 deletions
2
packages/db/prisma/migrations/20250120222015_add_active_org_id/migration.sql
This file contains hidden or 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,2 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "User" ADD COLUMN "activeOrgId" INTEGER; |
27 changes: 27 additions & 0 deletions
27
packages/db/prisma/migrations/20250120230752_relate_repo_to_org/migration.sql
This file contains hidden or 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,27 @@ | ||
| -- RedefineTables | ||
| PRAGMA defer_foreign_keys=ON; | ||
| PRAGMA foreign_keys=OFF; | ||
| CREATE TABLE "new_Repo" ( | ||
| "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
| "name" TEXT NOT NULL, | ||
| "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" DATETIME NOT NULL, | ||
| "indexedAt" DATETIME, | ||
| "isFork" BOOLEAN NOT NULL, | ||
| "isArchived" BOOLEAN NOT NULL, | ||
| "metadata" JSONB NOT NULL, | ||
| "cloneUrl" TEXT NOT NULL, | ||
| "tenantId" INTEGER NOT NULL, | ||
| "repoIndexingStatus" TEXT NOT NULL DEFAULT 'NEW', | ||
| "external_id" TEXT NOT NULL, | ||
| "external_codeHostType" TEXT NOT NULL, | ||
| "external_codeHostUrl" TEXT NOT NULL, | ||
| "orgId" INTEGER, | ||
| CONSTRAINT "Repo_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "Org" ("id") ON DELETE CASCADE ON UPDATE CASCADE | ||
| ); | ||
| INSERT INTO "new_Repo" ("cloneUrl", "createdAt", "external_codeHostType", "external_codeHostUrl", "external_id", "id", "indexedAt", "isArchived", "isFork", "metadata", "name", "repoIndexingStatus", "tenantId", "updatedAt") SELECT "cloneUrl", "createdAt", "external_codeHostType", "external_codeHostUrl", "external_id", "id", "indexedAt", "isArchived", "isFork", "metadata", "name", "repoIndexingStatus", "tenantId", "updatedAt" FROM "Repo"; | ||
| DROP TABLE "Repo"; | ||
| ALTER TABLE "new_Repo" RENAME TO "Repo"; | ||
| CREATE UNIQUE INDEX "Repo_external_id_external_codeHostUrl_key" ON "Repo"("external_id", "external_codeHostUrl"); | ||
| PRAGMA foreign_keys=ON; | ||
| PRAGMA defer_foreign_keys=OFF; |
This file contains hidden or 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
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,64 @@ | ||
| 'use server'; | ||
|
|
||
| import { auth } from "./auth"; | ||
| import { notAuthenticated, notFound } from "./lib/serviceError"; | ||
| import { prisma } from "@/prisma"; | ||
|
|
||
|
|
||
| export const createOrg = async (name: string) => { | ||
| const session = await auth(); | ||
| if (!session) { | ||
| return notAuthenticated(); | ||
| } | ||
|
|
||
| // Create the org | ||
| const org = await prisma.org.create({ | ||
| data: { | ||
| name, | ||
| members: { | ||
| create: { | ||
| userId: session.user.id, | ||
| role: "OWNER", | ||
| }, | ||
| }, | ||
| } | ||
| }); | ||
|
|
||
| return { | ||
| id: org.id, | ||
| } | ||
| } | ||
|
|
||
| export const switchActiveOrg = async (orgId: number) => { | ||
| const session = await auth(); | ||
| if (!session) { | ||
| return notAuthenticated(); | ||
| } | ||
|
|
||
| // Check to see if the user is a member of the org | ||
| const membership = await prisma.userToOrg.findUnique({ | ||
| where: { | ||
| orgId_userId: { | ||
| userId: session.user.id, | ||
| orgId, | ||
| } | ||
| }, | ||
| }); | ||
| if (!membership) { | ||
| return notFound(); | ||
| } | ||
|
|
||
| // Update the user's active org | ||
| await prisma.user.update({ | ||
| where: { | ||
| id: session.user.id, | ||
| }, | ||
| data: { | ||
| activeOrgId: orgId, | ||
| } | ||
| }); | ||
|
|
||
| return { | ||
| id: orgId, | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,31 @@ | ||
| import { auth } from "@/auth"; | ||
| import { getUser, getUserOrgs } from "../../data/user"; | ||
| import { OrgSelectorDropdown } from "./orgSelectorDropdown"; | ||
|
|
||
| export const OrgSelector = async () => { | ||
| const session = await auth(); | ||
| if (!session) { | ||
| return null; | ||
| } | ||
|
|
||
| const user = await getUser(session.user.id); | ||
| if (!user) { | ||
| return null; | ||
| } | ||
|
|
||
| const orgs = await getUserOrgs(session.user.id); | ||
| const activeOrg = orgs.find((org) => org.id === user.activeOrgId); | ||
| if (!activeOrg) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <OrgSelectorDropdown | ||
| orgs={orgs.map((org) => ({ | ||
| name: org.name, | ||
| id: org.id, | ||
| }))} | ||
| activeOrgId={activeOrg.id} | ||
| /> | ||
| ) | ||
| } |
80 changes: 80 additions & 0 deletions
80
packages/web/src/app/components/orgSelector/orgCreationDialog.tsx
This file contains hidden or 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,80 @@ | ||
| 'use client'; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; | ||
| import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; | ||
| import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import { PlusCircledIcon } from "@radix-ui/react-icons"; | ||
| import { useForm } from "react-hook-form"; | ||
| import { z } from "zod"; | ||
|
|
||
| const formSchema = z.object({ | ||
| name: z.string().min(2).max(40), | ||
| }); | ||
|
|
||
|
|
||
| interface OrgCreationDialogProps { | ||
| onSubmit: (data: z.infer<typeof formSchema>) => void; | ||
| isOpen: boolean; | ||
| onOpenChange: (isOpen: boolean) => void; | ||
| } | ||
|
|
||
| export const OrgCreationDialog = ({ | ||
| onSubmit, | ||
| isOpen, | ||
| onOpenChange, | ||
| }: OrgCreationDialogProps) => { | ||
| const form = useForm<z.infer<typeof formSchema>>({ | ||
| resolver: zodResolver(formSchema), | ||
| defaultValues: { | ||
| name: "", | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <Dialog | ||
| open={isOpen} | ||
| onOpenChange={onOpenChange} | ||
| > | ||
| <DialogTrigger asChild> | ||
| <DropdownMenuItem | ||
| onSelect={(e) => e.preventDefault()} | ||
| > | ||
| <Button | ||
| variant="ghost" | ||
| size="default" | ||
| className="w-full justify-start gap-1.5 p-0" | ||
| > | ||
| <PlusCircledIcon className="h-5 w-5 text-muted-foreground" /> | ||
| Create organization | ||
| </Button> | ||
| </DropdownMenuItem> | ||
| </DialogTrigger> | ||
| <DialogContent> | ||
| <DialogHeader> | ||
| <DialogTitle>Create an organization</DialogTitle> | ||
| <DialogDescription>Collaborate with</DialogDescription> | ||
| </DialogHeader> | ||
| <Form {...form}> | ||
| <form onSubmit={form.handleSubmit(onSubmit)}> | ||
| <FormField | ||
| control={form.control} | ||
| name="name" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormLabel>Organization name</FormLabel> | ||
| <FormControl> | ||
| <Input {...field} /> | ||
| </FormControl> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
| <Button className="mt-5" type="submit">Submit</Button> | ||
| </form> | ||
| </Form> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ) | ||
| } | ||
This file contains hidden or 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,36 @@ | ||
| import { cn } from "@/lib/utils"; | ||
| import placeholderAvatar from "@/public/placeholder_avatar.png"; | ||
| import { cva } from "class-variance-authority"; | ||
| import Image from "next/image"; | ||
|
|
||
| interface OrgIconProps { | ||
| className?: string; | ||
| size?: "default"; | ||
| } | ||
|
|
||
| const iconVariants = cva( | ||
| "rounded-full", | ||
| { | ||
| variants: { | ||
| size: { | ||
| default: "w-5 h-5" | ||
| } | ||
| }, | ||
| defaultVariants: { | ||
| size: "default" | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| export const OrgIcon = ({ | ||
| className, | ||
| size, | ||
| }: OrgIconProps) => { | ||
| return ( | ||
| <Image | ||
| src={placeholderAvatar} | ||
| alt="Organization avatar" | ||
| className={cn(iconVariants({ size, className }))} | ||
| /> | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.