Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Refactor app creation modal to use template selection

Juan Denis3020f23d5ca6
diff --git a/frontend/src/pages/Applications.jsx b/frontend/src/pages/Applications.jsx
index 6c61ef83..6ebbb9fd 100644
--- a/frontend/src/pages/Applications.jsx
+++ b/frontend/src/pages/Applications.jsx
@@ -449,7 +449,7 @@ const Applications = () => {
)}
{showCreateModal && (
- <CreateAppModal onClose={() => setShowCreateModal(false)} onCreated={loadApps} />
+ <CreateAppModal onClose={() => setShowCreateModal(false)} />
)}
{confirmModal && (
@@ -722,331 +722,60 @@ const ConfirmModal = ({ title, message, confirmText, danger, onConfirm, onCancel
);
};
-const CreateAppModal = ({ onClose, onCreated }) => {
- const [step, setStep] = useState(1);
- const [appType, setAppType] = useState('');
- const [formData, setFormData] = useState({
- name: '',
- app_type: '',
- php_version: '8.2',
- python_version: '3.11',
- port: 8000,
- root_path: '/var/www/',
- // WordPress specific
- site_url: '',
- site_title: '',
- admin_user: 'admin',
- admin_email: '',
- db_name: '',
- db_user: '',
- db_password: '',
- // Docker specific
- docker_image: '',
- docker_ports: '',
- docker_volumes: '',
- docker_env: '',
- });
- const [loading, setLoading] = useState(false);
- const [error, setError] = useState('');
+const CreateAppModal = ({ onClose }) => {
+ const navigate = useNavigate();
- const appTypes = [
- { id: 'wordpress', name: 'WordPress', icon: 'W', color: '#21759b', description: 'Full WordPress installation with WP-CLI' },
- { id: 'php', name: 'PHP', icon: 'P', color: '#777bb4', description: 'PHP application with FPM' },
- { id: 'flask', name: 'Flask', icon: 'F', color: '#fcd34d', description: 'Python Flask application' },
- { id: 'django', name: 'Django', icon: 'D', color: '#34d399', description: 'Python Django application' },
- { id: 'docker', name: 'Docker', icon: 'D', color: '#2496ed', description: 'Docker container application' },
- { id: 'static', name: 'Static', icon: 'S', color: '#60a5fa', description: 'Static HTML/CSS/JS site' },
+ // Popular Docker templates to show
+ const templates = [
+ { id: 'wordpress', name: 'WordPress', icon: 'W', color: '#21759b', description: 'Full WordPress installation with database' },
+ { id: 'nextcloud', name: 'Nextcloud', icon: 'N', color: '#0082c9', description: 'Self-hosted cloud storage platform' },
+ { id: 'grafana', name: 'Grafana', icon: 'G', color: '#f46800', description: 'Monitoring and observability dashboards' },
+ { id: 'portainer', name: 'Portainer', icon: 'P', color: '#13bef9', description: 'Docker container management UI' },
+ { id: 'uptime-kuma', name: 'Uptime Kuma', icon: 'U', color: '#5cdd8b', description: 'Self-hosted monitoring tool' },
+ { id: 'gitea', name: 'Gitea', icon: 'G', color: '#609926', description: 'Lightweight Git hosting service' },
];
- function handleChange(e) {
- setFormData({ ...formData, [e.target.name]: e.target.value });
+ function selectTemplate(templateId) {
+ onClose();
+ navigate(`/templates?install=${templateId}`);
}
- function selectAppType(type) {
- setAppType(type);
- setFormData({ ...formData, app_type: type, root_path: `/var/www/${formData.name || 'mysite'}` });
- setStep(2);
- }
-
- async function handleSubmit(e) {
- e.preventDefault();
- setError('');
- setLoading(true);
-
- try {
- if (appType === 'wordpress') {
- await api.installWordPress({
- path: formData.root_path,
- site_url: formData.site_url,
- site_title: formData.site_title || formData.name,
- admin_user: formData.admin_user,
- admin_email: formData.admin_email,
- db_name: formData.db_name,
- db_user: formData.db_user,
- db_password: formData.db_password,
- php_version: formData.php_version,
- });
- } else if (appType === 'flask') {
- await api.createFlaskApp({
- name: formData.name,
- path: formData.root_path,
- python_version: formData.python_version,
- port: formData.port,
- workers: formData.workers || 2,
- });
- } else if (appType === 'django') {
- await api.createDjangoApp({
- name: formData.name,
- path: formData.root_path,
- python_version: formData.python_version,
- port: formData.port,
- workers: formData.workers || 2,
- });
- } else if (appType === 'docker') {
- const ports = formData.docker_ports ? formData.docker_ports.split(',').map(p => p.trim()) : [];
- const volumes = formData.docker_volumes ? formData.docker_volumes.split(',').map(v => v.trim()) : [];
- const env = formData.docker_env ? Object.fromEntries(
- formData.docker_env.split('\n').filter(l => l.includes('=')).map(l => {
- const [key, ...rest] = l.split('=');
- return [key.trim(), rest.join('=').trim()];
- })
- ) : {};
-
- await api.createDockerApp({
- name: formData.name,
- path: formData.root_path,
- image: formData.docker_image,
- ports,
- volumes,
- env,
- });
- } else {
- await api.createApp({
- name: formData.name,
- app_type: appType,
- php_version: appType === 'php' ? formData.php_version : null,
- root_path: formData.root_path,
- });
- }
-
- onCreated();
- onClose();
- } catch (err) {
- setError(err.message || 'Failed to create application');
- } finally {
- setLoading(false);
- }
+ function goToAllTemplates() {
+ onClose();
+ navigate('/templates');
}
return (
<div className="modal-overlay" onClick={onClose}>
- <div className="modal" onClick={e => e.stopPropagation()}>
+ <div className="modal modal-lg" onClick={e => e.stopPropagation()}>
<div className="modal-header">
- <h2>{step === 1 ? 'Select Application Type' : `Create ${appTypes.find(t => t.id === appType)?.name} App`}</h2>
+ <h2>Select Application Type</h2>
<button className="modal-close" onClick={onClose}>
<X size={20} />
</button>
</div>
- {error && <div className="error-message">{error}</div>}
-
- {step === 1 ? (
- <div className="app-type-grid">
- {appTypes.map(type => (
- <button
- key={type.id}
- className="app-type-card"
- onClick={() => selectAppType(type.id)}
- >
- <div className="app-type-icon" style={{ background: type.color }}>
- {type.id === 'docker' ? <Container size={20} /> : type.icon}
- </div>
- <h3>{type.name}</h3>
- <p>{type.description}</p>
- </button>
- ))}
- </div>
- ) : (
- <form onSubmit={handleSubmit}>
- <div className="form-group">
- <label>Site Name</label>
- <input
- type="text"
- name="name"
- value={formData.name}
- onChange={handleChange}
- placeholder="My Awesome Site"
- required
- />
- </div>
-
- <div className="form-group">
- <label>Root Path</label>
- <input
- type="text"
- name="root_path"
- value={formData.root_path}
- onChange={handleChange}
- placeholder="/var/www/mysite"
- required
- />
- </div>
-
- {appType === 'wordpress' && (
- <>
- <div className="form-group">
- <label>Site URL</label>
- <input
- type="url"
- name="site_url"
- value={formData.site_url}
- onChange={handleChange}
- placeholder="https://example.com"
- required
- />
- </div>
-
- <div className="form-group">
- <label>Admin Email</label>
- <input
- type="email"
- name="admin_email"
- value={formData.admin_email}
- onChange={handleChange}
- placeholder="[email protected]"
- required
- />
- </div>
-
- <div className="form-row">
- <div className="form-group">
- <label>Database Name</label>
- <input
- type="text"
- name="db_name"
- value={formData.db_name}
- onChange={handleChange}
- required
- />
- </div>
- <div className="form-group">
- <label>Database User</label>
- <input
- type="text"
- name="db_user"
- value={formData.db_user}
- onChange={handleChange}
- required
- />
- </div>
- </div>
-
- <div className="form-group">
- <label>Database Password</label>
- <input
- type="password"
- name="db_password"
- value={formData.db_password}
- onChange={handleChange}
- required
- />
- </div>
- </>
- )}
-
- {(appType === 'php' || appType === 'wordpress') && (
- <div className="form-group">
- <label>PHP Version</label>
- <select name="php_version" value={formData.php_version} onChange={handleChange}>
- <option value="8.0">PHP 8.0</option>
- <option value="8.1">PHP 8.1</option>
- <option value="8.2">PHP 8.2</option>
- <option value="8.3">PHP 8.3</option>
- </select>
+ <div className="app-type-grid">
+ {templates.map(template => (
+ <button
+ key={template.id}
+ className="app-type-card"
+ onClick={() => selectTemplate(template.id)}
+ >
+ <div className="app-type-icon" style={{ background: template.color }}>
+ {template.id === 'portainer' ? <Container size={20} /> : template.icon}
</div>
- )}
-
- {['flask', 'django'].includes(appType) && (
- <>
- <div className="form-group">
- <label>Python Version</label>
- <select name="python_version" value={formData.python_version} onChange={handleChange}>
- <option value="3.9">Python 3.9</option>
- <option value="3.10">Python 3.10</option>
- <option value="3.11">Python 3.11</option>
- <option value="3.12">Python 3.12</option>
- </select>
- </div>
- <div className="form-group">
- <label>Port</label>
- <input
- type="number"
- name="port"
- value={formData.port}
- onChange={handleChange}
- min="1024"
- max="65535"
- />
- </div>
- </>
- )}
-
- {appType === 'docker' && (
- <>
- <div className="form-group">
- <label>Docker Image *</label>
- <input
- type="text"
- name="docker_image"
- value={formData.docker_image}
- onChange={handleChange}
- placeholder="nginx:latest, mysql:8, redis:alpine"
- required
- />
- </div>
- <div className="form-group">
- <label>Ports (comma-separated)</label>
- <input
- type="text"
- name="docker_ports"
- value={formData.docker_ports}
- onChange={handleChange}
- placeholder="8080:80, 3306:3306"
- />
- </div>
- <div className="form-group">
- <label>Volumes (comma-separated)</label>
- <input
- type="text"
- name="docker_volumes"
- value={formData.docker_volumes}
- onChange={handleChange}
- placeholder="/host/data:/container/data"
- />
- </div>
- <div className="form-group">
- <label>Environment Variables (one per line, KEY=value)</label>
- <textarea
- name="docker_env"
- value={formData.docker_env}
- onChange={handleChange}
- placeholder="MYSQL_ROOT_PASSWORD=secret&#10;MYSQL_DATABASE=mydb"
- rows={4}
- />
- </div>
- </>
- )}
+ <h3>{template.name}</h3>
+ <p>{template.description}</p>
+ </button>
+ ))}
+ </div>
- <div className="modal-actions">
- <button type="button" className="btn btn-secondary" onClick={() => setStep(1)}>
- Back
- </button>
- <button type="submit" className="btn btn-primary" disabled={loading}>
- {loading ? 'Creating...' : 'Create Application'}
- </button>
- </div>
- </form>
- )}
+ <div className="modal-footer">
+ <button className="btn btn-secondary" onClick={goToAllTemplates}>
+ Browse All Templates
+ </button>
+ </div>
</div>
</div>
);
diff --git a/frontend/src/pages/Templates.jsx b/frontend/src/pages/Templates.jsx
index 44427d13..fd560dab 100644
--- a/frontend/src/pages/Templates.jsx
+++ b/frontend/src/pages/Templates.jsx
@@ -110,11 +110,28 @@ const Templates = () => {
const selectedCategory = searchParams.get('category') || null;
const searchQuery = searchParams.get('search') || '';
const sortBy = searchParams.get('sort') || 'name-asc';
+ const installTemplateId = searchParams.get('install');
useEffect(() => {
loadData();
}, []);
+ // Auto-open install modal if template ID is in URL
+ useEffect(() => {
+ if (installTemplateId && templates.length > 0 && !loading) {
+ const template = templates.find(t => t.id === installTemplateId);
+ if (template) {
+ handleViewTemplate(template).then(() => {
+ setShowInstallModal(true);
+ });
+ // Clear the install param from URL
+ const newParams = new URLSearchParams(searchParams);
+ newParams.delete('install');
+ setSearchParams(newParams, { replace: true });
+ }
+ }
+ }, [installTemplateId, templates, loading]);
+
useEffect(() => {
loadTemplates();
}, [selectedCategory, searchQuery]);