import { useState, useEffect } from 'react'; import { X, Link2, GitBranch, AlertCircle, Check } from 'lucide-react'; import api from '../services/api'; import Modal from './Modal'; const LinkAppModal = ({ app, onClose, onLinked }) => { const [apps, setApps] = useState([]); const [loading, setLoading] = useState(true); const [linking, setLinking] = useState(false); const [error, setError] = useState(''); const [selectedAppId, setSelectedAppId] = useState(''); const [asEnvironment, setAsEnvironment] = useState('development'); const [tablePrefix, setTablePrefix] = useState(''); const [propagateCredentials, setPropagateCredentials] = useState(true); useEffect(() => { loadCompatibleApps(); }, [app]); async function loadCompatibleApps() { try { const data = await api.getApps(); // Filter to same type, not already linked, not self const compatible = (data.apps || []).filter(a => a.id !== app.id && a.app_type === app.app_type && !a.has_linked_app && a.environment_type === 'standalone' ); setApps(compatible); } catch (err) { setError('Failed to load applications'); } finally { setLoading(false); } } async function handleLink(e) { e.preventDefault(); if (!selectedAppId) { setError('Please select an application to link'); return; } setLinking(true); setError(''); try { await api.linkApp(app.id, parseInt(selectedAppId), asEnvironment, { propagateCredentials, tablePrefix: tablePrefix || undefined }); onLinked(); onClose(); } catch (err) { setError(err.message || 'Failed to link applications'); } finally { setLinking(false); } } const selectedApp = apps.find(a => a.id === parseInt(selectedAppId)); const envDescriptions = { development: 'This app will be the DEVELOPMENT environment', production: 'This app will be the PRODUCTION environment', staging: 'This app will be the STAGING environment' }; return ( {error && (
{error}
)} {loading ? (
Loading compatible apps...
) : apps.length === 0 ? (

No Compatible Apps

There are no other {app.app_type} applications available to link. Create another {app.app_type} app first, or ensure existing apps are not already linked.

) : (
Current app: {app.name} {app.app_type.toUpperCase()}
{['development', 'production', 'staging'].map(env => ( ))}
{selectedApp && (
Preview
{app.name} {asEnvironment === 'development' ? 'DEV' : asEnvironment === 'production' ? 'PROD' : 'STAGING'}
{selectedApp.name} {asEnvironment === 'development' ? 'PROD' : 'DEV'}
)} {app.app_type === 'docker' && ( <>
Copy database connection settings from production to development app
{propagateCredentials && (
setTablePrefix(e.target.value)} placeholder="wp_dev_ (auto-generated if empty)" /> Different prefix allows both apps to share the same database
)} )}
)}
); }; export default LinkAppModal;