File size: 4,606 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use client";

import { useState, useEffect } from "react";
import { Card, Button, Input } from "@/shared/components";
import { useTranslations } from "next-intl";
import { useNotificationStore } from "@/store/notificationStore";

export default function AutoDisableCard() {
  const [data, setData] = useState({ enabled: false, threshold: 3 });
  const [draft, setDraft] = useState({ enabled: false, threshold: 3 });
  const [loading, setLoading] = useState(true);
  const [saving, setSaving] = useState(false);
  const [editMode, setEditMode] = useState(false);
  const t = useTranslations("settings");
  const tc = useTranslations("common");
  const notify = useNotificationStore();

  useEffect(() => {
    fetch("/api/settings/auto-disable-accounts")
      .then((res) => res.json())
      .then((json) => {
        setData(json);
        setDraft(json);
        setLoading(false);
      })
      .catch(() => setLoading(false));
  }, []);

  const handleSave = async () => {
    setSaving(true);
    try {
      const res = await fetch("/api/settings/auto-disable-accounts", {
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(draft),
      });
      if (!res.ok) throw new Error("Failed to save auto-disable config");
      const savedData = await res.json();
      setData(savedData);
      setEditMode(false);
      notify.success(t("savedSuccessfully") || "Saved successfully");
    } catch (err) {
      notify.error(err instanceof Error ? err.message : "Error saving");
    } finally {
      setSaving(false);
    }
  };

  if (loading) return null;

  return (
    <Card className="p-0 overflow-hidden">
      <div className="p-6">
        <div className="flex items-center justify-between mb-4">
          <div className="flex items-center gap-2">
            <span className="material-symbols-outlined text-xl text-primary" aria-hidden="true">
              block
            </span>
            <h2 className="text-lg font-bold">{t("autoDisableBannedAccounts")}</h2>
          </div>
          {editMode ? (
            <div className="flex gap-2">
              <Button
                size="sm"
                variant="secondary"
                onClick={() => {
                  setDraft(data);
                  setEditMode(false);
                }}
              >
                {tc("cancel")}
              </Button>
              <Button
                size="sm"
                variant="primary"
                icon="save"
                onClick={handleSave}
                disabled={saving}
              >
                {tc("save")}
              </Button>
            </div>
          ) : (
            <Button size="sm" variant="secondary" icon="edit" onClick={() => setEditMode(true)}>
              {tc("edit")}
            </Button>
          )}
        </div>

        <p className="text-sm text-text-muted mb-4">{t("autoDisableDescription")}</p>

        <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
          <div className="rounded-lg bg-black/5 dark:bg-white/5 p-4 flex flex-col justify-center">
            <label className="flex items-center gap-3 cursor-pointer">
              <input
                type="checkbox"
                checked={editMode ? draft.enabled : data.enabled}
                onChange={(e) => setDraft((prev) => ({ ...prev, enabled: e.target.checked }))}
                disabled={!editMode}
                className="w-4 h-4 text-primary bg-surface/50 border-white/20 rounded focus:ring-primary/50"
              />
              <span className="text-sm font-medium">{t("autoDisableBannedAccounts")}</span>
            </label>
          </div>

          <div className="rounded-lg bg-black/5 dark:bg-white/5 p-4">
            <h3 className="text-xs font-bold uppercase tracking-wider mb-2 flex items-center gap-2">
              {t("autoDisableThreshold")}
            </h3>
            {editMode ? (
              <Input
                type="number"
                min="1"
                max="10"
                value={draft.threshold}
                onChange={(e) =>
                  setDraft((prev) => ({ ...prev, threshold: parseInt(e.target.value) || 1 }))
                }
              />
            ) : (
              <span className={`text-sm font-mono ${!data.enabled && "opacity-50"}`}>
                {t("failures", { count: data.threshold })}
              </span>
            )}
            <p className="text-xs text-text-muted mt-2">{t("autoDisableThresholdDesc")}</p>
          </div>
        </div>
      </div>
    </Card>
  );
}