Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | 5x 5x 130x 130x 130x 130x 130x 72x 130x 8x 8x 8x 8x 8x 5x 5x 8x 130x 6x 6x 6x 6x 130x 187x 59x 128x 187x 130x 18x 187x 6x 1x 6x 2x 22x 10x 107x 1x 1x | import { useState, useMemo } from "react";
import { Plus, X, Shield } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { Button } from "../ui/button";
import type { UserTenantRole, Pro } from "../../lib/api";
const PLATFORM_ROLES = ["admin", "super_admin"];
const PRO_ROLES = ["owner", "manager", "staff"];
interface UserRolesSectionProps {
roles: UserTenantRole[];
pros: Pro[];
onAddRole: (role: {
tenantType: "platform" | "pro";
tenantId?: string;
role: string;
}) => Promise<void>;
onRemoveRole: (roleId: number) => Promise<void>;
}
export function UserRolesSection({
roles,
pros,
onAddRole,
onRemoveRole,
}: UserRolesSectionProps) {
const [showAddRole, setShowAddRole] = useState(false);
const [newRole, setNewRole] = useState({
tenantType: "platform" as "platform" | "pro",
tenantId: "",
role: "",
});
const [isAddingRole, setIsAddingRole] = useState(false);
const [removingRoleId, setRemovingRoleId] = useState<number | null>(null);
// O(1) pro lookup by ID
const proMap = useMemo(
() => new Map(pros.map((p) => [p.id, p])),
[pros],
);
const handleAddRole = async () => {
Iif (!newRole.role) return;
Iif (newRole.tenantType === "pro" && !newRole.tenantId) return;
setIsAddingRole(true);
try {
await onAddRole({
tenantType: newRole.tenantType,
tenantId:
newRole.tenantType === "pro" ? newRole.tenantId : undefined,
role: newRole.role,
});
setShowAddRole(false);
setNewRole({ tenantType: "platform", tenantId: "", role: "" });
} catch {
// Error is handled by parent component, keep modal open so user can retry
} finally {
setIsAddingRole(false);
}
};
const handleRemoveRole = async (roleId: number) => {
setRemovingRoleId(roleId);
try {
await onRemoveRole(roleId);
} catch {
// Error is handled by parent component
} finally {
setRemovingRoleId(null);
}
};
const getRoleDisplay = (role: UserTenantRole) => {
if (role.tenantType === "platform") {
return `Platform: ${role.role.replace("_", " ")}`;
}
const pro = proMap.get(role.tenantId ?? "");
return `Pro (${pro?.businessName || role.tenantId}): ${role.role}`;
};
return (
<>
<Card>
<CardHeader className="flex flex-row items-center justify-between">
<CardTitle className="text-lg">
<Shield className="h-5 w-5 inline mr-2" />
Roles ({roles.length})
</CardTitle>
<Button size="sm" onClick={() => setShowAddRole(true)}>
<Plus className="h-4 w-4 mr-1" />
Add Role
</Button>
</CardHeader>
<CardContent>
{roles.length === 0 ? (
<p className="text-foreground-muted text-center py-8">
No roles assigned. Click "Add Role" to assign one.
</p>
) : (
<div className="space-y-2">
{roles.map((role) => (
<div
key={role.id}
className="flex items-center justify-between p-3 bg-background-muted rounded-lg"
>
<div>
<span
className={`inline-block px-2 py-1 text-xs font-medium rounded-full mr-2 ${
role.tenantType === "platform"
? "bg-secondary-100 text-secondary-800"
: "bg-primary-100 text-primary-800"
}`}
>
{role.tenantType}
</span>
<span className="text-foreground-default">
{getRoleDisplay(role)}
</span>
</div>
<button
type="button"
onClick={() => handleRemoveRole(role.id)}
disabled={removingRoleId === role.id}
className="p-1 text-error hover:bg-error-light rounded disabled:opacity-50"
title="Remove role"
>
{removingRoleId === role.id ? (
<span className="block h-4 w-4 border-2 border-error border-t-transparent rounded-full animate-spin" />
) : (
<X className="h-4 w-4" />
)}
</button>
</div>
))}
</div>
)}
</CardContent>
</Card>
{/* Add Role Modal */}
{showAddRole && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
<div className="bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4">
<div className="flex items-center justify-between p-4 border-b border-border-default">
<h3 className="text-lg font-semibold">Add Role</h3>
<button
type="button"
onClick={() => setShowAddRole(false)}
className="p-1 hover:bg-background-muted rounded"
>
<X className="h-5 w-5" />
</button>
</div>
<div className="p-4 space-y-4">
<div>
<label
htmlFor="role-type"
className="block text-sm font-medium text-foreground-default mb-1"
>
Role Type
</label>
<select
id="role-type"
value={newRole.tenantType}
onChange={(e) =>
setNewRole({
...newRole,
tenantType: e.target.value as "platform" | "pro",
role: "",
tenantId: "",
})
}
className="h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
>
<option value="platform">Platform (Admin)</option>
<option value="pro">Pro (Team Member)</option>
</select>
</div>
{newRole.tenantType === "pro" && (
<div>
<label
htmlFor="pro-select"
className="block text-sm font-medium text-foreground-default mb-1"
>
Pro
</label>
<select
id="pro-select"
value={newRole.tenantId}
onChange={(e) =>
setNewRole({ ...newRole, tenantId: e.target.value })
}
className="h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
>
<option value="">Select pro</option>
{pros.map((pro) => (
<option key={pro.id} value={pro.id}>
{pro.businessName}
</option>
))}
</select>
</div>
)}
<div>
<label
htmlFor="role-select"
className="block text-sm font-medium text-foreground-default mb-1"
>
Role
</label>
<select
id="role-select"
value={newRole.role}
onChange={(e) =>
setNewRole({ ...newRole, role: e.target.value })
}
className="h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
>
<option value="">Select role</option>
{(newRole.tenantType === "platform"
? PLATFORM_ROLES
: PRO_ROLES
).map((role) => (
<option key={role} value={role}>
{role.replace("_", " ")}
</option>
))}
</select>
</div>
</div>
<div className="flex justify-end gap-2 p-4 border-t border-border-default">
<Button
variant="outline"
onClick={() => {
setShowAddRole(false);
setNewRole({
tenantType: "platform",
tenantId: "",
role: "",
});
}}
>
Cancel
</Button>
<Button
onClick={handleAddRole}
isLoading={isAddingRole}
disabled={
!newRole.role ||
(newRole.tenantType === "pro" && !newRole.tenantId)
}
>
Add Role
</Button>
</div>
</div>
</div>
)}
</>
);
}
|