Files
ok_r/app/my-krs/page.tsx
karlcarlo fc9cfda29e feat: 添加 API 客户端以处理 basePath 并更新所有 fetch 调用
移除 next.config.js 中的 standalone 输出配置,添加 lib/api-client.ts 封装 fetch 请求,
自动为所有 API 调用添加配置的 basePath。更新所有页面和组件中的 fetch 调用以使用新的 fetchApi 函数,
确保在部署到子路径时 API 请求能正确工作。同时调整 tsconfig.json 的格式并修复项目创建时的字段名。
2026-01-28 15:54:19 +08:00

261 lines
9.8 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';
import { fetchApi } from '@/lib/api-client';
interface KRWithRelations extends KR {
owner: User | null;
project: { id: string; name: string; status: string } | null;
}
export default function MyKRsPage() {
const { currentUser, users } = useUser();
const [krs, setKRs] = useState<KRWithRelations[]>([]);
const [isLoading, setIsLoading] = useState(true);
// Group KRs by owner for owner view
const krsByOwner = users.map(user => ({
user,
krs: krs.filter(kr => kr.owner_user_id === user.id && isKRActive(kr))
})).filter(group => group.krs.length > 0 || group.user.role !== 'owner') // Filter out users with no active KRs unless it's a member/pm we want to track
.sort((a, b) => {
// Sort by KR count descending (users with KRs first)
if (a.krs.length > 0 && b.krs.length === 0) return -1;
if (a.krs.length === 0 && b.krs.length > 0) return 1;
return 0;
});
useEffect(() => {
async function fetchKRs() {
try {
const res = await fetchApi('/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));
const isOwner = currentUser?.role === 'owner';
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">
{isOwner ? '全员 KR 概览' : '我的 KR'}
</h1>
<p className="text-muted-foreground mt-1">
{isOwner
? '查看所有成员的当前周期目标执行情况'
: `${currentUser?.name} 负责的执行目标`
}
</p>
</div>
{isOwner ? (
<div className="space-y-8">
{krsByOwner.map(({ user, krs }) => (
<div key={user.id} className="space-y-4">
<div className="flex items-center gap-3 border-b pb-2">
<div className="h-8 w-8 rounded-full bg-slate-200 flex items-center justify-center text-sm font-medium text-slate-600">
{user.name[0]}
</div>
<div>
<h2 className="text-lg font-semibold text-slate-800">{user.name}</h2>
<p className="text-xs text-muted-foreground">
{krs.length} 个进行中的 KR
</p>
</div>
</div>
{krs.length === 0 ? (
<div className="p-8 text-center bg-slate-50 rounded-lg border border-dashed">
<p className="text-muted-foreground text-sm">暂无进行中的 KR</p>
</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{krs.map(kr => <KRCard key={kr.id} kr={kr} />)}
</div>
)}
</div>
))}
{krsByOwner.length === 0 && (
<div className="text-center py-12">
<p className="text-muted-foreground">没有任何成员有正在进行的 KR</p>
</div>
)}
</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>
);
}