|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Avatar } from "@cap/ui"; |
| 4 | +import { useClickAway } from "@uidotdev/usehooks"; |
| 5 | +import clsx from "clsx"; |
| 6 | +import { Check, ChevronDown } from "lucide-react"; |
| 7 | +import { AnimatePresence, motion } from "motion/react"; |
| 8 | +import Image from "next/image"; |
| 9 | +import Link from "next/link"; |
| 10 | +import { useRouter } from "next/navigation"; |
| 11 | +import { |
| 12 | + type Dispatch, |
| 13 | + type LegacyRef, |
| 14 | + type MutableRefObject, |
| 15 | + type SetStateAction, |
| 16 | + useRef, |
| 17 | + useState, |
| 18 | +} from "react"; |
| 19 | +import { useDashboardContext } from "../Contexts"; |
| 20 | +import { CapIcon, CogIcon, LayersIcon } from "./AnimatedIcons"; |
| 21 | +import { updateActiveOrganization } from "./Navbar/server"; |
| 22 | + |
| 23 | +const Tabs = [ |
| 24 | + { icon: <LayersIcon size={20} />, href: "/dashboard/spaces/browse" }, |
| 25 | + { icon: <CapIcon size={25} />, href: "/dashboard/caps" }, |
| 26 | + { |
| 27 | + icon: <CogIcon size={22} />, |
| 28 | + href: "/dashboard/settings/organization", |
| 29 | + ownerOnly: true, |
| 30 | + }, |
| 31 | +]; |
| 32 | + |
| 33 | +const MobileTab = () => { |
| 34 | + const [open, setOpen] = useState(false); |
| 35 | + const containerRef = useRef<HTMLDivElement>(null); |
| 36 | + const { activeOrganization: activeOrg, user } = useDashboardContext(); |
| 37 | + const isOwner = activeOrg?.organization.ownerId === user.id; |
| 38 | + const menuRef = useClickAway((e) => { |
| 39 | + if ( |
| 40 | + containerRef.current && |
| 41 | + !containerRef.current.contains(e.target as Node) |
| 42 | + ) { |
| 43 | + setOpen(false); |
| 44 | + } |
| 45 | + }); |
| 46 | + return ( |
| 47 | + <div className="flex sticky bottom-0 z-50 flex-1 justify-between items-center px-5 w-screen h-16 border-t lg:hidden border-gray-5 bg-gray-1"> |
| 48 | + <AnimatePresence> |
| 49 | + {open && <OrgsMenu setOpen={setOpen} menuRef={menuRef} />} |
| 50 | + </AnimatePresence> |
| 51 | + <Orgs open={open} setOpen={setOpen} containerRef={containerRef} /> |
| 52 | + <div className="flex gap-6 justify-between items-center h-full text-gray-11"> |
| 53 | + {Tabs.filter((i) => !i.ownerOnly || isOwner).map((tab) => ( |
| 54 | + <Link href={tab.href} key={tab.href}> |
| 55 | + {tab.icon} |
| 56 | + </Link> |
| 57 | + ))} |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +const Orgs = ({ |
| 64 | + setOpen, |
| 65 | + open, |
| 66 | + containerRef, |
| 67 | +}: { |
| 68 | + setOpen: Dispatch<SetStateAction<boolean>>; |
| 69 | + open: boolean; |
| 70 | + containerRef: MutableRefObject<HTMLDivElement | null>; |
| 71 | +}) => { |
| 72 | + const { activeOrganization: activeOrg } = useDashboardContext(); |
| 73 | + return ( |
| 74 | + <div |
| 75 | + onClick={() => setOpen((p) => !p)} |
| 76 | + ref={containerRef} |
| 77 | + className="flex gap-1.5 items-center p-2 rounded-full border bg-gray-3 border-gray-5" |
| 78 | + > |
| 79 | + {activeOrg?.organization.iconUrl ? ( |
| 80 | + <div className="overflow-hidden relative flex-shrink-0 rounded-full size-[24px]"> |
| 81 | + <Image |
| 82 | + src={activeOrg.organization.iconUrl} |
| 83 | + alt={activeOrg.organization.name || "Organization icon"} |
| 84 | + fill |
| 85 | + className="object-cover" |
| 86 | + /> |
| 87 | + </div> |
| 88 | + ) : ( |
| 89 | + <Avatar |
| 90 | + letterClass="text-xs" |
| 91 | + className="relative flex-shrink-0 mx-auto size-6" |
| 92 | + name={activeOrg?.organization.name ?? "No organization found"} |
| 93 | + /> |
| 94 | + )} |
| 95 | + <p className="text-xs mr-2 text-gray-12 truncate w-fit max-w-[90px]"> |
| 96 | + {activeOrg?.organization.name} |
| 97 | + </p> |
| 98 | + <ChevronDown |
| 99 | + className={clsx( |
| 100 | + "text-gray-11 size-4 transition-transform", |
| 101 | + open && "rotate-180", |
| 102 | + )} |
| 103 | + /> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +const OrgsMenu = ({ |
| 109 | + setOpen, |
| 110 | + menuRef, |
| 111 | +}: { |
| 112 | + setOpen: Dispatch<SetStateAction<boolean>>; |
| 113 | + menuRef: MutableRefObject<Element>; |
| 114 | +}) => { |
| 115 | + const { activeOrganization: activeOrg, organizationData: orgData } = |
| 116 | + useDashboardContext(); |
| 117 | + const router = useRouter(); |
| 118 | + return ( |
| 119 | + <motion.div |
| 120 | + initial={{ scale: 0.98, opacity: 0 }} |
| 121 | + animate={{ scale: 1, opacity: 1 }} |
| 122 | + exit={{ scale: 0.9, opacity: 0 }} |
| 123 | + transition={{ duration: 0.15 }} |
| 124 | + ref={menuRef as LegacyRef<HTMLDivElement>} |
| 125 | + className={ |
| 126 | + "isolate absolute overscroll-contain bottom-14 p-2 space-y-1.5 w-full rounded-xl h-fit border bg-gray-3 max-h-[325px] custom-scroll max-w-[200px] border-gray-4" |
| 127 | + } |
| 128 | + > |
| 129 | + {orgData?.map((organization) => { |
| 130 | + const isSelected = |
| 131 | + activeOrg?.organization.id === organization.organization.id; |
| 132 | + return ( |
| 133 | + <div |
| 134 | + className={clsx( |
| 135 | + "p-2 rounded-lg transition-colors duration-300 group", |
| 136 | + isSelected |
| 137 | + ? "pointer-events-none" |
| 138 | + : "text-gray-10 hover:text-gray-12 hover:bg-gray-6", |
| 139 | + )} |
| 140 | + key={organization.organization.name + "-organization"} |
| 141 | + onClick={async () => { |
| 142 | + await updateActiveOrganization(organization.organization.id); |
| 143 | + setOpen(false); |
| 144 | + router.push("/dashboard/caps"); |
| 145 | + }} |
| 146 | + > |
| 147 | + <div className="flex gap-2 items-center w-full"> |
| 148 | + {organization.organization.iconUrl ? ( |
| 149 | + <div className="overflow-hidden relative flex-shrink-0 rounded-full size-5"> |
| 150 | + <Image |
| 151 | + src={organization.organization.iconUrl} |
| 152 | + alt={organization.organization.name || "Organization icon"} |
| 153 | + fill |
| 154 | + className="object-cover" |
| 155 | + /> |
| 156 | + </div> |
| 157 | + ) : ( |
| 158 | + <Avatar |
| 159 | + letterClass="text-xs" |
| 160 | + className="relative flex-shrink-0 size-5" |
| 161 | + name={organization.organization.name} |
| 162 | + /> |
| 163 | + )} |
| 164 | + <p |
| 165 | + className={clsx( |
| 166 | + "flex-1 text-sm transition-colors duration-200 group-hover:text-gray-12", |
| 167 | + isSelected ? "text-gray-12" : "text-gray-10", |
| 168 | + )} |
| 169 | + > |
| 170 | + {organization.organization.name} |
| 171 | + </p> |
| 172 | + {isSelected && ( |
| 173 | + <Check size={18} className={"ml-auto text-gray-12"} /> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + ); |
| 178 | + })} |
| 179 | + </motion.div> |
| 180 | + ); |
| 181 | +}; |
| 182 | + |
| 183 | +export default MobileTab; |
0 commit comments