"use client"; import { getDocumentIntlLocale } from "@/lib/i18n/browser"; import { useTranslations } from "@/components/i18n/i18n-provider"; import { createCalendarEventRecord, deleteCalendarEventRecord, updateCalendarEventRecord, } from "@/app/(dashboard)/calendar/actions"; import { Badge, Button, Card, CardContent, Input, Label, Textarea } from "poyraz-ui/atoms"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Tabs, TabsContent, TabsList, TabsTrigger, toast, } from "poyraz-ui/molecules"; import { Clock, Pencil, Plus, Trash2 } from "lucide-react"; import { useMemo, useState } from "react"; export type CalendarRelationOption = { id: string; name: string; }; export type CalendarTaskOption = { id: string; title: string; }; export type CalendarEventItem = { id: string; title: string; description: string | null; type: "meeting" | "focus" | "deadline" | "personal" | "finance"; starts_at: string; ends_at: string | null; client_id: string | null; project_id: string | null; task_id: string | null; clientName: string | null; projectName: string | null; taskTitle: string | null; translations?: Record>; }; const typeLabels = { meeting: "Toplantı", focus: "Odak", deadline: "Deadline", personal: "Kişisel", finance: "Finans", }; const typeClasses = { meeting: "border-blue-200 bg-blue-50 text-blue-700", focus: "border-emerald-200 bg-emerald-50 text-emerald-700", deadline: "border-rose-200 bg-rose-50 text-rose-700", personal: "border-amber-200 bg-amber-50 text-amber-700", finance: "border-primary/20 bg-primary/10 text-primary", }; type CalendarClientProps = { events: CalendarEventItem[]; clients: CalendarRelationOption[]; projects: CalendarRelationOption[]; tasks: CalendarTaskOption[]; activeLocales: { code: string; name: string }[]; }; export function CalendarClient({ events, clients, projects, tasks, activeLocales }: CalendarClientProps) { const t = useTranslations(); const [monthDate, setMonthDate] = useState(() => new Date()); const [selectedDate, setSelectedDate] = useState(() => toDateKey(new Date())); const days = useMemo(() => buildMonthDays(monthDate), [monthDate]); const eventsByDay = useMemo(() => groupEventsByDay(events), [events]); const selectedEvents = eventsByDay.get(selectedDate) || []; const upcomingEvents = events .filter((event) => new Date(event.starts_at) >= startOfToday()) .slice(0, 6); function shiftMonth(amount: number) { setMonthDate((current) => new Date(current.getFullYear(), current.getMonth() + amount, 1)); } return (

{t("calendar.title")}

{formatMonth(monthDate)}

{t("common.itemsCount", { count: events.length })}

{["Pzt", "Sal", "Çar", "Per", "Cum", "Cmt", "Paz"].map((day) => (
{day}
))}
{days.map((day) => { const dayEvents = eventsByDay.get(day.key) || []; const isSelected = selectedDate === day.key; return ( ); })}

{formatDateLabel(selectedDate)}

{t("common.itemsCount", { count: selectedEvents.length })}

{t("calendar.upcoming")}

); } function EventList({ events, clients, projects, tasks, activeLocales, compact = false, }: { events: CalendarEventItem[]; clients: CalendarRelationOption[]; projects: CalendarRelationOption[]; tasks: CalendarTaskOption[]; activeLocales: { code: string; name: string }[]; compact?: boolean; }) { const t = useTranslations(); if (events.length === 0) { return

{t("calendar.noEvents")}

; } return (
{events.map((event) => (
{event.title}
{formatTimeRange(event)}
{!compact ? (
{event.projectName || event.clientName || event.taskTitle || event.description || "Bağlantı yok"}
) : null}
{t(`calendar.types.${event.type}` as any)}
{!compact ? (
) : null}
))}
); } function CalendarEventDialog({ mode, event, defaultDate, clients, projects, tasks, activeLocales, }: { mode: "create" | "edit"; event?: CalendarEventItem; defaultDate?: string; clients: CalendarRelationOption[]; projects: CalendarRelationOption[]; tasks: CalendarTaskOption[]; activeLocales: { code: string; name: string }[]; }) { const t = useTranslations(); const [open, setOpen] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const action = mode === "create" ? createCalendarEventRecord : updateCalendarEventRecord; async function handleSubmit(formData: FormData) { setIsSubmitting(true); try { await action(formData); setOpen(false); toast.success(mode === "create" ? t("calendar.messages.created") : t("calendar.messages.updated")); } catch (error) { toast.error( error instanceof Error ? error.message : t("calendar.errors.saveFailed"), ); } finally { setIsSubmitting(false); } } return (
{event ? : null} {mode === "create" ? t("calendar.form.createTitle") : t("calendar.form.editTitle")} {t("calendar.description")}
); } function EventFormFields({ event, defaultDate, clients, projects, tasks, activeLocales, }: { event?: CalendarEventItem; defaultDate?: string; clients: CalendarRelationOption[]; projects: CalendarRelationOption[]; tasks: CalendarTaskOption[]; activeLocales: { code: string; name: string }[]; }) { const t = useTranslations(); const startsAt = event?.starts_at ? toDateTimeLocal(event.starts_at) : `${defaultDate || toDateKey(new Date())}T09:00`; return (
{activeLocales.length > 1 ? ( {activeLocales.map((locale) => ( {locale.name} ))} {activeLocales.map((locale) => (