Thoth / web /src /ui.tsx
mfirat007's picture
Upload 75 files
a86cedf verified
Raw History Blame Contribute Delete
7.57 kB
// 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 (
<div className="modal-backdrop" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
<div className={`modal${wide ? ' modal-wide' : ''}`} role="dialog" aria-modal="true" aria-label={title}>
<header className="modal-header">
<h2>{title}</h2>
<button className="icon-btn" onClick={onClose} aria-label="Kapat"><X size={18} /></button>
</header>
<div className="modal-body">{children}</div>
</div>
</div>
);
}
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<string | null>;
confirm: (o: ConfirmOptions) => Promise<boolean>;
toast: (text: string, kind?: 'error' | 'info') => void;
/** Bir hatayı kullanıcıya bildirim olarak gösterir. */
fail: (err: unknown) => void;
}
const Ctx = createContext<UiContext>(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<Dialog | null>(null);
const [toasts, setToasts] = useState<Toast[]>([]);
const [value, setValue] = useState('');
const nextId = useRef(1);
const prompt = useCallback((options: PromptOptions) => {
setValue(options.initial ?? '');
return new Promise<string | null>((resolve) => setDialog({ kind: 'prompt', options, resolve }));
}, []);
const confirm = useCallback(
(options: ConfirmOptions) => new Promise<boolean>((resolve) => setDialog({ kind: 'confirm', options, resolve })),
[],
);
const toast = useCallback((text: string, kind: 'error' | 'info' = 'info') => {
const id = nextId.current++;
setToasts((t) => [...t, { id, text, kind }]);
setTimeout(() => setToasts((t) => t.filter((x) => x.id !== id)), kind === 'error' ? 7000 : 3500);
}, []);
const fail = useCallback((err: unknown) => toast(err instanceof Error ? err.message : String(err), 'error'), [toast]);
const close = (result: string | boolean | null) => {
if (!dialog) return;
(dialog.resolve as (v: unknown) => void)(result);
setDialog(null);
};
return (
<Ctx.Provider value={{ prompt, confirm, toast, fail }}>
{children}
{dialog?.kind === 'prompt' && (
<Modal title={dialog.options.title} onClose={() => close(null)}>
<form onSubmit={(e) => { e.preventDefault(); if (value.trim()) close(value.trim()); }}>
{dialog.options.label && <label className="field-label">{dialog.options.label}</label>}
<input className="input" autoFocus value={value} placeholder={dialog.options.placeholder} onChange={(e) => setValue(e.target.value)} />
<div className="modal-actions">
<button type="button" className="btn" onClick={() => close(null)}>Vazgeç</button>
<button type="submit" className="btn btn-primary" disabled={!value.trim()}>{dialog.options.confirmText ?? 'Tamam'}</button>
</div>
</form>
</Modal>
)}
{dialog?.kind === 'confirm' && (
<Modal title={dialog.options.title} onClose={() => close(false)}>
<p className="modal-text">{dialog.options.message}</p>
<div className="modal-actions">
<button className="btn" onClick={() => close(false)}>Vazgeç</button>
<button autoFocus className={`btn ${dialog.options.danger ? 'btn-danger' : 'btn-primary'}`} onClick={() => close(true)}>
{dialog.options.confirmText ?? 'Tamam'}
</button>
</div>
</Modal>
)}
<div className="toasts" aria-live="polite">
{toasts.map((t) => <div key={t.id} className={`toast toast-${t.kind}`}>{t.text}</div>)}
</div>
</Ctx.Provider>
);
}
// ---- Tema ----
export type Theme = 'light' | 'dark';
function readTheme(): Theme {
try {
const saved = localStorage.getItem('thoth:tema');
if (saved === 'light' || saved === 'dark') return saved;
} catch {
// depolama kapalı olabilir
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
export function useTheme(): [Theme, () => void] {
const [theme, setTheme] = useState<Theme>(readTheme);
useEffect(() => {
document.documentElement.dataset.theme = theme;
}, [theme]);
const toggle = () =>
setTheme((t) => {
const next = t === 'dark' ? 'light' : 'dark';
try {
localStorage.setItem('thoth:tema', next);
} catch {
// depolama kapalı olabilir
}
return next;
});
return [theme, toggle];
}
/** localStorage'da saklanan basit bir tercih. */
export function useStored<T>(key: string, initial: T): [T, (v: T) => void] {
const [value, setValue] = useState<T>(() => {
try {
const raw = localStorage.getItem(key);
return raw == null ? initial : (JSON.parse(raw) as T);
} catch {
return initial;
}
});
const set = (v: T) => {
setValue(v);
try {
localStorage.setItem(key, JSON.stringify(v));
} catch {
// depolama kapalı olabilir
}
};
return [value, set];
}
/**
* Thoth işareti: MitrÆnlil uygulama ikonu ailesindedir (yuvarlak köşeli zemin, kalın harf, sağ üstte "AI" rozeti).
* Kırmızı zemin + beyaz harf; rozet küçük boyutlarda okunmadığı için 30 pikselin altında gösterilmez.
*/
export function Logo({ size = 28 }: { size?: number }) {
const badge = size >= 30;
return (
<svg width={size} height={size} viewBox="0 0 64 64" aria-hidden="true">
<rect width="64" height="64" rx="14" fill="var(--brand-red)" />
<path d={badge ? 'M8 34 12.8 24.5H46V34H35.5V55H25.5V34Z' : 'M9 27.5 13.8 18H53V27.5H37V50H27V27.5Z'} fill="#fff" />
{badge && (
<g>
<circle cx="50" cy="14" r="8.6" fill="var(--brand-red)" stroke="#fff" strokeWidth="2.4" />
<text x="50" y="17.7" textAnchor="middle" fontSize="10" fontWeight="800" fill="#fff" fontFamily="Inter, Arial, sans-serif" letterSpacing="-0.3">AI</text>
</g>
)}
</svg>
);
}
/** MitrÆnlil iletişim adresi. */
export function Contact({ className = 'contact' }: { className?: string }) {
return <span className={className}>İletişim: <a href="mailto:bilgi@mitranlil.tr">bilgi@mitranlil.tr</a></span>;
}
/** Kurumsal imza: "bir MitrÆnlil ürünü" (Æ kırmızı). */
export function BrandBy({ className = 'brand-by' }: { className?: string }) {
return <span className={className}>bir <strong className="mitraenlil">Mitr<span className="ae">Æ</span>nlil</strong> ürünü</span>;
}