File size: 4,332 Bytes
16b7924
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
  Alert,
  Button,
  Group,
  Modal,
  NumberInput,
  Stack,
  Switch,
  Text,
} from "@mantine/core";
import { useForm } from "@mantine/form";
import { notifications } from "@mantine/notifications";
import { IconInfoCircle } from "@tabler/icons-react";
import { usePubSub } from "create-pubsub/react";
import { useState } from "react";
import { useSearchHistory } from "../../hooks/useSearchHistory";
import { addLogEntry } from "../../modules/logEntries";
import { settingsPubSub } from "../../modules/pubSub";

interface HistorySettingsProps {
  onClose?: () => void;
}

export default function HistorySettings({ onClose }: HistorySettingsProps) {
  const [settings, setSettings] = usePubSub(settingsPubSub);
  const { recentSearches, clearAll } = useSearchHistory();
  const [showClearConfirm, setShowClearConfirm] = useState(false);

  const form = useForm({
    initialValues: {
      enableHistory: settings.enableHistory,
      historyMaxEntries: settings.historyMaxEntries,
      historyAutoCleanup: settings.historyAutoCleanup,
      historyRetentionDays: settings.historyRetentionDays,
    },
    onValuesChange: (values) => {
      setSettings({
        ...settings,
        ...values,
      });
    },
  });

  const handleClearHistory = async () => {
    try {
      await clearAll();
      setShowClearConfirm(false);
      notifications.show({
        title: "History Cleared",
        message: "All search history has been cleared",
        color: "blue",
      });
      addLogEntry("User cleared all search history");
    } catch (_) {
      notifications.show({
        title: "Clear Failed",
        message: "Failed to clear history. Please try again.",
        color: "red",
      });
    }
  };

  return (
    <>
      <Stack gap="md" p="md">
        <Switch
          {...form.getInputProps("enableHistory", {
            type: "checkbox",
          })}
          label="Enable Search History"
          description="Store your searches for quick access and suggestions"
        />

        {form.values.enableHistory && (
          <>
            <NumberInput
              {...form.getInputProps("historyMaxEntries")}
              label="Maximum Entries"
              description="Maximum number of searches to keep in history"
              min={10}
              max={10000}
              step={50}
            />

            <Switch
              {...form.getInputProps("historyAutoCleanup", {
                type: "checkbox",
              })}
              label="Automatic Cleanup"
              description="Automatically remove old entries to maintain performance"
            />

            {form.values.historyAutoCleanup && (
              <NumberInput
                {...form.getInputProps("historyRetentionDays")}
                label="Retention Days"
                min={0}
              />
            )}

            <Button
              color="default"
              variant="default"
              onClick={() => setShowClearConfirm(true)}
              disabled={recentSearches.length === 0}
            >
              Clear all history
            </Button>

            {onClose && (
              <Button variant="outline" onClick={onClose}>
                Close
              </Button>
            )}
          </>
        )}
      </Stack>

      <Modal
        opened={showClearConfirm}
        onClose={() => setShowClearConfirm(false)}
        title="Clear Search History"
        centered
      >
        <Stack gap="md">
          <Alert color="orange" icon={<IconInfoCircle size={16} />}>
            This action will permanently delete all {recentSearches.length}{" "}
            search entries. This cannot be undone.
          </Alert>

          <Text size="sm">
            Are you sure you want to clear your entire search history?
          </Text>

          <Group justify="flex-end" gap="sm">
            <Button
              variant="subtle"
              color="default"
              onClick={handleClearHistory}
            >
              Clear History
            </Button>
            <Button
              variant="default"
              color="default"
              onClick={() => setShowClearConfirm(false)}
            >
              Cancel
            </Button>
          </Group>
        </Stack>
      </Modal>
    </>
  );
}