import React, { useState, useEffect } from 'react';
import { Shield, Plus, Trash2, Star, Edit3, Save } from 'lucide-react';
import wordpressApi from '../../services/wordpress';
import Spinner from '../Spinner';
const SanitizationProfileForm = ({ onClose, onProfilesChange }) => {
const [profiles, setProfiles] = useState([]);
const [loading, setLoading] = useState(true);
const [editingProfile, setEditingProfile] = useState(null);
const [showCreateForm, setShowCreateForm] = useState(false);
useEffect(() => {
loadProfiles();
}, []);
async function loadProfiles() {
setLoading(true);
try {
const data = await wordpressApi.getSanitizationProfiles();
setProfiles(data.profiles || []);
} catch {
setProfiles([]);
} finally {
setLoading(false);
}
}
async function handleDelete(profileId) {
try {
await wordpressApi.deleteSanitizationProfile(profileId);
setProfiles(prev => prev.filter(p => p.id !== profileId));
onProfilesChange?.();
} catch {
// handled silently
}
}
async function handleSetDefault(profileId) {
try {
await wordpressApi.updateSanitizationProfile(profileId, { is_default: true });
setProfiles(prev => prev.map(p => ({
...p,
is_default: p.id === profileId
})));
onProfilesChange?.();
} catch {
// handled silently
}
}
async function handleSaveProfile(profileData) {
try {
if (editingProfile) {
const result = await wordpressApi.updateSanitizationProfile(
editingProfile.id,
profileData
);
setProfiles(prev => prev.map(p =>
p.id === editingProfile.id ? result.profile : p
));
} else {
const result = await wordpressApi.createSanitizationProfile(profileData);
setProfiles(prev => [...prev, result.profile]);
}
setEditingProfile(null);
setShowCreateForm(false);
onProfilesChange?.();
} catch {
// handled silently
}
}
return (
e.stopPropagation()}>
Sanitization Profiles
{loading ? (
) : (
<>
{profiles.map(profile => (
{ setEditingProfile(profile); setShowCreateForm(false); }}
onDelete={() => handleDelete(profile.id)}
onSetDefault={() => handleSetDefault(profile.id)}
/>
))}
{!showCreateForm && !editingProfile && (
)}
{(showCreateForm || editingProfile) && (
{ setEditingProfile(null); setShowCreateForm(false); }}
/>
)}
>
)}
);
};
const ProfileCard = ({ profile, onEdit, onDelete, onSetDefault }) => (
{profile.name}
{profile.is_builtin && Built-in}
{profile.is_default && Default}
{!profile.is_default && (
)}
{!profile.is_builtin && (
)}
{profile.description && (
{profile.description}
)}
);
const ProfileRuleTags = ({ config }) => {
const tags = [];
if (config.anonymize_emails) tags.push('Anonymize emails');
if (config.anonymize_names) tags.push('Anonymize names');
if (config.reset_passwords) tags.push('Reset passwords');
if (config.strip_payment_data) tags.push('Strip payments');
if (config.remove_transients) tags.push('Clear transients');
if (config.truncate_tables?.length > 0) tags.push(`Truncate ${config.truncate_tables.length} tables`);
if (config.exclude_tables?.length > 0) tags.push(`Exclude ${config.exclude_tables.length} tables`);
if (config.custom_search_replace && Object.keys(config.custom_search_replace).length > 0) {
tags.push(`${Object.keys(config.custom_search_replace).length} replacements`);
}
return (
{tags.map(tag => (
{tag}
))}
{tags.length === 0 && (
No rules configured
)}
);
};
const ProfileEditor = ({ profile, onSave, onCancel }) => {
const [name, setName] = useState(profile?.name || '');
const [description, setDescription] = useState(profile?.description || '');
const [config, setConfig] = useState(profile?.config || {
anonymize_emails: false,
anonymize_names: false,
reset_passwords: false,
truncate_tables: [],
exclude_tables: [],
strip_payment_data: false,
remove_transients: false,
custom_search_replace: {},
});
const [truncateInput, setTruncateInput] = useState(
(profile?.config?.truncate_tables || []).join(', ')
);
const [excludeInput, setExcludeInput] = useState(
(profile?.config?.exclude_tables || []).join(', ')
);
const [saving, setSaving] = useState(false);
function handleConfigToggle(key) {
setConfig(prev => ({ ...prev, [key]: !prev[key] }));
}
async function handleSubmit(e) {
e.preventDefault();
setSaving(true);
try {
const finalConfig = {
...config,
truncate_tables: truncateInput.split(',').map(s => s.trim()).filter(Boolean),
exclude_tables: excludeInput.split(',').map(s => s.trim()).filter(Boolean),
};
await onSave({ name, description, config: finalConfig });
} finally {
setSaving(false);
}
}
return (
);
};
export default SanitizationProfileForm;