DiffWeave / studio /src /components /UserProfileView.jsx
shak3008's picture
feat(notifications): add dynamic bell notification center with collaborator request approval and fix profile workspace privacy
36d7087
Raw History Blame Contribute Delete
22.2 kB
import React, { useState, useEffect } from 'react';
import {
User,
Files,
Database,
GitPullRequest,
Heart,
Globe,
Lock,
Calendar,
CheckCircle2,
Clock,
ArrowRight,
ExternalLink,
Search,
Filter,
Activity,
FileText,
Shield,
Layers,
Sparkles,
ArrowLeft,
X,
Share2,
Copy
} from 'lucide-react';
import { api } from '../api/client';
export default function UserProfileView({
userIdOrUsername,
onClose,
onSelectWorkspace,
currentUser
}) {
const [profileData, setProfileData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const [activeTab, setActiveTab] = useState('workspaces'); // 'workspaces' | 'activity'
const [workspaceSearch, setWorkspaceSearch] = useState('');
const [activityFilter, setActivityFilter] = useState('ALL');
const [copiedId, setCopiedId] = useState(false);
const [requestingAccess, setRequestingAccess] = useState({});
useEffect(() => {
if (userIdOrUsername) {
loadProfile(userIdOrUsername);
}
}, [userIdOrUsername]);
const loadProfile = async (target) => {
setLoading(true);
setError('');
try {
const data = await api.getUserProfile(target);
setProfileData(data);
} catch (err) {
setError(`Could not load profile: ${err.message}`);
} finally {
setLoading(false);
}
};
const handleCopyId = () => {
if (!profileData?.user?.id) return;
navigator.clipboard.writeText(profileData.user.id);
setCopiedId(true);
setTimeout(() => setCopiedId(false), 2000);
};
const handleRequestAccess = async (workspaceId) => {
setRequestingAccess((prev) => ({ ...prev, [workspaceId]: true }));
try {
await api.requestWorkspaceCollab(workspaceId, 'REQUESTED_COLLAB');
// Update local state to show pending
setProfileData((prev) => {
if (!prev) return prev;
return {
...prev,
workspaces: prev.workspaces.map((w) =>
w.id === workspaceId ? { ...w, viewer_status: 'PENDING' } : w
),
};
});
} catch (err) {
alert(`Could not request access: ${err.message || err}`);
} finally {
setRequestingAccess((prev) => ({ ...prev, [workspaceId]: false }));
}
};
if (loading) {
return (
<div className="max-w-6xl mx-auto px-4 py-16 flex flex-col items-center justify-center space-y-4">
<div className="w-10 h-10 border-2 border-emerald-500 border-t-transparent rounded-full animate-spin" />
<p className="text-xs text-slate-400 font-mono">Loading user profile & workspace history...</p>
</div>
);
}
if (error || !profileData?.user) {
return (
<div className="max-w-xl mx-auto my-12 p-8 rounded-2xl bg-[#090D16] border border-slate-800 text-center space-y-4">
<User className="w-10 h-10 text-slate-500 mx-auto" />
<h3 className="text-sm font-bold text-white">Profile Not Found</h3>
<p className="text-xs text-slate-400">{error || 'This user does not exist or has no public profile.'}</p>
{onClose && (
<button
onClick={onClose}
className="px-4 py-2 rounded-xl bg-slate-800 text-xs text-slate-200 hover:text-white transition"
>
Go Back
</button>
)}
</div>
);
}
const { user, stats, workspaces = [], activities = [] } = profileData;
// Filter workspaces
const filteredWorkspaces = workspaces.filter((w) => {
const q = workspaceSearch.toLowerCase().trim();
return !q || w.name.toLowerCase().includes(q) || (w.description || '').toLowerCase().includes(q);
});
// Filter activities
const filteredActivities = activities.filter((act) => {
if (activityFilter === 'ALL') return true;
if (activityFilter === 'DOCUMENTS') return act.type === 'DOCUMENT_STAGED';
if (activityFilter === 'PR_REVIEWS') return act.type === 'PR_REVIEW';
if (activityFilter === 'FACTS') return act.type === 'FACT_EXTRACTED';
return true;
});
return (
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 space-y-8 animate-in fade-in duration-200">
{/* Top Navigation / Breadcrumb */}
{onClose && (
<div className="flex items-center justify-between pb-2 border-b border-slate-800/80">
<button
onClick={onClose}
className="text-xs text-slate-400 hover:text-white transition flex items-center space-x-1.5 font-semibold"
>
<ArrowLeft className="w-4 h-4" />
<span>Back to Explorer</span>
</button>
<span className="text-[11px] text-slate-500 font-mono">Public User Profile</span>
</div>
)}
{/* GitHub-style Header Profile Card */}
<div className="bg-[#090D16] border border-slate-800 rounded-3xl p-6 sm:p-8 relative overflow-hidden shadow-2xl">
{/* Ambient Gradient Glow */}
<div className="absolute top-0 right-0 w-80 h-80 bg-gradient-to-br from-emerald-500/10 via-sky-500/5 to-transparent rounded-full blur-3xl pointer-events-none" />
<div className="flex flex-col md:flex-row md:items-center justify-between gap-6 relative z-10">
{/* User Info */}
<div className="flex items-start space-x-5">
<div className="w-20 h-20 sm:w-24 sm:h-24 rounded-2xl bg-gradient-to-tr from-emerald-500 via-teal-500 to-sky-500 p-[2px] shadow-xl shrink-0">
<div className="w-full h-full bg-[#060910] rounded-[14px] flex items-center justify-center text-3xl font-black text-emerald-400 font-mono">
{(user.username || user.email || 'U')[0].toUpperCase()}
</div>
</div>
<div className="space-y-1.5">
<div className="flex flex-wrap items-center gap-2.5">
<h1 className="text-xl sm:text-2xl font-bold text-white tracking-tight">
{user.username || 'Collaborator'}
</h1>
<span className="px-2.5 py-0.5 rounded-full text-[10px] font-mono font-bold uppercase bg-emerald-500/10 text-emerald-400 border border-emerald-500/30">
{user.role || 'USER'}
</span>
{user.is_self && (
<span className="px-2 py-0.5 rounded text-[10px] font-mono bg-sky-500/10 text-sky-400 border border-sky-500/30">
YOU
</span>
)}
</div>
<p className="text-xs text-slate-400 font-mono">{user.email}</p>
<div className="flex items-center space-x-3 text-[11px] text-slate-500 font-mono pt-1">
<button
onClick={handleCopyId}
className="hover:text-slate-300 transition flex items-center space-x-1"
title="Copy User ID"
>
<Copy className="w-3 h-3 text-slate-500" />
<span>ID: {user.id.slice(0, 8)}...</span>
{copiedId && <span className="text-emerald-400 font-bold">&check;</span>}
</button>
<span>&bull;</span>
<span className="flex items-center space-x-1">
<Globe className="w-3 h-3 text-slate-500" />
<span>Public Contributor</span>
</span>
</div>
</div>
</div>
{/* Quick Metrics Bar */}
<div className="grid grid-cols-2 sm:grid-cols-4 gap-3 bg-[#0D121F] border border-slate-800/80 rounded-2xl p-4 shrink-0">
<div className="text-center px-3">
<span className="block text-xl font-extrabold text-white font-mono">
{stats.total_workspaces}
</span>
<span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
Workspaces
</span>
</div>
<div className="text-center px-3 border-l border-slate-800">
<span className="block text-xl font-extrabold text-sky-400 font-mono">
{stats.total_documents}
</span>
<span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
Documents
</span>
</div>
<div className="text-center px-3 border-l border-slate-800">
<span className="block text-xl font-extrabold text-emerald-400 font-mono">
{stats.total_facts}
</span>
<span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
Facts Verified
</span>
</div>
<div className="text-center px-3 border-l border-slate-800">
<span className="block text-xl font-extrabold text-violet-400 font-mono">
{stats.total_reviews}
</span>
<span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
PR Reviews
</span>
</div>
</div>
</div>
</div>
{/* Main Tabs Navigation */}
<div className="flex border-b border-slate-800 text-xs font-semibold">
<button
onClick={() => setActiveTab('workspaces')}
className={`pb-3 px-4 border-b-2 transition flex items-center space-x-2 ${
activeTab === 'workspaces'
? 'border-emerald-400 text-white'
: 'border-transparent text-slate-400 hover:text-slate-200'
}`}
>
<Layers className="w-4 h-4 text-emerald-400" />
<span>Workspaces & Repositories</span>
<span className="px-2 py-0.2 rounded-full bg-slate-800 text-slate-300 font-mono text-[10px]">
{workspaces.length}
</span>
</button>
<button
onClick={() => setActiveTab('activity')}
className={`pb-3 px-4 border-b-2 transition flex items-center space-x-2 ${
activeTab === 'activity'
? 'border-emerald-400 text-white'
: 'border-transparent text-slate-400 hover:text-slate-200'
}`}
>
<Activity className="w-4 h-4 text-violet-400" />
<span>Activity & Workspace Details</span>
<span className="px-2 py-0.2 rounded-full bg-slate-800 text-slate-300 font-mono text-[10px]">
{activities.length}
</span>
</button>
</div>
{/* TAB 1: WORKSPACES */}
{activeTab === 'workspaces' && (
<div className="space-y-4">
{/* Search bar inside workspaces */}
<div className="flex items-center justify-between gap-3">
<div className="relative flex-1 max-w-sm">
<Search className="w-3.5 h-3.5 text-slate-500 absolute left-3 top-1/2 -translate-y-1/2" />
<input
type="text"
placeholder="Find a workspace..."
value={workspaceSearch}
onChange={(e) => setWorkspaceSearch(e.target.value)}
className="w-full bg-[#090D16] border border-slate-800 rounded-xl pl-9 pr-3 py-1.5 text-xs text-white placeholder-slate-500 focus:outline-none focus:border-emerald-500 font-mono"
/>
</div>
<span className="text-[11px] text-slate-500 font-mono">
Showing {filteredWorkspaces.length} of {workspaces.length} workspaces
</span>
</div>
{filteredWorkspaces.length === 0 ? (
<div className="py-12 text-center text-xs text-slate-500 border border-dashed border-slate-800 rounded-2xl">
No matching workspaces found.
</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{filteredWorkspaces.map((ws) => (
<div
key={ws.id}
className="bg-[#090D16] border border-slate-800/90 hover:border-slate-700 rounded-2xl p-5 space-y-3 transition group flex flex-col justify-between"
>
<div className="space-y-2">
<div className="flex items-start justify-between gap-2">
<div className="flex items-center space-x-2 min-w-0">
<div className={`p-1.5 rounded-lg shrink-0 ${
ws.visibility === 'public'
? 'bg-sky-500/10 text-sky-400 border border-sky-500/20'
: 'bg-amber-500/10 text-amber-400 border border-amber-500/20'
}`}>
{ws.visibility === 'public' ? (
<Globe className="w-4 h-4" />
) : (
<Lock className="w-4 h-4" />
)}
</div>
<h3 className="font-bold text-sm text-white group-hover:text-emerald-300 transition truncate">
{ws.name}
</h3>
</div>
<div className="flex items-center space-x-1.5 shrink-0">
<span className={`px-2 py-0.5 rounded text-[9px] font-mono font-bold uppercase ${
ws.visibility === 'public'
? 'bg-sky-500/10 text-sky-400 border border-sky-500/20'
: 'bg-amber-500/10 text-amber-400 border border-amber-500/20'
}`}>
{ws.visibility}
</span>
<span className="px-1.5 py-0.5 rounded text-[9px] font-mono bg-slate-800 text-slate-400">
{ws.user_role}
</span>
</div>
</div>
<p className="text-xs text-slate-400 line-clamp-2 leading-relaxed">
{ws.description || 'Deterministic document intelligence and master knowledge register.'}
</p>
</div>
<div className="pt-3 border-t border-slate-800/80 flex items-center justify-between text-[11px] text-slate-400 font-mono">
<div className="flex items-center space-x-3">
<span className="flex items-center space-x-1">
<Files className="w-3.5 h-3.5 text-slate-500" />
<span>{ws.doc_count} docs</span>
</span>
<span>&bull;</span>
<span className="flex items-center space-x-1">
<Database className="w-3.5 h-3.5 text-emerald-400" />
<span>{ws.knowledge_count} facts</span>
</span>
{ws.pending_pr_count > 0 && (
<>
<span>&bull;</span>
<span className="text-amber-400 font-semibold">
{ws.pending_pr_count} PRs
</span>
</>
)}
</div>
{ws.is_accessible ? (
<button
onClick={() => onSelectWorkspace && onSelectWorkspace(ws)}
className="px-3 py-1 rounded-lg bg-emerald-600 hover:bg-emerald-500 text-white font-semibold text-xs transition flex items-center space-x-1"
>
<span>Open</span>
<ArrowRight className="w-3 h-3" />
</button>
) : (
<div className="flex items-center space-x-2">
{ws.viewer_status === 'PENDING' ? (
<span className="text-[10px] text-amber-400 bg-amber-500/10 border border-amber-500/20 px-2 py-1 rounded-md font-mono">
Pending Approval
</span>
) : (
<button
onClick={() => handleRequestAccess(ws.id)}
disabled={Boolean(requestingAccess[ws.id])}
className="px-2.5 py-1 rounded-lg bg-teal-600 hover:bg-teal-500 text-white font-semibold text-[11px] transition flex items-center space-x-1 disabled:opacity-50"
>
<span>{requestingAccess[ws.id] ? 'Requesting...' : 'Request Access'}</span>
</button>
)}
</div>
)}
</div>
</div>
))}
</div>
)}
</div>
)}
{/* TAB 2: DETAILED ACTIVITY TRAIL ("literally every detail about what they did in that workspace") */}
{activeTab === 'activity' && (
<div className="space-y-4">
{/* Activity Filters */}
<div className="flex flex-wrap items-center justify-between gap-3 text-xs">
<div className="flex items-center space-x-1.5">
<span className="text-slate-500 font-mono text-[11px] mr-1">Activity Type:</span>
{[
{ id: 'ALL', label: 'All Activities' },
{ id: 'DOCUMENTS', label: 'Documents Staged' },
{ id: 'PR_REVIEWS', label: 'PR Reviews' },
{ id: 'FACTS', label: 'Facts Extracted' }
].map((filter) => (
<button
key={filter.id}
onClick={() => setActivityFilter(filter.id)}
className={`px-2.5 py-1 rounded-lg text-[11px] font-semibold transition ${
activityFilter === filter.id
? 'bg-emerald-500/10 text-emerald-300 border border-emerald-500/30'
: 'text-slate-400 hover:text-white bg-slate-900 border border-slate-800'
}`}
>
{filter.label}
</button>
))}
</div>
<span className="text-[11px] text-slate-500 font-mono">
{filteredActivities.length} actions recorded
</span>
</div>
{filteredActivities.length === 0 ? (
<div className="py-12 text-center text-xs text-slate-500 border border-dashed border-slate-800 rounded-2xl">
No matching activity recorded for this filter.
</div>
) : (
<div className="divide-y divide-slate-800/80 bg-[#090D16] border border-slate-800 rounded-2xl overflow-hidden">
{filteredActivities.map((act) => {
let badgeTheme = {
bg: 'bg-slate-800',
text: 'text-slate-300',
icon: Activity,
label: 'ACTIVITY'
};
if (act.type === 'DOCUMENT_STAGED') {
badgeTheme = {
bg: 'bg-blue-500/10 border-blue-500/30 text-blue-400',
icon: FileText,
label: 'DOCUMENT STAGED'
};
} else if (act.type === 'PR_REVIEW') {
badgeTheme = {
bg: 'bg-violet-500/10 border-violet-500/30 text-violet-400',
icon: GitPullRequest,
label: 'PR REVIEW'
};
} else if (act.type === 'FACT_EXTRACTED') {
badgeTheme = {
bg: 'bg-emerald-500/10 border-emerald-500/30 text-emerald-400',
icon: Sparkles,
label: 'FACT VERIFIED'
};
}
const IconComponent = badgeTheme.icon;
return (
<div
key={act.id}
className="p-4 sm:p-5 flex flex-col sm:flex-row sm:items-center justify-between gap-3 hover:bg-[#0D121F] transition text-xs"
>
<div className="flex items-start space-x-3.5">
<div className={`p-2 rounded-xl border shrink-0 mt-0.5 ${badgeTheme.bg}`}>
<IconComponent className="w-4 h-4" />
</div>
<div className="space-y-1">
<div className="flex flex-wrap items-center gap-2">
<span className={`px-2 py-0.2 rounded text-[9px] font-mono font-bold border ${badgeTheme.bg}`}>
{badgeTheme.label}
</span>
<span className="text-slate-400 font-mono text-[11px]">in</span>
<span className="font-bold text-white font-mono">
{act.workspace_name}
</span>
</div>
<p className="text-slate-200 text-xs font-medium leading-relaxed">
{act.description}
</p>
</div>
</div>
<div className="flex items-center space-x-3 shrink-0 self-end sm:self-center font-mono text-[11px] text-slate-500">
<Clock className="w-3 h-3 text-slate-600" />
<span>{act.timestamp ? new Date(act.timestamp).toLocaleDateString() : 'recent'}</span>
{onSelectWorkspace && (
<button
onClick={() => onSelectWorkspace({ id: act.workspace_id, name: act.workspace_name })}
className="px-2 py-1 rounded bg-slate-800 hover:bg-slate-700 text-slate-300 hover:text-white transition flex items-center space-x-1"
>
<span>Go</span>
<ArrowRight className="w-3 h-3" />
</button>
)}
</div>
</div>
);
})}
</div>
)}
</div>
)}
</div>
);
}