FlowWeb / src /features /templates /SaveTemplateModal.jsx
danylokhodus's picture
Claude Opus 5.5
feat: new landing, template system, auto-layout, Free/Plus plans; remove AI and hide habits
0dcbaee
Raw History Blame Contribute Delete
7.78 kB
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;