import { useCallback, useState } from "react"; import { Button, Dialog, DialogActions, DialogBody, DialogContent, DialogSurface, DialogTitle, Field, Input, MessageBar, MessageBarBody, Spinner, Switch, Table, TableBody, TableCell, TableCellLayout, TableHeader, TableHeaderCell, TableRow, Text, } from "@fluentui/react-components"; import { useCommonSectionStyles } from "../../theme/commonStyles"; import { AddRegular, ArrowClockwiseRegular, DeleteRegular, EditRegular, EyeRegular, PlayRegular, } from "@fluentui/react-icons"; import { useBlocklists } from "../../hooks/useBlocklist"; import type { BlocklistSource, PreviewResponse } from "../../types/blocklist"; import { useBlocklistStyles } from "./blocklistStyles"; interface SourceFormValues { name: string; url: string; enabled: boolean; } interface SourceFormDialogProps { open: boolean; mode: "add" | "edit"; initial: SourceFormValues; saving: boolean; error: string | null; onClose: () => void; onSubmit: (values: SourceFormValues) => void; } function SourceFormDialog({ open, mode, initial, saving, error, onClose, onSubmit, }: SourceFormDialogProps): React.JSX.Element { const styles = useBlocklistStyles(); const [values, setValues] = useState(initial); const handleOpen = useCallback((): void => { setValues(initial); }, [initial]); return ( { if (!data.open) onClose(); }} > {mode === "add" ? "Add Blocklist Source" : "Edit Blocklist Source"}
{error && ( {error} )} { setValues((p) => ({ ...p, name: d.value })); }} placeholder="e.g. Blocklist.de — All" /> { setValues((p) => ({ ...p, url: d.value })); }} placeholder="https://lists.blocklist.de/lists/all.txt" /> { setValues((p) => ({ ...p, enabled: d.checked })); }} />
); } interface PreviewDialogProps { open: boolean; source: BlocklistSource | null; onClose: () => void; fetchPreview: (id: number) => Promise; } function PreviewDialog({ open, source, onClose, fetchPreview }: PreviewDialogProps): React.JSX.Element { const styles = useBlocklistStyles(); const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleOpen = useCallback((): void => { if (!source) return; setData(null); setError(null); setLoading(true); fetchPreview(source.id) .then((result) => { setData(result); setLoading(false); }) .catch((err: unknown) => { setError(err instanceof Error ? err.message : "Failed to fetch preview"); setLoading(false); }); }, [source, fetchPreview]); return ( { if (!d.open) onClose(); }} > Preview — {source?.name ?? ""} {loading && (
)} {error && ( {error} )} {data && (
{data.valid_count} valid IPs / {data.skipped_count} skipped of {data.total_lines} total lines. Showing first {data.entries.length}:
{data.entries.map((entry) => (
{entry}
))}
)}
); } interface SourcesSectionProps { onRunImport: () => void; runImportRunning: boolean; } const EMPTY_SOURCE: SourceFormValues = { name: "", url: "", enabled: true }; export function BlocklistSourcesSection({ onRunImport, runImportRunning }: SourcesSectionProps): React.JSX.Element { const styles = useBlocklistStyles(); const sectionStyles = useCommonSectionStyles(); const { sources, loading, error, refresh, createSource, updateSource, removeSource, previewSource } = useBlocklists(); const [dialogOpen, setDialogOpen] = useState(false); const [dialogMode, setDialogMode] = useState<"add" | "edit">("add"); const [dialogInitial, setDialogInitial] = useState(EMPTY_SOURCE); const [editingId, setEditingId] = useState(null); const [saving, setSaving] = useState(false); const [saveError, setSaveError] = useState(null); const [previewOpen, setPreviewOpen] = useState(false); const [previewSourceItem, setPreviewSourceItem] = useState(null); const openAdd = useCallback((): void => { setDialogMode("add"); setDialogInitial(EMPTY_SOURCE); setEditingId(null); setSaveError(null); setDialogOpen(true); }, []); const openEdit = useCallback((source: BlocklistSource): void => { setDialogMode("edit"); setDialogInitial({ name: source.name, url: source.url, enabled: source.enabled }); setEditingId(source.id); setSaveError(null); setDialogOpen(true); }, []); const handleSubmit = useCallback( (values: SourceFormValues): void => { setSaving(true); setSaveError(null); const op = dialogMode === "add" ? createSource({ name: values.name, url: values.url, enabled: values.enabled }) : updateSource(editingId ?? -1, { name: values.name, url: values.url, enabled: values.enabled }); op .then(() => { setSaving(false); setDialogOpen(false); }) .catch((err: unknown) => { setSaving(false); setSaveError(err instanceof Error ? err.message : "Failed to save source"); }); }, [dialogMode, editingId, createSource, updateSource], ); const handleToggleEnabled = useCallback( (source: BlocklistSource): void => { void updateSource(source.id, { enabled: !source.enabled }); }, [updateSource], ); const handleDelete = useCallback( (source: BlocklistSource): void => { void removeSource(source.id); }, [removeSource], ); const handlePreview = useCallback((source: BlocklistSource): void => { setPreviewSourceItem(source); setPreviewOpen(true); }, []); return (
Blocklist Sources
{error && ( {error} )} {loading ? (
) : sources.length === 0 ? (
No blocklist sources configured. Click "Add Source" to get started.
) : (
Name URL Enabled Actions {sources.map((source) => ( {source.name} {source.url} { handleToggleEnabled(source); }} label={source.enabled ? "On" : "Off"} />
))}
)} { setDialogOpen(false); }} onSubmit={handleSubmit} /> { setPreviewOpen(false); }} fetchPreview={previewSource} />
); }