|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | + |
| 5 | +import { useDebounce } from "@calcom/lib/hooks/useDebounce"; |
| 6 | +import { useLocale } from "@calcom/lib/hooks/useLocale"; |
| 7 | +import { trpc } from "@calcom/trpc/react"; |
| 8 | +import type { RouterOutputs } from "@calcom/trpc/react"; |
| 9 | +import { Avatar } from "@calcom/ui/components/avatar"; |
| 10 | +import { Button } from "@calcom/ui/components/button"; |
| 11 | +import { Checkbox, TextField } from "@calcom/ui/components/form"; |
| 12 | +import { |
| 13 | + Sheet, |
| 14 | + SheetContent, |
| 15 | + SheetHeader, |
| 16 | + SheetTitle, |
| 17 | + SheetBody, |
| 18 | + SheetFooter, |
| 19 | +} from "@calcom/ui/components/sheet"; |
| 20 | +import { SkeletonContainer, SkeletonText } from "@calcom/ui/components/skeleton"; |
| 21 | +import { showToast } from "@calcom/ui/components/toast"; |
| 22 | + |
| 23 | +type Flag = RouterOutputs["viewer"]["features"]["list"][number]; |
| 24 | + |
| 25 | +interface AssignFeatureSheetProps { |
| 26 | + flag: Flag; |
| 27 | + open: boolean; |
| 28 | + onOpenChange: (open: boolean) => void; |
| 29 | +} |
| 30 | + |
| 31 | +export function AssignFeatureSheet({ flag, open, onOpenChange }: AssignFeatureSheetProps) { |
| 32 | + const { t } = useLocale(); |
| 33 | + const utils = trpc.useUtils(); |
| 34 | + const [searchTerm, setSearchTerm] = useState(""); |
| 35 | + const debouncedSearchTerm = useDebounce(searchTerm, 300); |
| 36 | + |
| 37 | + const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isPending } = |
| 38 | + trpc.viewer.admin.getTeamsForFeature.useInfiniteQuery( |
| 39 | + { |
| 40 | + featureId: flag.slug, |
| 41 | + limit: 20, |
| 42 | + searchTerm: debouncedSearchTerm || undefined, |
| 43 | + }, |
| 44 | + { |
| 45 | + enabled: open, |
| 46 | + getNextPageParam: (lastPage) => lastPage.nextCursor, |
| 47 | + } |
| 48 | + ); |
| 49 | + |
| 50 | + const teams = data?.pages.flatMap((page) => page.teams) ?? []; |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + if (!open) { |
| 54 | + setSearchTerm(""); |
| 55 | + } |
| 56 | + }, [open]); |
| 57 | + |
| 58 | + const assignMutation = trpc.viewer.admin.assignFeatureToTeam.useMutation({ |
| 59 | + onSuccess: () => { |
| 60 | + utils.viewer.admin.getTeamsForFeature.invalidate({ featureId: flag.slug }); |
| 61 | + showToast(t("feature_assigned_successfully"), "success"); |
| 62 | + }, |
| 63 | + onError: (err) => { |
| 64 | + showToast(err.message, "error"); |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + const unassignMutation = trpc.viewer.admin.unassignFeatureFromTeam.useMutation({ |
| 69 | + onSuccess: () => { |
| 70 | + utils.viewer.admin.getTeamsForFeature.invalidate({ featureId: flag.slug }); |
| 71 | + showToast(t("feature_unassigned_successfully"), "success"); |
| 72 | + }, |
| 73 | + onError: (err) => { |
| 74 | + showToast(err.message, "error"); |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + const handleToggleTeam = (teamId: number, currentlyHasFeature: boolean) => { |
| 79 | + if (currentlyHasFeature) { |
| 80 | + unassignMutation.mutate({ |
| 81 | + teamId, |
| 82 | + featureId: flag.slug, |
| 83 | + }); |
| 84 | + } else { |
| 85 | + assignMutation.mutate({ |
| 86 | + teamId, |
| 87 | + featureId: flag.slug, |
| 88 | + }); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + const handleClose = () => { |
| 93 | + onOpenChange(false); |
| 94 | + }; |
| 95 | + |
| 96 | + const isLoading = assignMutation.isPending || unassignMutation.isPending; |
| 97 | + |
| 98 | + return ( |
| 99 | + <Sheet open={open} onOpenChange={handleClose}> |
| 100 | + <SheetContent className="bg-muted"> |
| 101 | + <SheetHeader> |
| 102 | + <SheetTitle>Assign: {flag.slug}</SheetTitle> |
| 103 | + </SheetHeader> |
| 104 | + <SheetBody> |
| 105 | + <div className="mb-4"> |
| 106 | + <TextField |
| 107 | + type="text" |
| 108 | + placeholder={t("search")} |
| 109 | + value={searchTerm} |
| 110 | + onChange={(e) => setSearchTerm(e.target.value)} |
| 111 | + /> |
| 112 | + </div> |
| 113 | + {isPending ? ( |
| 114 | + <SkeletonContainer> |
| 115 | + <div className="space-y-3"> |
| 116 | + {[...Array(5)].map((_, i) => ( |
| 117 | + <SkeletonText key={i} className="h-16 w-full" /> |
| 118 | + ))} |
| 119 | + </div> |
| 120 | + </SkeletonContainer> |
| 121 | + ) : teams && teams.length > 0 ? ( |
| 122 | + <> |
| 123 | + <div className="space-y-2"> |
| 124 | + {teams.map((team) => ( |
| 125 | + <button |
| 126 | + key={team.id} |
| 127 | + type="button" |
| 128 | + onClick={() => handleToggleTeam(team.id, team.hasFeature)} |
| 129 | + disabled={isLoading} |
| 130 | + className="bg-default border-subtle hover:bg-muted flex w-full items-center justify-between rounded-lg border p-4 text-left transition-colors disabled:cursor-not-allowed disabled:opacity-50"> |
| 131 | + <div className="flex items-center gap-3"> |
| 132 | + <div className="relative"> |
| 133 | + {team.isOrganization ? ( |
| 134 | + <div className="h-8 w-8 overflow-hidden rounded"> |
| 135 | + {team.logoUrl ? ( |
| 136 | + <img |
| 137 | + src={team.logoUrl} |
| 138 | + alt={team.name || ""} |
| 139 | + className="h-full w-full object-cover" |
| 140 | + /> |
| 141 | + ) : ( |
| 142 | + <div className="bg-emphasis text-default flex h-full w-full items-center justify-center text-xs font-semibold"> |
| 143 | + {team.name?.charAt(0).toUpperCase()} |
| 144 | + </div> |
| 145 | + )} |
| 146 | + </div> |
| 147 | + ) : ( |
| 148 | + <Avatar size="sm" alt={team.name || ""} imageSrc={team.logoUrl} /> |
| 149 | + )} |
| 150 | + {team.parent && team.parentId && ( |
| 151 | + <div className="border-emphasis absolute -bottom-1 -right-1 h-4 w-4 overflow-hidden rounded border"> |
| 152 | + {team.parent.logoUrl ? ( |
| 153 | + <img |
| 154 | + src={team.parent.logoUrl} |
| 155 | + alt={team.parent.name || ""} |
| 156 | + className="h-full w-full object-cover" |
| 157 | + /> |
| 158 | + ) : ( |
| 159 | + <div className="bg-emphasis text-default flex h-full w-full items-center justify-center text-[8px] font-semibold"> |
| 160 | + {team.parent.name?.charAt(0).toUpperCase()} |
| 161 | + </div> |
| 162 | + )} |
| 163 | + </div> |
| 164 | + )} |
| 165 | + </div> |
| 166 | + <div> |
| 167 | + <p className="text-emphasis text-sm font-medium">{team.name}</p> |
| 168 | + {team.slug && <p className="text-subtle text-xs">{team.slug}</p>} |
| 169 | + {team.parent && ( |
| 170 | + <p className="text-subtle text-xs"> |
| 171 | + {t("organization")}: {team.parent.name} |
| 172 | + </p> |
| 173 | + )} |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + <Checkbox |
| 177 | + checked={team.hasFeature} |
| 178 | + disabled={isLoading} |
| 179 | + onCheckedChange={() => {}} |
| 180 | + /> |
| 181 | + </button> |
| 182 | + ))} |
| 183 | + </div> |
| 184 | + {hasNextPage && ( |
| 185 | + <div className="mt-4 flex justify-center"> |
| 186 | + <Button |
| 187 | + color="secondary" |
| 188 | + onClick={() => fetchNextPage()} |
| 189 | + loading={isFetchingNextPage} |
| 190 | + disabled={isFetchingNextPage}> |
| 191 | + {t("load_more")} |
| 192 | + </Button> |
| 193 | + </div> |
| 194 | + )} |
| 195 | + </> |
| 196 | + ) : ( |
| 197 | + <p className="text-subtle text-center text-sm">{t("no_teams_found")}</p> |
| 198 | + )} |
| 199 | + </SheetBody> |
| 200 | + <SheetFooter> |
| 201 | + <Button color="secondary" onClick={handleClose}> |
| 202 | + {t("close")} |
| 203 | + </Button> |
| 204 | + </SheetFooter> |
| 205 | + </SheetContent> |
| 206 | + </Sheet> |
| 207 | + ); |
| 208 | +} |
0 commit comments