import { useState } from "react"; import { handleUpdateUserPermission,…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import { useState } from "react"; import { handleUpdateUserPermission, handleUpdateUserRoles, } from "../../utils/api"; import { toast } from "react-hot-toast"; import { capitalizeFirstLetter } from "../../utils/capitalizeFirstLetter";
interface UserAccessCardProps { user: { _id: string; userName: string; roles: string[]; permissions?: string[]; }; onUpdated: (updatedUser: any) => void; }
const ALL_ROLES = ["staff", "manager", "admin", "developer"]; const ALL_PERMISSIONS = [ "canViewReports", "canEditProduct", "canDeleteUser", "canChangeRole", "canManageOrders", ];
export default function UserAccessCard({ user, onUpdated, }: UserAccessCardProps) { const [roles, setRoles] = useState<string[]>(user.roles); const [permissions, setPermissions] = useState<string[]>( user.permissions || [] ); const [loading, setLoading] = useState(false);
const toggleRole = (role: string) => { setRoles((prev) => prev.includes(role) ? prev.filter((r) => r !== role) : [...prev, role] ); };
const togglePermission = (perm: string) => { setPermissions((prev) => prev.includes(perm) ? prev.filter((p) => p !== perm) : [...prev, perm] ); };
const handleUpdate = async () => { setLoading(true); try { const token = localStorage.getItem("token"); const userId = user._id;
await handleUpdateUserRoles({ roles, userId, token });
await handleUpdateUserPermission({
permissions,
token,
onUpdated,
userId,
});
toast.success("Access updated successfully!");
} catch (err) {
console.error("Update failed:", err);
toast.error("Failed to update access.");
} finally {
setLoading(false);
}
};
const isDeveloper = roles.includes("developer");
return ( {capitalizeFirstLetter(user.userName)} {/* ID: {user._id} */}
<div className="mb-4">
<p className="text-sm font-medium text-gray-700 mb-1">Roles:</p>
<div className="flex flex-wrap gap-2">
{ALL_ROLES.map((role) => (
<button
key={role}
type="button"
onClick={() => toggleRole(role)}
className={`px-3 py-1 rounded-full text-sm border ${
roles.includes(role)
? "bg-blue-600 text-white border-blue-600"
: "bg-gray-100 text-gray-700 border-gray-300"
}`}
>
{role}
</button>
))}
</div>
</div>
<div className="mb-4">
<p className="text-sm font-medium text-gray-700 mb-1">Permissions:</p>
{isDeveloper ? (
<div className="text-sm text-gray-500 italic">
Developer has all permissions by default.
</div>
) : (
<div className="grid grid-cols-2 gap-2">
{ALL_PERMISSIONS.map((perm) => (
<label key={perm} className="flex items-center gap-2 text-sm">
<input
type="checkbox"
checked={permissions.includes(perm)}
onChange={() => togglePermission(perm)}
className="accent-blue-600"
/>
{perm}
</label>
))}
</div>
)}
</div>
<div className="text-right">
{!isDeveloper && (
<button
onClick={handleUpdate}
disabled={loading}
className="bg-blue-600 text-white px-4 py-1.5 rounded hover:bg-blue-700 transition disabled:opacity-50"
>
{loading ? "Updating..." : "Update Access"}
</button>
)}
</div>
</div>
); }
this is my simple design. i need modern, smart, animation, professional and unique design
Below you’ll find a full, drop-in replacement for your component that keeps all of the logic you already wrote but wraps it in:
• A glassmorphic, gradient-border card
• Micro-interactions on hover / tap (scale + shadow)
• Smooth entrance / exit animations with Framer-Motion
• Animated chips / switches instead of plain buttons & checkboxes
• A shimmering skeleton while the roles / permissions are being fetched (so the UI never “jumps”)
Everything is still pure React + Tailwind CSS (no custom CSS files) with one additional dependency: framer-motion (only 33 kb gzip).
🛠 1. Install framer-motion
npm i framer-motion
# or
yarn add framer-motion
🛠 2. The polished component
// UserAccessCard.tsx
import { useState, useEffect } from "react";
import {
handleUpdateUserPermission,
handleUpdateUserRoles,
} from "../../utils/api";
import { toast } from "react-hot-toast";
import { capitalizeFirstLetter } from "../../utils/capitalizeFirstLetter";
import { motion, AnimatePresence } from "framer-motion";
interface UserAccessCardProps {
user: {
_id: string;
userName: string;
roles: string[];
permissions?: string[];
};
onUpdated: (updatedUser: any) => void;
}
const ALL_ROLES = ["staff", "manager", "admin", "developer"] as const;
const ALL_PERMISSIONS = [
"canViewReports",
"canEditProduct",
"canDeleteUser",
"canChangeRole",
"canManageOrders",
] as const;
export default function UserAccessCard({
user,
onUpdated,
}: UserAccessCardProps) {
const [roles, setRoles] = useState<string[]>([]);
const [permissions, setPermissions] = useState<string[]>([]);
const [loading, setLoading] = useState(false);
const [booting, setBooting] = useState(true); // skeleton
/* -------------------------------------------------- */
/* Initial data load */
/* -------------------------------------------------- */
useEffect(() => {
// simulate suspense-style boot delay (optional)
const timeout = setTimeout(() => {
setRoles(user.roles);
setPermissions(user.permissions ?? []);
setBooting(false);
}, 400); // 0.4s shimmering skeleton
return () => clearTimeout(timeout);
}, [user]);
/* -------------------------------------------------- */
/* Helpers / togglers */
/* -------------------------------------------------- */
const toggleRole = (role: string) =>
setRoles((prev) =>
prev.includes(role) ? prev.filter((r) => r !== role) : [...prev, role]
);
const togglePermission = (perm: string) =>
setPermissions((prev) =>
prev.includes(perm) ? prev.filter((p) => p !== perm) : [...prev, perm]
);
/* -------------------------------------------------- */
/* Update Call */
/* -------------------------------------------------- */
const handleUpdate = async () => {
setLoading(true);
try {
const token = localStorage.getItem("token");
const userId = user._id;
await handleUpdateUserRoles({ roles, userId, token });
await handleUpdateUserPermission({
permissions,
token,
onUpdated,
userId,
});
toast.success("Access updated successfully!");
} catch (err) {
console.error("Update failed:", err);
toast.error("Failed to update access.");
} finally {
setLoading(false);
}
};
const isDeveloper = roles.includes("developer");
/* -------------------------------------------------- */
/* Variants */
/* -------------------------------------------------- */
const cardVariants = {
hidden: { opacity: 0, y: 15, scale: 0.97 },
visible: {
opacity: 1,
y: 0,
scale: 1,
transition: { type: "spring", stiffness: 90, damping: 14 },
},
exit: { opacity: 0, y: 15, scale: 0.97 },
};
/* -------------------------------------------------- */
/* Render */
/* -------------------------------------------------- */
return (
<AnimatePresence mode="wait">
<motion.div
key={user._id} // triggers animation when user changes
variants={cardVariants}
initial="hidden"
animate="visible"
exit="exit"
whileHover={{ y: -2, boxShadow: "0 4px 20px rgba(0,0,0,0.08)" }}
className="relative overflow-hidden rounded-2xl p-[1px]
before:absolute before:inset-0 before:-z-10
before:bg-gradient-to-br before:from-purple-500 before:to-blue-500
before:blur-sm"
>
{/* Glass layer */}
<div className="rounded-[inherit] bg-white/70 backdrop-blur-xl p-6">
{/* ───────────────── Boot skeleton ───────────────── */}
{booting ? (
<Skeleton />
) : (
<>
{/* ------------ Header ------------ */}
<div className="mb-5 flex items-center justify-between">
<h3 className="text-xl font-semibold text-gray-800">
{capitalizeFirstLetter(user.userName)}
</h3>
{/* status dot */}
<span className="flex h-3 w-3">
<span className="absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75 animate-ping" />
<span className="relative inline-flex rounded-full h-3 w-3 bg-emerald-500"></span>
</span>
</div>
{/* ------------ Roles ------------ */}
<section className="mb-6">
<p className="mb-2 text-sm font-medium text-gray-600">
Roles
</p>
<div className="flex flex-wrap gap-2">
{ALL_ROLES.map((role) => (
<RoleChip
key={role}
role={role}
active={roles.includes(role)}
onClick={toggleRole}
/>
))}
</div>
</section>
{/* ------------ Permissions ------------ */}
<section className="mb-6">
<p className="mb-2 text-sm font-medium text-gray-600">
Permissions
</p>
{isDeveloper ? (
<p className="text-sm italic text-gray-500">
Developer has all permissions by default.
</p>
) : (
<div className="grid grid-cols-2 gap-3">
{ALL_PERMISSIONS.map((perm) => (
<PermissionSwitch
key={perm}
label={perm}
checked={permissions.includes(perm)}
onChange={() => togglePermission(perm)}
/>
))}
</div>
)}
</section>
{/* ------------ CTA ------------ */}
{!isDeveloper && (
<div className="text-right">
<motion.button
whileTap={{ scale: 0.95 }}
disabled={loading}
onClick={handleUpdate}
className="inline-flex items-center gap-2 rounded-md
bg-gradient-to-r from-blue-600 to-purple-600
px-5 py-2 text-sm font-medium text-white
shadow-lg transition
hover:from-blue-700 hover:to-purple-700
disabled:cursor-not-allowed disabled:opacity-50"
>
{loading ? (
<>
<svg
className="h-4 w-4 animate-spin"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="3"
d="M12 4v1m0 14v1m6-7h1M4 12H3m15.364 5.364l-.707-.707M6.343 6.343l-.707-.707m0 11.314l.707-.707M17.657 6.343l.707-.707"
/>
</svg>
Updating...
</>
) : (
"Update Access"
)}
</motion.button>
</div>
)}
</>
)}
</div>
</motion.div>
</AnimatePresence>
);
}
/* -------------------------------------------------------------------------- */
/* Helper Pieces */
/* -------------------------------------------------------------------------- */
/**
* Role chip with color coding + animation
*/
function RoleChip({
role,
active,
onClick,
}: {
role: string;
active: boolean;
onClick: (role: string) => void;
}) {
// Pick a hue per role (just for fun)
const colorMap: Record<string, string> = {
staff: "bg-sky-100 text-sky-700/90 border-sky-200",
manager: "bg-amber-100 text-amber-700/90 border-amber-200",
admin: "bg-rose-100 text-rose-700/90 border-rose-200",
developer: "bg-emerald-100 text-emerald-700/90 border-emerald-200",
};
return (
<motion.button
whileTap={{ scale: 0.9 }}
onClick={() => onClick(role)}
className={`select-none whitespace-nowrap rounded-full border px-3 py-1 text-xs font-medium
transition ${
active
? colorMap[role] ?? "bg-gray-200 text-gray-800 border-gray-300"
: "bg-white/60 text-gray-600 border-gray-300 hover:bg-gray-100"
}`}
>
{role}
</motion.button>
);
}
/**
* Permission switch – pure CSS switch w/ framer-motion “thumb”
*/
function PermissionSwitch({
label,
checked,
onChange,
}: {
label: string;
checked: boolean;
onChange: () => void;
}) {
return (
<label className="flex cursor-pointer items-center gap-3 text-sm">
{/* Switch */}
<div
className={`relative h-5 w-10 rounded-full transition-colors
${checked ? "bg-blue-600/90" : "bg-gray-300/70"}`}
onClick={onChange}
>
<motion.span
layout
className="absolute top-0.5 h-4 w-4 rounded-full bg-white shadow"
animate={{
x: checked ? 20 : 0,
}}
transition={{ type: "spring", stiffness: 600, damping: 30 }}
/>
</div>
<span className="truncate">{label}</span>
</label>
);
}
/**
* Simple shimmering skeleton
*/
function Skeleton() {
return (
<div className="space-y-4">
<div className="h-5 w-1/3 animate-pulse rounded bg-gray-300/60" />
<div className="h-4 w-24 animate-pulse rounded bg-gray-300/50" />
<div className="flex flex-wrap gap-2">
{Array.from({ length: 3 }).map((_, i) => (
<div
key={i}
className="h-7 w-20 animate-pulse rounded-full bg-gray-300/50"
/>
))}
</div>
<div className="grid grid-cols-2 gap-3">
{Array.from({ length: 4 }).map((_, i) => (
<div
key={i}
className="h-5 animate-pulse rounded bg-gray-300/40"
></div>
))}
</div>
<div className="h-9 w-32 animate-pulse rounded bg-gray-300/50" />
</div>
);
}
💡 3. What changed & why it feels “next-gen”
Glassmorphism + gradient border
• Uses Tailwind’s arbitrary value [1px] trick to paint a 1-pixel border.
• backdrop-blur-xl for the frosted effect.
Subtle motion
• Framer-motion for entrance (“spring”) + hover lift.
• Role chips & switch thumbs animate scale / position.
Chips instead of rectangles
• Contextual color per role (immediately scannable).
Inline SVG spinner & shimmering skeleton
• No flicker during first data set or update call.
One dependency, small footprint
• framer-motion is tree-shakeable; the rest is pure Tailwind.
Feel free to fine-tune gradients / colors to fit your brand palette. Enjoy the ✨ slicker ✨ access panel!