| 1 | "use client"; |
| 2 | |
| 3 | import { useQuery, useQueryClient |
| 4 | import { UsersRound } |
| 5 | import Link from "next/link |
| 6 | import { useState } |
| 7 | |
| 8 | import { SettingCard } |
| 9 | import { Button } |
| 10 | import { Card } |
| 11 | import { |
| 12 | Empty, |
| 13 | EmptyDescription, |
| 14 | EmptyHeader, |
| 15 | EmptyMedia, |
| 16 | EmptyTitle, |
| 17 | } from "@/components/ui/empty"; |
| 18 | import { Field, FieldLabel |
| 19 | import { Input } |
| 20 | import { |
| 21 | Item, |
| 22 | ItemActions, |
| 23 | ItemContent, |
| 24 | ItemDescription, |
| 25 | ItemMedia, |
| 26 | ItemTitle, |
| 27 | } from "@/components/ui/item"; |
| 28 | import { apiClient, apiErrorMessage |
| 29 | import { authClient } |
| 30 | |
| 31 | import { TeamMembers } |
| 32 | import { useOrganization } |
| 33 | |
| 34 | /** Teams in the organization's settings: create and delete them, and manage who is on each. */ |
| 35 | export function OrganizationTeams({ |
| 36 | const queryClient = |
| 37 | const { organization, |
| 38 | const [name, |
| 39 | |
| 40 | const { data: |
| 41 | queryKey: ["organization-teams", |
| 42 | queryFn: async () |
| 43 | const { data, |
| 44 | "/api/organizations/{slug}/teams", |
| 45 | { params: { path: |
| 46 | ); |
| 47 | if (error) throw |
| 48 | return data.teams |
| 49 | }, |
| 50 | }); |
| 51 | const refresh = |
| 52 | queryClient.invalidateQueries({ |
| 53 | |
| 54 | return ( |
| 55 | <div className="flex flex-col gap-8 |
| 56 | <SettingCard |
| 57 | title="Create a team" |
| 58 | hint="Give a team a role on a repository from that repository's Access settings." |
| 59 | onSubmit={() => |
| 60 | change( |
| 61 | () => |
| 62 | authClient.organization.createTeam |
| 63 | organizationId: organization?.id |
| 64 | name: name.trim |
| 65 | }), |
| 66 | `Team ${name.trim |
| 67 | ).then((ok |
| 68 | if (!ok) return |
| 69 | setName(""); |
| 70 | return refresh(); |
| 71 | }) |
| 72 | } |
| 73 | pending={busy} |
| 74 | canSave={Boolean(organization |
| 75 | submitText="Create team" |
| 76 | > |
| 77 | <Field> |
| 78 | <FieldLabel htmlFor=" |
| 79 | Team name |
| 80 | </FieldLabel> |
| 81 | <Input |
| 82 | id="team-name" |
| 83 | placeholder="Backend" |
| 84 | autoComplete="off" |
| 85 | value={name} |
| 86 | onChange={(event) |
| 87 | className="max-w-md" |
| 88 | /> |
| 89 | </Field> |
| 90 | </SettingCard> |
| 91 | |
| 92 | <section className="flex flex-col gap-4 |
| 93 | <h2 className="text-sm font-semibold |
| 94 | {teams.length === |
| 95 | <Card className=" |
| 96 | <Empty className=" |
| 97 | <EmptyHeader> |
| 98 | <EmptyMedia variant=" |
| 99 | <UsersRound /> |
| 100 | </EmptyMedia> |
| 101 | <EmptyTitle>No teams yet</EmptyTitle |
| 102 | <EmptyDescription> |
| 103 | Group members to share repositories with all of them at once. |
| 104 | </EmptyDescription> |
| 105 | </EmptyHeader> |
| 106 | </Empty> |
| 107 | </Card> |
| 108 | ) : ( |
| 109 | teams.map((team |
| 110 | <Card key={ |
| 111 | <Item className=" |
| 112 | <ItemMedia variant=" |
| 113 | <UsersRound /> |
| 114 | </ItemMedia> |
| 115 | <ItemContent> |
| 116 | <ItemTitle> |
| 117 | <Link |
| 118 | href={`/${slug |
| 119 | className="hover:underline" |
| 120 | > |
| 121 | {team.name} |
| 122 | </Link> |
| 123 | </ItemTitle> |
| 124 | <ItemDescription> |
| 125 | @{slug}/{team |
| 126 | {team.repositoryCount === |
| 127 | ? "1 repository" |
| 128 | : `${team.repositoryCount |
| 129 | </ItemDescription> |
| 130 | </ItemContent> |
| 131 | <ItemActions> |
| 132 | <Button |
| 133 | size="sm" |
| 134 | variant="ghost" |
| 135 | className="text-destructive" |
| 136 | disabled={busy || |
| 137 | onClick={() => |
| 138 | change( |
| 139 | () => |
| 140 | authClient.organization.removeTeam |
| 141 | teamId: team.id |
| 142 | organizationId: organization?.id |
| 143 | }), |
| 144 | `Team ${team.name |
| 145 | ).then((ok |
| 146 | if (ok) return |
| 147 | }) |
| 148 | } |
| 149 | > |
| 150 | Delete team |
| 151 | </Button> |
| 152 | </ItemActions> |
| 153 | </Item> |
| 154 | <TeamMembers organization={ |
| 155 | </Card> |
| 156 | )) |
| 157 | )} |
| 158 | </section> |
| 159 | </div> |
| 160 | ); |
| 161 | } |