Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): reflect role and manage access across dashboard chrome

Richie McIlroy44d288ff91b5
diff --git a/apps/web/app/(org)/dashboard/_components/MobileTab.tsx b/apps/web/app/(org)/dashboard/_components/MobileTab.tsx
index eaf58e5e5..1cc764507 100644
--- a/apps/web/app/(org)/dashboard/_components/MobileTab.tsx
+++ b/apps/web/app/(org)/dashboard/_components/MobileTab.tsx
@@ -15,6 +15,10 @@ import {
useState,
} from "react";
import { SignedImageUrl } from "@/components/SignedImageUrl";
+import {
+ canViewOrganizationSettings,
+ getEffectiveOrganizationRole,
+} from "@/lib/permissions/roles";
import { useDashboardContext } from "../Contexts";
import { CapIcon, CogIcon, LayersIcon } from "./AnimatedIcons";
import { updateActiveOrganization } from "./Navbar/server";
@@ -25,15 +29,23 @@ const Tabs = [
{
icon: <CogIcon size={22} />,
href: "/dashboard/settings/organization",
- ownerOnly: true,
+ adminOnly: true,
},
];
const MobileTab = () => {
const [open, setOpen] = useState(false);
- const containerRef = useRef<HTMLDivElement>(null);
+ const containerRef = useRef<HTMLButtonElement>(null);
const { activeOrganization: activeOrg, user } = useDashboardContext();
- const isOwner = activeOrg?.organization.ownerId === user.id;
+ const currentMember = activeOrg?.members.find(
+ (member) => member.userId === user.id,
+ );
+ const currentRole = getEffectiveOrganizationRole({
+ userId: user.id,
+ ownerId: activeOrg?.organization.ownerId,
+ memberRole: currentMember?.role,
+ });
+ const canViewSettings = canViewOrganizationSettings(currentRole);
const menuRef = useClickAway((e) => {
if (
containerRef.current &&
@@ -51,7 +63,7 @@ const MobileTab = () => {
<Orgs open={open} setOpen={setOpen} containerRef={containerRef} />
</div>
<div className="flex gap-6 justify-between items-center h-full text-gray-11">
- {Tabs.filter((i) => !i.ownerOnly || isOwner).map((tab) => (
+ {Tabs.filter((i) => !i.adminOnly || canViewSettings).map((tab) => (
<Link href={tab.href} key={tab.href}>
{tab.icon}
</Link>
@@ -68,11 +80,12 @@ const Orgs = ({
}: {
setOpen: Dispatch<SetStateAction<boolean>>;
open: boolean;
- containerRef: MutableRefObject<HTMLDivElement | null>;
+ containerRef: MutableRefObject<HTMLButtonElement | null>;
}) => {
const { activeOrganization: activeOrg } = useDashboardContext();
return (
- <div
+ <button
+ type="button"
onClick={() => setOpen((p) => !p)}
ref={containerRef}
className="flex gap-1.5 items-center flex-auto max-w-[224px] p-2 rounded-full border bg-gray-3 border-gray-5"
@@ -92,7 +105,7 @@ const Orgs = ({
open && "rotate-180",
)}
/>
- </div>
+ </button>
);
};
@@ -121,9 +134,10 @@ const OrgsMenu = ({
const isSelected =
activeOrg?.organization.id === organization.organization.id;
return (
- <div
+ <button
+ type="button"
className={clsx(
- "p-2 rounded-lg transition-colors duration-300 group",
+ "p-2 rounded-lg transition-colors duration-300 group w-full text-left",
isSelected
? "pointer-events-none"
: "text-gray-10 hover:text-gray-12 hover:bg-gray-6",
@@ -154,7 +168,7 @@ const OrgsMenu = ({
<Check size={18} className={"ml-auto text-gray-12"} />
)}
</div>
- </div>
+ </button>
);
})}
</motion.div>
diff --git a/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx b/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx
index 0e7849d69..ed19a9507 100644
--- a/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx
+++ b/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx
@@ -34,6 +34,10 @@ import { NewOrganization } from "@/components/forms/NewOrganization";
import { SignedImageUrl } from "@/components/SignedImageUrl";
import { Tooltip } from "@/components/Tooltip";
import { UsageButton } from "@/components/UsageButton";
+import {
+ canViewOrganizationSettings,
+ getEffectiveOrganizationRole,
+} from "@/lib/permissions/roles";
import { useDashboardContext } from "../../Contexts";
import {
CapIcon,
@@ -94,7 +98,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
{
name: "Organization Settings",
href: `/dashboard/settings/organization`,
- ownerOnly: true,
+ adminOnly: true,
matchChildren: true,
icon: <CogIcon />,
subNav: [],
@@ -120,6 +124,15 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
const [createLoading, setCreateLoading] = useState(false);
const [organizationName, setOrganizationName] = useState("");
const isOwner = activeOrg?.organization.ownerId === user.id;
+ const currentMember = activeOrg?.members.find(
+ (member) => member.userId === user.id,
+ );
+ const currentRole = getEffectiveOrganizationRole({
+ userId: user.id,
+ ownerId: activeOrg?.organization.ownerId,
+ memberRole: currentMember?.role,
+ });
+ const canViewSettings = canViewOrganizationSettings(currentRole);
const [_openAIDialog, _setOpenAIDialog] = useState(false);
const router = useRouter();
@@ -161,6 +174,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
)}
role="combobox"
aria-expanded={open}
+ tabIndex={0}
>
<div
className={clsx(
@@ -315,6 +329,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
>
{manageNavigation
.filter((item) => !item.ownerOnly || isOwner)
+ .filter((item) => !item.adminOnly || canViewSettings)
.map((item) => (
<div
key={item.name}
@@ -336,7 +351,6 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
},
}}
layoutId="navlinks"
- id="navlinks"
className="absolute h-[36px] w-full rounded-xl pointer-events-none bg-gray-3"
/>
)}
diff --git a/apps/web/app/(org)/dashboard/_components/Navbar/MemberAvatars.tsx b/apps/web/app/(org)/dashboard/_components/Navbar/MemberAvatars.tsx
index e17555c90..72cf717ca 100644
--- a/apps/web/app/(org)/dashboard/_components/Navbar/MemberAvatars.tsx
+++ b/apps/web/app/(org)/dashboard/_components/Navbar/MemberAvatars.tsx
@@ -3,6 +3,10 @@
import { Plus } from "lucide-react";
import { SignedImageUrl } from "@/components/SignedImageUrl";
import { Tooltip } from "@/components/Tooltip";
+import {
+ canManageOrganizationMembers,
+ getEffectiveOrganizationRole,
+} from "@/lib/permissions/roles";
import { useDashboardContext } from "../../Contexts";
const MAX_VISIBLE = 4;
@@ -11,7 +15,15 @@ export function MemberAvatars() {
const { activeOrganization, sidebarCollapsed, setInviteDialogOpen, user } =
useDashboardContext();
- const isOwner = user?.id === activeOrganization?.organization.ownerId;
+ const currentMember = activeOrganization?.members.find(
+ (member) => member.userId === user?.id,
+ );
+ const currentRole = getEffectiveOrganizationRole({
+ userId: user?.id,
+ ownerId: activeOrganization?.organization.ownerId,
+ memberRole: currentMember?.role,
+ });
+ const canInviteMembers = canManageOrganizationMembers(currentRole);
if (sidebarCollapsed) return null;
@@ -19,6 +31,12 @@ export function MemberAvatars() {
const visibleMembers = members.slice(0, MAX_VISIBLE);
const extraCount = members.length - MAX_VISIBLE;
const emptySlots = Math.max(0, MAX_VISIBLE - members.length);
+ const emptySlotKeys = [
+ "empty-slot-1",
+ "empty-slot-2",
+ "empty-slot-3",
+ "empty-slot-4",
+ ].slice(0, emptySlots);
return (
<div className="flex items-center mt-2.5 px-2.5">
@@ -48,10 +66,10 @@ export function MemberAvatars() {
</div>
)}
- {isOwner &&
- Array.from({ length: emptySlots }).map((_, i) => (
+ {canInviteMembers &&
+ emptySlotKeys.map((slotKey) => (
<Tooltip
- key={`empty-${i}`}
+ key={slotKey}
content="Invite to your organization"
position="bottom"
delayDuration={0}
diff --git a/apps/web/app/(org)/dashboard/_components/Navbar/SpacesList.tsx b/apps/web/app/(org)/dashboard/_components/Navbar/SpacesList.tsx
index 37dd3e13b..9b7639468 100644
--- a/apps/web/app/(org)/dashboard/_components/Navbar/SpacesList.tsx
+++ b/apps/web/app/(org)/dashboard/_components/Navbar/SpacesList.tsx
@@ -28,7 +28,7 @@ import { ConfirmationDialog } from "../ConfirmationDialog";
import SpaceDialog from "./SpaceDialog";
const SpacesList = ({ toggleMobileNav }: { toggleMobileNav?: () => void }) => {
- const { spacesData, sidebarCollapsed, user } = useDashboardContext();
+ const { spacesData, sidebarCollapsed } = useDashboardContext();
const [showSpaceDialog, setShowSpaceDialog] = useState(false);
const [showAllSpaces, setShowAllSpaces] = useState(false);
const [activeDropTarget, setActiveDropTarget] = useState<string | null>(null);
@@ -107,7 +107,6 @@ const SpacesList = ({ toggleMobileNav }: { toggleMobileNav?: () => void }) => {
const cap = JSON.parse(capData);
- // Call the share action with just this space ID
const result = await shareCap({
capId: cap.id,
spaceIds: [spaceId],
@@ -206,7 +205,6 @@ const SpacesList = ({ toggleMobileNav }: { toggleMobileNav?: () => void }) => {
</Link>
</Tooltip>
- {/* Wrapper div with overflow hidden to prevent scrollbar flash */}
<div className="overflow-hidden">
<div
className={clsx(
@@ -222,7 +220,6 @@ const SpacesList = ({ toggleMobileNav }: { toggleMobileNav?: () => void }) => {
}}
>
{displayedSpaces.map((space: Spaces) => {
- const isOwner = space.createdById === user?.id;
return (
<Tooltip
position="right"
@@ -303,8 +300,7 @@ const SpacesList = ({ toggleMobileNav }: { toggleMobileNav?: () => void }) => {
className="ml-1.5 size-2.5 text-amber-600"
/>
)}
- {/* Hide delete button for 'All spaces' synthetic entry */}
- {!space.primary && isOwner && (
+ {!space.primary && space.currentUserCanManage && (
<button
type="button"
onClick={(e) => handleDeleteSpace(e, space)}