import { useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Switch, Button, Stack, Paper, Text, Loader, Group } from '@mantine/core'; import { alert } from '@app/components/toast'; import RestartConfirmationModal from '@app/components/shared/config/RestartConfirmationModal'; import { useRestartServer } from '@app/components/shared/config/useRestartServer'; import { useAdminSettings } from '@app/hooks/useAdminSettings'; import PendingBadge from '@app/components/shared/config/PendingBadge'; import { useLoginRequired } from '@app/hooks/useLoginRequired'; import LoginRequiredBanner from '@app/components/shared/config/LoginRequiredBanner'; import apiClient from '@app/services/apiClient'; interface PrivacySettingsData { enableAnalytics?: boolean; googleVisibility?: boolean; metricsEnabled?: boolean; } export default function AdminPrivacySection() { const { t } = useTranslation(); const { loginEnabled, validateLoginEnabled, getDisabledStyles } = useLoginRequired(); const { restartModalOpened, showRestartModal, closeRestartModal, restartServer } = useRestartServer(); const { settings, setSettings, loading, saving, fetchSettings, saveSettings, isFieldPending, } = useAdminSettings({ sectionName: 'privacy', fetchTransformer: async () => { const [metricsResponse, systemResponse] = await Promise.all([ apiClient.get('/api/v1/admin/settings/section/metrics'), apiClient.get('/api/v1/admin/settings/section/system') ]); const metrics = metricsResponse.data; const system = systemResponse.data; const result: any = { enableAnalytics: system.enableAnalytics || false, googleVisibility: system.googlevisibility || false, metricsEnabled: metrics.enabled || false }; // Merge pending blocks from both endpoints const pendingBlock: any = {}; if (system._pending?.enableAnalytics !== undefined) { pendingBlock.enableAnalytics = system._pending.enableAnalytics; } if (system._pending?.googlevisibility !== undefined) { pendingBlock.googleVisibility = system._pending.googlevisibility; } if (metrics._pending?.enabled !== undefined) { pendingBlock.metricsEnabled = metrics._pending.enabled; } if (Object.keys(pendingBlock).length > 0) { result._pending = pendingBlock; } return result; }, saveTransformer: (settings) => { const deltaSettings = { 'system.enableAnalytics': settings.enableAnalytics, 'system.googlevisibility': settings.googleVisibility, 'metrics.enabled': settings.metricsEnabled }; return { sectionData: {}, deltaSettings }; } }); useEffect(() => { if (loginEnabled) { fetchSettings(); } }, [loginEnabled, fetchSettings]); const handleSave = async () => { if (!validateLoginEnabled()) { return; } try { await saveSettings(); showRestartModal(); } catch (_error) { alert({ alertType: 'error', title: t('admin.error', 'Error'), body: t('admin.settings.saveError', 'Failed to save settings'), }); } }; // Override loading state when login is disabled const actualLoading = loginEnabled ? loading : false; if (actualLoading) { return ( ); } return (
{t('admin.settings.privacy.title', 'Privacy')} {t('admin.settings.privacy.description', 'Configure privacy and data collection settings.')}
{/* Analytics & Tracking */} {t('admin.settings.privacy.analytics', 'Analytics & Tracking')}
{t('admin.settings.privacy.enableAnalytics.label', 'Enable Analytics')} {t('admin.settings.privacy.enableAnalytics.description', 'Collect anonymous usage analytics to help improve the application')}
{ if (!loginEnabled) return; setSettings({ ...settings, enableAnalytics: e.target.checked }); }} disabled={!loginEnabled} styles={getDisabledStyles()} />
{t('admin.settings.privacy.metricsEnabled.label', 'Enable Metrics')} {t('admin.settings.privacy.metricsEnabled.description', 'Enable collection of performance and usage metrics')}
{ if (!loginEnabled) return; setSettings({ ...settings, metricsEnabled: e.target.checked }); }} disabled={!loginEnabled} styles={getDisabledStyles()} />
{/* Search Engine Visibility */} {t('admin.settings.privacy.searchEngine', 'Search Engine Visibility')}
{t('admin.settings.privacy.googleVisibility.label', 'Google Visibility')} {t('admin.settings.privacy.googleVisibility.description', 'Allow search engines to index this application')}
{ if (!loginEnabled) return; setSettings({ ...settings, googleVisibility: e.target.checked }); }} disabled={!loginEnabled} styles={getDisabledStyles()} />
{/* Save Button */} {/* Restart Confirmation Modal */}
); }