// Ortak arayüz parçaları: pencere (modal), onay/soru diyalogları, bildirimler, tema.
import { X } from 'lucide-react';
import { type ReactNode, createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
export function Modal({ title, onClose, children, wide }: { title: string; onClose: () => void; children: ReactNode; wide?: boolean }) {
useEffect(() => {
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose();
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [onClose]);
return (
e.target === e.currentTarget && onClose()}>
);
}
interface PromptOptions {
title: string;
label?: string;
initial?: string;
placeholder?: string;
confirmText?: string;
}
interface ConfirmOptions {
title: string;
message: string;
confirmText?: string;
danger?: boolean;
}
type Toast = { id: number; text: string; kind: 'error' | 'info' };
interface UiContext {
prompt: (o: PromptOptions) => Promise;
confirm: (o: ConfirmOptions) => Promise;
toast: (text: string, kind?: 'error' | 'info') => void;
/** Bir hatayı kullanıcıya bildirim olarak gösterir. */
fail: (err: unknown) => void;
}
const Ctx = createContext(null!);
export const useUi = () => useContext(Ctx);
type Dialog =
| { kind: 'prompt'; options: PromptOptions; resolve: (v: string | null) => void }
| { kind: 'confirm'; options: ConfirmOptions; resolve: (v: boolean) => void };
export function UiProvider({ children }: { children: ReactNode }) {
const [dialog, setDialog] = useState