Spaces:
Running
Running
File size: 7,777 Bytes
0dcbaee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import React, { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { X, BookOpen, Check, Loader2 } from 'lucide-react';
import TemplatePreview from './TemplatePreview';
import { createTemplate } from './templatesApi';
/**
* Saves the board — or only the selected nodes — as a personal template.
* `serialize(selectedOnly)` returns the template content from the canvas.
*/
const SaveTemplateModal = ({ open, onClose, serialize, selectedCount, initialTitle, initialDesc }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const [title, setTitle] = useState(initialTitle || '');
const [desc, setDesc] = useState(initialDesc || '');
const [category, setCategory] = useState('');
const [selectedOnly, setSelectedOnly] = useState(selectedCount >= 2);
const [saving, setSaving] = useState(false);
const [error, setError] = useState('');
const [limitHit, setLimitHit] = useState(false);
const [done, setDone] = useState(false);
const content = open ? serialize(selectedOnly) : null;
const handleSubmit = async (e) => {
e.preventDefault();
if (!title.trim() || !content?.nodes.length) return;
setSaving(true);
setError('');
setLimitHit(false);
try {
await createTemplate({
title: title.trim(),
description: desc.trim(),
category: category.trim() || t('templates.my_category', 'My templates'),
icon: 'BookOpen',
content,
});
setDone(true);
setTimeout(onClose, 1100);
} catch (err) {
const msg = err.response?.data?.message || t('templates.save_failed', 'Could not save the template.');
setError(msg);
setLimitHit(/plan|limit/i.test(msg));
} finally {
setSaving(false);
}
};
return (
<AnimatePresence>
{open && (
<div className="fixed inset-0 z-[70] flex items-center justify-center p-4">
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} onClick={onClose} className="absolute inset-0 bg-primary/25 backdrop-blur-sm" />
<motion.div
initial={{ opacity: 0, y: 24, rotate: -1 }}
animate={{ opacity: 1, y: 0, rotate: 0 }}
exit={{ opacity: 0, y: 24 }}
transition={{ type: 'spring', stiffness: 260, damping: 24 }}
className="relative z-10 bg-surface-container-lowest w-full max-w-lg p-8 rough-border rough-shadow flex flex-col gap-5 max-h-[92vh] overflow-y-auto"
>
<button type="button" onClick={onClose} className="absolute top-4 right-4 text-on-surface-variant hover:text-primary cursor-pointer">
<X size={24} />
</button>
<h3 className="text-3xl font-display-lg text-primary flex items-center gap-2">
<BookOpen size={24} />
<span>{t('templates.save_title', 'Save as template')}</span>
</h3>
{done ? (
<motion.div initial={{ scale: 0.6, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} className="py-8 flex flex-col items-center gap-3 text-primary">
<div className="w-16 h-16 rounded-full bg-secondary flex items-center justify-center border-4 border-primary"><Check size={32} /></div>
<span className="font-bold text-lg">{t('templates.saved', 'Template saved')}</span>
</motion.div>
) : (
<form onSubmit={handleSubmit} className="flex flex-col gap-5">
<div className="rounded-xl border-2 border-primary/10 graph-paper-bg px-3 pt-3">
<TemplatePreview content={content} height={150} />
</div>
{selectedCount >= 2 && (
<div className="flex bg-secondary/5 border-2 border-primary rounded-xl p-1">
{[false, true].map((only) => (
<button
key={String(only)}
type="button"
onClick={() => setSelectedOnly(only)}
className={`flex-1 py-2 rounded-lg text-xs font-bold uppercase tracking-wider cursor-pointer transition-colors ${selectedOnly === only ? 'bg-primary text-on-primary' : 'text-primary'}`}
>
{only
? t('templates.only_selected', { count: selectedCount, defaultValue: `Selected (${selectedCount})` })
: t('templates.whole_board', 'Whole board')}
</button>
))}
</div>
)}
<label className="flex flex-col gap-1">
<span className="text-xs font-bold text-primary uppercase tracking-wider">{t('templates.field_title', 'Title')}</span>
<input value={title} onChange={(e) => setTitle(e.target.value)} maxLength={80} required className="bg-surface-container-low border-b-2 border-primary/20 focus:border-primary px-2 py-2 text-lg outline-none" />
</label>
<label className="flex flex-col gap-1">
<span className="text-xs font-bold text-primary uppercase tracking-wider">{t('templates.field_desc', 'Description')}</span>
<textarea rows={2} value={desc} onChange={(e) => setDesc(e.target.value)} maxLength={300} className="bg-surface-container-low border-b-2 border-primary/20 focus:border-primary px-2 py-2 outline-none resize-none" />
</label>
<label className="flex flex-col gap-1">
<span className="text-xs font-bold text-primary uppercase tracking-wider">{t('templates.field_category', 'Category')}</span>
<input value={category} onChange={(e) => setCategory(e.target.value)} placeholder={t('templates.my_category', 'My templates')} maxLength={40} className="bg-surface-container-low border-b-2 border-primary/20 focus:border-primary px-2 py-2 outline-none" />
</label>
<p className="text-[11px] text-on-surface-variant">
{t('templates.save_hint', 'Statuses, progress and assignees are not saved — the template is a clean starting point.')}
</p>
{error && (
<div className="bg-error/10 text-error p-3 rough-border text-sm font-bold flex flex-col gap-2">
<span>{error}</span>
{limitHit && (
<button type="button" onClick={() => navigate('/subscription')} className="self-start underline cursor-pointer">
{t('templates.go_plus', 'Plus removes the limit')}
</button>
)}
</div>
)}
<div className="flex gap-3">
<button type="button" onClick={onClose} className="flex-1 py-3 text-sm font-bold border-2 border-primary/10 rounded-xl hover:bg-primary/5 cursor-pointer">
{t('common.cancel', 'Cancel')}
</button>
<button type="submit" disabled={saving || !title.trim() || !content?.nodes.length} className="flex-1 py-3 text-sm font-bold text-on-primary bg-primary border-2 border-primary rounded-xl cursor-pointer disabled:opacity-50 flex items-center justify-center gap-2">
{saving && <Loader2 size={16} className="animate-spin" />}
{t('templates.save_btn', 'Save template')}
</button>
</div>
</form>
)}
</motion.div>
</div>
)}
</AnimatePresence>
);
};
export default SaveTemplateModal;
|