summaryrefslogtreecommitdiff
path: root/front/src/pages/Settings.tsx
blob: cd9eec745d536f99cc5fb572546eb653070dfc3c (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import useLocalState from "@/state/state";
import { useState } from "react";
import toast from "react-hot-toast";
import { ThemeSwitcher } from "@/styles/ThemeSwitcher";
import Icon from "@/components/Icon";
import "@/styles/Settings.css";

function Settings() {
  const { key, relays, api, addNotification } = useLocalState((s) => ({
    key: s.key,
    relays: s.relays,
    api: s.api,
    addNotification: s.addNotification,
  }));
  const [newRelay, setNewRelay] = useState("");
  const [isAddingRelay, setIsAddingRelay] = useState(false);
  const [isCyclingKey, setIsCyclingKey] = useState(false);

  async function removeRelay(url: string) {
    try {
      await api?.deleteRelay(url);
      toast.success("Relay removed");
    } catch (error) {
      toast.error("Failed to remove relay");
      console.error("Remove relay error:", error);
    }
  }

  async function addNewRelay() {
    if (!newRelay.trim()) {
      toast.error("Please enter a relay URL");
      return;
    }

    setIsAddingRelay(true);
    try {
      const valid = ["wss:", "ws:"];
      const url = new URL(newRelay);
      if (!valid.includes(url.protocol)) {
        toast.error("Invalid Relay URL - must use wss:// or ws://");
        return;
      }

      await api?.addRelay(newRelay);
      toast.success("Relay added");
      setNewRelay("");
    } catch (error) {
      toast.error("Invalid relay URL or failed to add relay");
      console.error("Add relay error:", error);
    } finally {
      setIsAddingRelay(false);
    }
  }

  async function cycleKey() {
    setIsCyclingKey(true);
    try {
      await api?.cycleKeys();
      toast.success("Key cycled successfully");
    } catch (error) {
      toast.error("Failed to cycle key");
      console.error("Cycle key error:", error);
    } finally {
      setIsCyclingKey(false);
    }
  }

  const handleKeyPress = (e: React.KeyboardEvent) => {
    if (e.key === "Enter") {
      addNewRelay();
    }
  };

  return (
    <div className="settings-page">
      <div className="settings-header">
        <h1>Settings</h1>
        <p>Manage your Nostrill configuration and preferences</p>
      </div>

      <div className="settings-content">
        {/* Notifications Test Section - Remove in production */}
        <div className="settings-section">
          <div className="section-header">
            <Icon name="bell" size={20} />
            <h2>Test Notifications</h2>
          </div>
          <div className="section-content">
            <div className="setting-item">
              <div className="setting-info">
                <label>Test Notification System</label>
                <p>Generate test notifications to see how they work</p>
              </div>
              <div className="setting-control">
                <button 
                  className="test-notification-btn"
                  onClick={() => {
                    const types = ["follow", "reply", "react", "mention", "access_request"];
                    const randomType = types[Math.floor(Math.random() * types.length)] as any;
                    addNotification({
                      type: randomType,
                      from: "~sampel-palnet",
                      message: "This is a test notification",
                      reaction: randomType === "react" ? "👍" : undefined,
                    });
                    toast.success("Test notification sent!");
                  }}
                >
                  <Icon name="bell" size={16} />
                  Send Test Notification
                </button>
              </div>
            </div>
          </div>
        </div>

        {/* Appearance Section */}
        <div className="settings-section">
          <div className="section-header">
            <Icon name="settings" size={20} />
            <h2>Appearance</h2>
          </div>
          <div className="section-content">
            <div className="setting-item">
              <div className="setting-info">
                <label>Theme</label>
                <p>Choose your preferred color theme</p>
              </div>
              <div className="setting-control">
                <ThemeSwitcher />
              </div>
            </div>
          </div>
        </div>

        {/* Identity Section */}
        <div className="settings-section">
          <div className="section-header">
            <Icon name="key" size={20} />
            <h2>Identity</h2>
          </div>
          <div className="section-content">
            <div className="setting-item">
              <div className="setting-info">
                <label>Nostr Public Key</label>
                <p>Your unique identifier on the Nostr network</p>
              </div>
              <div className="setting-control">
                <div className="key-display">
                  <code className="pubkey">{key || "No key generated"}</code>
                  <button
                    onClick={cycleKey}
                    disabled={isCyclingKey}
                    className="cycle-btn"
                    title="Generate new key pair"
                  >
                    {isCyclingKey ? (
                      <Icon name="settings" size={16} />
                    ) : (
                      <Icon name="settings" size={16} />
                    )}
                    {isCyclingKey ? "Cycling..." : "Cycle Key"}
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>

        {/* Nostr Relays Section */}
        <div className="settings-section">
          <div className="section-header">
            <Icon name="nostr" size={20} />
            <h2>Nostr Relays</h2>
          </div>
          <div className="section-content">
            <div className="setting-item">
              <div className="setting-info">
                <label>Connected Relays</label>
                <p>Manage your Nostr relay connections</p>
              </div>
              <div className="setting-control">
                <div className="relay-list">
                  {Object.keys(relays).length === 0 ? (
                    <div className="no-relays">
                      <Icon name="nostr" size={24} color="textMuted" />
                      <p>No relays configured</p>
                    </div>
                  ) : (
                    Object.keys(relays).map((url) => (
                      <div key={url} className="relay-item">
                        <div className="relay-info">
                          <span className="relay-url">{url}</span>
                          <span className="relay-status">Connected</span>
                        </div>
                        <button
                          onClick={() => removeRelay(url)}
                          className="remove-relay-btn"
                          title="Remove relay"
                        >
                          ×
                        </button>
                      </div>
                    ))
                  )}

                  <div className="add-relay-form">
                    <div className="relay-input-group">
                      <input
                        type="text"
                        value={newRelay}
                        onChange={(e) => setNewRelay(e.target.value)}
                        onKeyPress={handleKeyPress}
                        placeholder="wss://relay.example.com"
                        className="relay-input"
                      />
                      <button
                        onClick={addNewRelay}
                        disabled={isAddingRelay || !newRelay.trim()}
                        className="add-relay-btn"
                      >
                        {isAddingRelay ? (
                          <>
                            <Icon name="settings" size={16} />
                            Adding...
                          </>
                        ) : (
                          <>
                            <Icon name="settings" size={16} />
                            Add Relay
                          </>
                        )}
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
export default Settings;