Thoth / web /src /components /GiydirDialog.tsx
mfirat007's picture
Upload 74 files
4a51f94 verified
Raw History Blame Contribute Delete
4.52 kB
import { type FormEvent, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { api } from '../api';
import { Modal, useUi } from '../ui';
interface LatexResult {
id: string;
kapsama: number;
uyarilar: string[];
}
/** Çalışmayı (bu proje ya da bir Word dosyası) yüklenen dergi/kurum şablonuna giydirir. */
export function GiydirDialog({ projectId, projectTitle, flush, onClose }: {
projectId: string;
projectTitle: string;
flush: () => Promise<boolean>;
onClose: () => void;
}) {
const ui = useUi();
const navigate = useNavigate();
const [hedef, setHedef] = useState<File | null>(null);
const [kaynakTuru, setKaynakTuru] = useState<'proje' | 'word'>('proje');
const [kaynak, setKaynak] = useState<File | null>(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
const isWord = !!hedef && /\.(docx|dotx)$/i.test(hedef.name);
async function submit(e: FormEvent) {
e.preventDefault();
if (!hedef) return;
setError('');
setBusy(true);
try {
if (kaynakTuru === 'proje' && !(await flush())) return;
const form = new FormData();
form.append('hedef', hedef);
if (kaynakTuru === 'word' && kaynak) form.append('kaynak', kaynak);
if (isWord) {
const res = await fetch(`/api/projects/${projectId}/giydir`, { method: 'POST', headers: { 'X-Thoth': '1' }, body: form });
if (!res.ok) throw new Error((await res.json().catch(() => null))?.error ?? 'Şablona giydirilemedi.');
const ad = kaynakTuru === 'word' && kaynak ? kaynak.name.replace(/\.docx$/i, '') : projectTitle;
const url = URL.createObjectURL(await res.blob());
const link = document.createElement('a');
link.href = url;
link.download = `${ad} (${hedef.name.replace(/\.(docx|dotx)$/i, '')}).docx`;
link.click();
setTimeout(() => URL.revokeObjectURL(url), 10_000);
ui.toast('Şablona giydirilmiş Word dosyası indirildi.');
onClose();
} else {
const res = await api<LatexResult>(`/projects/${projectId}/giydir`, { body: form });
onClose();
navigate(`/proje/${res.id}`);
ui.toast('Çalışma şablona giydirildi ve yeni bir proje olarak açıldı. Derleyip kontrol edin.');
for (const u of res.uyarilar) ui.toast(u, 'error');
}
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
} finally {
setBusy(false);
}
}
return (
<Modal title="Şablona giydir" onClose={busy ? () => {} : onClose}>
<form onSubmit={submit}>
<p className="modal-text">
Dergi ya da kurumun şablonunu yükleyin; çalışmanız içeriği değiştirilmeden şablonun düzenine aktarılır.
</p>
<label className="field-label" htmlFor="giydir-hedef">Hedef şablon</label>
<input id="giydir-hedef" className="input" type="file" accept=".docx,.dotx,.zip,.tex" required onChange={(e) => setHedef(e.target.files?.[0] ?? null)} />
<p className="hint">
Word şablonu (.docx, .dotx): biçemler, kenar boşlukları ve üst bilgiler şablondan alınır, sonuç Word olarak iner.
LaTeX şablonu (.zip ya da .tex): yapay zekâ çalışmanızı şablonun ana dosyasına yerleştirir, sonuç yeni bir proje olarak açılır.
</p>
<span className="field-label">Giydirilecek çalışma</span>
<label className="check">
<input type="radio" name="giydir-kaynak" checked={kaynakTuru === 'proje'} onChange={() => setKaynakTuru('proje')} /> Bu proje ({projectTitle})
</label>
<br />
<label className="check">
<input type="radio" name="giydir-kaynak" checked={kaynakTuru === 'word'} onChange={() => setKaynakTuru('word')} /> Word dosyası
</label>
{kaynakTuru === 'word' && (
<input className="input" type="file" accept=".docx" required style={{ marginTop: 8 }} onChange={(e) => setKaynak(e.target.files?.[0] ?? null)} />
)}
{error && <p className="form-error">{error}</p>}
<div className="modal-actions">
{busy && <span className="muted">{isWord ? 'Giydiriliyor…' : 'Giydiriliyor… bu işlem birkaç dakika sürebilir.'}</span>}
<button type="button" className="btn" onClick={onClose} disabled={busy}>Vazgeç</button>
<button className="btn btn-primary" disabled={busy || !hedef}>Giydir</button>
</div>
</form>
</Modal>
);
}