import { useState } from 'react';
import { useNavigate } from 'react-router';
import { trpc } from '../lib/trpc';
import { voice } from '../lib/voice';
import { Card } from '../components/ui/Card';
import { Button } from '../components/ui/Button';
import { SkeletonCard } from '../components/ui/SkeletonCard';
import { toast } from 'sonner';
import { clsx } from 'clsx';
import { Check, X, Minus, TrendingUp, TrendingDown, ArrowRight } from 'lucide-react';
type Ampel = 'green' | 'amber' | 'red';
type Trend = 'besser' | 'gleich' | 'schlechter';
type TodoStatus = 'open' | 'done' | 'dropped';
export function ReflexionRoute() {
const today = trpc.dayEntry.today.useQuery();
const navigate = useNavigate();
if (today.isPending) return ;
if (!today.data?.entry) {
return (
Du bist heute noch nicht locked in. {voice.hint.notCheckedInToday}
);
}
return (
navigate('/puls')}
/>
);
}
function ReflectForm({
entry,
todos,
onDone,
}: {
entry: { id: string };
todos: Array<{
id: string;
title: string;
status: TodoStatus;
bezugKind: 'project' | 'deal' | 'campaign' | 'allgemein' | null;
}>;
onDone: () => void;
}) {
const [todoStates, setTodoStates] = useState<
Record
>(Object.fromEntries(todos.map((t) => [t.id, { status: t.status }])));
const [reflectionText, setReflectionText] = useState('');
const [ampel, setAmpel] = useState(null);
const [trend, setTrend] = useState(null);
const [blockerEnabled, setBlockerEnabled] = useState(false);
const [blockerTitle, setBlockerTitle] = useState('');
const [blockerDescription, setBlockerDescription] = useState('');
const hasProjectBezug = todos.some((t) => t.bezugKind === 'project');
const utils = trpc.useUtils();
const mutation = trpc.dayEntry.submitReflection.useMutation({
onSuccess: async () => {
toast(voice.toast.reflected);
await utils.dayEntry.today.invalidate();
onDone();
},
});
function setTodoStatus(id: string, status: TodoStatus) {
setTodoStates((prev) => ({ ...prev, [id]: { ...prev[id], status } }));
}
function handleSubmit(e: React.FormEvent) {
e.preventDefault();
if (!ampel) {
toast('Bitte Ampel setzen.');
return;
}
if (hasProjectBezug && !trend) {
toast('Bei Projektbezug ist der Trend Pflicht.');
return;
}
if (blockerEnabled && !blockerTitle.trim()) {
toast('Blocker braucht einen Titel.');
return;
}
mutation.mutate({
dayEntryId: entry.id,
todos: Object.entries(todoStates).map(([id, s]) => ({
id,
status: s.status,
dropReason: s.reason,
})),
reflectionText,
ampel,
trend: trend ?? null,
...(blockerEnabled
? { blocker: { title: blockerTitle, description: blockerDescription || undefined } }
: {}),
});
}
return (
);
}
function StatusBtn({
active,
onClick,
children,
label,
}: {
active: boolean;
onClick: () => void;
children: React.ReactNode;
label: string;
}) {
return (
);
}
function AmpelBtn({
value,
current,
onChange,
label,
}: {
value: Ampel;
current: Ampel | null;
onChange: (a: Ampel) => void;
label: string;
}) {
const bgMap: Record = {
green: 'bg-[var(--color-ampel-green-bg)] text-[var(--color-ampel-green-fg)]',
amber: 'bg-[var(--color-ampel-amber-bg)] text-[var(--color-ampel-amber-fg)]',
red: 'bg-[var(--color-ampel-red-bg)] text-[var(--color-ampel-red-fg)]',
};
const dotMap: Record = {
green: 'bg-[var(--color-ampel-green-fg)]',
amber: 'bg-[var(--color-ampel-amber-fg)]',
red: 'bg-[var(--color-ampel-red-fg)]',
};
const active = current === value;
return (
);
}
function TrendBtn({
value,
current,
onChange,
children,
}: {
value: Trend;
current: Trend | null;
onChange: (t: Trend) => void;
children: React.ReactNode;
}) {
const active = current === value;
return (
);
}