203 lines
7.6 KiB
TypeScript
203 lines
7.6 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import Link from 'next/link';
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
|
import { StatusDot } from '@/components/status-badge';
|
|
import { ProgressRing } from '@/components/progress-ring';
|
|
import { useUser } from '@/components/user-context';
|
|
import { calculateProgress, getDaysRemaining, formatDateRange, isKRActive, getTodayInTokyo } from '@/lib/kr-utils';
|
|
import { PERIOD_TYPES, REASON_CODES } from '@/lib/constants';
|
|
import type { KR, User } from '@/types/database';
|
|
import { Target, Clock, AlertTriangle, CheckCircle, History } from 'lucide-react';
|
|
|
|
interface KRWithRelations extends KR {
|
|
owner: User | null;
|
|
project: { id: string; name: string; status: string } | null;
|
|
}
|
|
|
|
export default function MyKRsPage() {
|
|
const { currentUser } = useUser();
|
|
const [krs, setKRs] = useState<KRWithRelations[]>([]);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
async function fetchKRs() {
|
|
try {
|
|
const res = await fetch('/api/krs');
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
setKRs(data);
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to fetch KRs:', error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}
|
|
|
|
fetchKRs();
|
|
}, []);
|
|
|
|
const myKRs = krs.filter(kr => kr.owner_user_id === currentUser?.id);
|
|
const activeKRs = myKRs.filter(isKRActive);
|
|
const completedKRs = activeKRs.filter(kr => calculateProgress(kr) >= 1);
|
|
const pastKRs = myKRs.filter(kr => !isKRActive(kr));
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="max-w-5xl mx-auto px-4 py-8">
|
|
<div className="animate-pulse space-y-6">
|
|
<div className="h-8 bg-slate-200 rounded w-32" />
|
|
<div className="space-y-4">
|
|
{[1, 2, 3].map(i => (
|
|
<div key={i} className="h-24 bg-slate-200 rounded-lg" />
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="max-w-5xl mx-auto px-4 py-8">
|
|
<div className="mb-8">
|
|
<h1 className="text-2xl font-bold text-slate-800">我的 KR</h1>
|
|
<p className="text-muted-foreground mt-1">
|
|
{currentUser?.name} 负责的执行目标
|
|
</p>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-3 gap-4 mb-8">
|
|
<Card>
|
|
<CardContent className="pt-6 text-center">
|
|
<Target className="h-8 w-8 text-slate-300 mx-auto mb-2" />
|
|
<p className="text-3xl font-bold text-slate-800">{activeKRs.length}</p>
|
|
<p className="text-sm text-muted-foreground">进行中</p>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardContent className="pt-6 text-center">
|
|
<CheckCircle className="h-8 w-8 text-emerald-300 mx-auto mb-2" />
|
|
<p className="text-3xl font-bold text-emerald-600">{completedKRs.length}</p>
|
|
<p className="text-sm text-muted-foreground">已达标</p>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardContent className="pt-6 text-center">
|
|
<AlertTriangle className="h-8 w-8 text-rose-300 mx-auto mb-2" />
|
|
<p className="text-3xl font-bold text-rose-600">
|
|
{activeKRs.filter(kr => kr.status === 'red').length}
|
|
</p>
|
|
<p className="text-sm text-muted-foreground">需关注</p>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
|
|
<Tabs defaultValue="active" className="space-y-6">
|
|
<TabsList>
|
|
<TabsTrigger value="active">进行中 ({activeKRs.length})</TabsTrigger>
|
|
<TabsTrigger value="past">已结束 ({pastKRs.length})</TabsTrigger>
|
|
<TabsTrigger value="all">全部 ({myKRs.length})</TabsTrigger>
|
|
</TabsList>
|
|
|
|
<TabsContent value="active" className="space-y-4">
|
|
{activeKRs.length === 0 ? (
|
|
<Card>
|
|
<CardContent className="py-12 text-center">
|
|
<Target className="h-12 w-12 text-slate-300 mx-auto mb-4" />
|
|
<p className="text-muted-foreground">暂无进行中的 KR</p>
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
activeKRs.map(kr => <KRCard key={kr.id} kr={kr} />)
|
|
)}
|
|
</TabsContent>
|
|
|
|
<TabsContent value="past" className="space-y-4">
|
|
{pastKRs.length === 0 ? (
|
|
<Card>
|
|
<CardContent className="py-12 text-center">
|
|
<History className="h-12 w-12 text-slate-300 mx-auto mb-4" />
|
|
<p className="text-muted-foreground">暂无已结束的 KR</p>
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
pastKRs.map(kr => <KRCard key={kr.id} kr={kr} showEnded />)
|
|
)}
|
|
</TabsContent>
|
|
|
|
<TabsContent value="all" className="space-y-4">
|
|
{myKRs.length === 0 ? (
|
|
<Card>
|
|
<CardContent className="py-12 text-center">
|
|
<Target className="h-12 w-12 text-slate-300 mx-auto mb-4" />
|
|
<p className="text-muted-foreground">暂无 KR</p>
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
myKRs.map(kr => <KRCard key={kr.id} kr={kr} showEnded={!isKRActive(kr)} />)
|
|
)}
|
|
</TabsContent>
|
|
</Tabs>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function KRCard({ kr, showEnded }: { kr: KRWithRelations; showEnded?: boolean }) {
|
|
const progress = calculateProgress(kr);
|
|
const daysLeft = getDaysRemaining(kr.period_end);
|
|
const isActive = isKRActive(kr);
|
|
|
|
return (
|
|
<Link href={`/krs/${kr.id}`}>
|
|
<Card className={`hover:shadow-md transition-shadow ${!isActive ? 'opacity-70' : ''}`}>
|
|
<CardContent className="py-4">
|
|
<div className="flex items-center gap-4">
|
|
<ProgressRing progress={progress} status={kr.status} size={64} />
|
|
<div className="flex-1 min-w-0">
|
|
<div className="flex items-center gap-2 mb-1">
|
|
<h3 className="font-medium text-slate-800 truncate">{kr.title}</h3>
|
|
{showEnded && (
|
|
<span className="text-xs bg-slate-200 text-slate-600 px-2 py-0.5 rounded">已结束</span>
|
|
)}
|
|
</div>
|
|
<div className="flex items-center gap-3 text-sm text-muted-foreground mb-2">
|
|
<span>{kr.project?.name}</span>
|
|
<span className="text-xs bg-slate-100 px-2 py-0.5 rounded">
|
|
{PERIOD_TYPES[kr.period_type]}
|
|
</span>
|
|
<span>{formatDateRange(kr.period_start, kr.period_end)}</span>
|
|
</div>
|
|
{kr.reason_code && kr.status !== 'green' && (
|
|
<div className="flex items-center gap-1 text-xs text-amber-600">
|
|
<AlertTriangle className="h-3 w-3" />
|
|
{REASON_CODES[kr.reason_code]}
|
|
{kr.note && ` - ${kr.note}`}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div className="text-right">
|
|
<p className="text-lg font-semibold">
|
|
{kr.current_value}/{kr.target_value}
|
|
</p>
|
|
<p className="text-sm text-muted-foreground">{kr.unit}</p>
|
|
{isActive && (
|
|
<p className={`text-xs mt-1 ${daysLeft <= 3 ? 'text-amber-600 font-medium' : 'text-muted-foreground'}`}>
|
|
<Clock className="h-3 w-3 inline mr-1" />
|
|
剩余 {daysLeft} 天
|
|
</p>
|
|
)}
|
|
</div>
|
|
<Button variant="outline" size="sm">
|
|
打卡
|
|
</Button>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</Link>
|
|
);
|
|
}
|