import { useRef, useState } from 'react' import { useLocation } from 'react-router' import { Info } from 'lucide-react' import { PageError, PageHeader, PageLoading } from '@/components/page' import { PeriodPicker } from '@/features/periods/period-picker' import { SelectedPeriod } from '@/features/periods/selected-period' import { Section } from '@/features/planning/plan-read' import { useCategories, usePaymentMethods } from '@/lib/catalog' import { useEntries } from '@/lib/entries' import { toIsoDate } from '@/lib/format' import { weekIndexFor } from '@/lib/ledger' import { isWritable } from '@/lib/period-select' import { useBudget } from '@/lib/periods' import { t } from '@/lib/strings' import type { Entry } from '@/types/Entry' import type { Period } from '@/types/Period' import { EntryForm } from './entry-form' import { EntryList } from './entry-list' import { WeekPicker } from './week-picker' import { WeekSummary } from './week-summary' export function LedgerPage() { return ( <> } /> {(p) => } ) } /** * Defter her açılışta **bugünün haftasıyla** başlar (dönem geçmişteyse son, gelecekteyse ilk * hafta); oklarla gezilen hafta yalnızca bu ekranda tutulur, adrese yazılmaz (kullanıcı kararı). * Düzen: hafta seçici → form → haftalık özet → girişler (masaüstünde form solda, diğerleri sağda). */ function LedgerView({ period }: { period: Period }) { const [today] = useState(() => toIsoDate(new Date())) const [editing, setEditing] = useState(null) // Panel'deki "Harcama Ekle" → tutar alanına odaklan const focusAmount = (useLocation().state as { focus?: boolean } | null)?.focus === true const formRef = useRef(null) const inPeriod = today >= period.start_date && today <= period.last_date const todayIndex = inPeriod ? weekIndexFor(period.weeks, today) : null const [index, setIndex] = useState(() => weekIndexFor(period.weeks, today)) // Dönem tarihleri değişip hafta sayısı azalırsa son haftaya düş const week = period.weeks[Math.min(index, period.weeks.length) - 1] const readOnly = !isWritable(period) const budget = useBudget(period.id) const categories = useCategories(true) const methods = usePaymentMethods(true) const entries = useEntries(period.id, { from: week.start_date, to: week.last_date }) const goTo = (n: number) => { setEditing(null) setIndex(n) } function edit(e: Entry) { setEditing(e) formRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' }) } const catalogFailed = [categories, methods].find((q) => q.isError) const catalog = categories.data && methods.data ? { c: categories.data, m: methods.data } : null return (
{readOnly && (

{t.ledger.readOnly}

)}
{!readOnly && (
{catalogFailed?.error ? ( ) : !catalog ? ( ) : ( goTo(weekIndexFor(period.weeks, date))} onCancel={() => setEditing(null)} /> )}
)}
{budget.isError ? ( ) : budget.data ? ( ) : ( )}
{entries.isError ? ( ) : entries.data && catalog ? ( editing?.id === id && setEditing(null)} /> ) : ( )}
) }