Files
butcem/src/components/layout/quick-add.tsx
T

52 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { Plus } from "lucide-react";
import { createContext, use, useState } from "react";
import { Button } from "@/components/ui/button";
import {
TransactionFormDialog,
type TransactionFormMode,
} from "@/features/transactions/components/transaction-form";
import { tr } from "@/i18n/tr";
type QuickAddApi = {
/** Hızlı ekle: yeni işlem formu (varsayılan gider; sekmelerle gelir/transfer). */
openQuickAdd: (type?: "expense" | "income" | "transfer") => void;
/** Düzenle / kopyala (işlem listesi ve ayrıntısı). */
openTransactionForm: (mode: TransactionFormMode) => void;
};
const QuickAddContext = createContext<QuickAddApi | null>(null);
export function useQuickAdd() {
const ctx = use(QuickAddContext);
if (!ctx) throw new Error("useQuickAdd, QuickAddProvider içinde kullanılmalı");
return ctx;
}
/** Uygulama genelinde tek işlem formu; her ekrandan açılabilir (Bölüm 4). */
export function QuickAddProvider({ children }: { children: React.ReactNode }) {
const [mode, setMode] = useState<TransactionFormMode | null>(null);
const api: QuickAddApi = {
openQuickAdd: (type = "expense") => setMode({ kind: "create", type }),
openTransactionForm: setMode,
};
return (
<QuickAddContext value={api}>
{children}
<TransactionFormDialog mode={mode} onOpenChange={(open) => !open && setMode(null)} />
</QuickAddContext>
);
}
/** Masaüstü üst bar düğmesi. */
export function QuickAddButton({ className }: { className?: string }) {
const { openQuickAdd } = useQuickAdd();
return (
<Button onClick={() => openQuickAdd()} className={className}>
<Plus />
{tr.quickAdd.button}
</Button>
);
}