Matthias Nott
2026-03-07 d97d98a524d2b2dd56c718a806eecab0b3270c88
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import React, { useCallback, useState } from "react";
import {
  Alert,
  Keyboard,
  KeyboardAvoidingView,
  Platform,
  Pressable,
  ScrollView,
  Text,
  TextInput,
  TouchableWithoutFeedback,
  View,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { router } from "expo-router";
import { useConnection } from "../contexts/ConnectionContext";
import { useTheme } from "../contexts/ThemeContext";
import { StatusDot } from "../components/ui/StatusDot";
import { ServerConfig } from "../types";
import { sendWol, isValidMac } from "../services/wol";
import { wsClient } from "../services/websocket";
export default function SettingsScreen() {
  const { serverConfig, status, connect, disconnect, saveServerConfig } =
    useConnection();
  const { colors } = useTheme();
  const [host, setHost] = useState(serverConfig?.host ?? "");
  const [localHost, setLocalHost] = useState(serverConfig?.localHost ?? "");
  const [port, setPort] = useState(
    serverConfig?.port ? String(serverConfig.port) : "8765"
  );
  const [macAddress, setMacAddress] = useState(serverConfig?.macAddress ?? "");
  const [saved, setSaved] = useState(false);
  const [waking, setWaking] = useState(false);
  const handleSave = useCallback(async () => {
    const trimmedHost = host.trim();
    const portNum = parseInt(port.trim(), 10);
    const trimmedLocal = localHost.trim();
    if ((!trimmedHost && !trimmedLocal) || isNaN(portNum) || portNum < 1 || portNum > 65535) {
      return;
    }
    const trimmedMac = macAddress.trim();
    const effectiveHost = trimmedHost || trimmedLocal;
    const config: ServerConfig = {
      host: effectiveHost,
      port: portNum,
      ...(trimmedLocal && trimmedHost ? { localHost: trimmedLocal } : {}),
      ...(trimmedMac ? { macAddress: trimmedMac } : {}),
    };
    await saveServerConfig(config);
    setSaved(true);
    setTimeout(() => setSaved(false), 2000);
  }, [host, localHost, port, macAddress, saveServerConfig]);
  const handleConnect = useCallback(() => {
    if (status === "connected" || status === "connecting") {
      disconnect();
    } else {
      connect();
    }
  }, [status, connect, disconnect]);
  const handleWake = useCallback(async () => {
    const mac = macAddress.trim() || serverConfig?.macAddress;
    if (!mac || !isValidMac(mac)) {
      Alert.alert("Invalid MAC", "Enter a valid MAC address (e.g. 6a:8a:e7:b3:8e:5c)");
      return;
    }
    setWaking(true);
    try {
      await sendWol(mac, host.trim() || serverConfig?.host);
      Alert.alert("WoL Sent", "Magic packet sent. The Mac should wake in a few seconds.");
    } catch (err) {
      Alert.alert("WoL Failed", err instanceof Error ? err.message : String(err));
    } finally {
      setWaking(false);
    }
  }, [macAddress, host, serverConfig]);
  const isFormValid = (host.trim().length > 0 || localHost.trim().length > 0) && parseInt(port, 10) > 0;
  return (
    <SafeAreaView style={{ flex: 1, backgroundColor: colors.bg }} edges={["top", "bottom"]}>
      <KeyboardAvoidingView
        style={{ flex: 1 }}
        behavior={Platform.OS === "ios" ? "padding" : "height"}
      >
        <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
          <ScrollView
            style={{ flex: 1 }}
            contentContainerStyle={{ paddingBottom: 32 }}
            keyboardShouldPersistTaps="handled"
          >
            {/* Header */}
            <View
              style={{
                flexDirection: "row",
                alignItems: "center",
                paddingHorizontal: 16,
                paddingVertical: 12,
                borderBottomWidth: 1,
                borderBottomColor: colors.border,
              }}
            >
              <Pressable
                onPress={() => router.back()}
                hitSlop={{ top: 6, bottom: 6, left: 6, right: 6 }}
                style={{
                  width: 36,
                  height: 36,
                  alignItems: "center",
                  justifyContent: "center",
                  borderRadius: 18,
                  backgroundColor: colors.bgTertiary,
                  marginRight: 12,
                }}
              >
                <Text style={{ color: colors.text, fontSize: 16 }}>{"\u2190"}</Text>
              </Pressable>
              <Text style={{ color: colors.text, fontSize: 22, fontWeight: "800", letterSpacing: -0.5 }}>
                Settings
              </Text>
            </View>
            <View style={{ paddingHorizontal: 16, marginTop: 24 }}>
              {/* Connection status card */}
              <View
                style={{
                  backgroundColor: colors.bgTertiary,
                  borderRadius: 16,
                  padding: 16,
                  marginBottom: 24,
                }}
              >
                <Text
                  style={{
                    color: colors.textSecondary,
                    fontSize: 11,
                    fontWeight: "500",
                    textTransform: "uppercase",
                    letterSpacing: 1.5,
                    marginBottom: 12,
                  }}
                >
                  Connection Status
                </Text>
                <View style={{ flexDirection: "row", alignItems: "center", gap: 12 }}>
                  <StatusDot status={status} size={12} />
                  <Text style={{ color: colors.text, fontSize: 16, fontWeight: "500" }}>
                    {status === "connected"
                      ? "Connected"
                      : status === "connecting"
                      ? "Connecting..."
                      : "Disconnected"}
                  </Text>
                </View>
                {serverConfig && (
                  <Text style={{ color: colors.textMuted, fontSize: 14, marginTop: 8 }}>
                    {wsClient.currentUrl || `ws://${serverConfig.host}:${serverConfig.port}`}
                  </Text>
                )}
              </View>
              {/* Server config */}
              <Text
                style={{
                  color: colors.textSecondary,
                  fontSize: 11,
                  fontWeight: "500",
                  textTransform: "uppercase",
                  letterSpacing: 1.5,
                  marginBottom: 12,
                }}
              >
                Server Configuration
              </Text>
              <View
                style={{
                  backgroundColor: colors.bgTertiary,
                  borderRadius: 16,
                  overflow: "hidden",
                  marginBottom: 16,
                }}
              >
                {/* Local Host (preferred when on same network) */}
                <View
                  style={{
                    paddingHorizontal: 16,
                    paddingVertical: 12,
                    borderBottomWidth: 1,
                    borderBottomColor: colors.border,
                  }}
                >
                  <Text style={{ color: colors.textMuted, fontSize: 11, marginBottom: 4 }}>
                    Local Address (optional)
                  </Text>
                  <TextInput
                    value={localHost}
                    onChangeText={setLocalHost}
                    placeholder="192.168.1.100"
                    placeholderTextColor={colors.textMuted}
                    autoCapitalize="none"
                    autoCorrect={false}
                    keyboardType="url"
                    style={{ color: colors.text, fontSize: 16, padding: 0 }}
                  />
                </View>
                {/* Remote Host (fallback / external) */}
                <View
                  style={{
                    paddingHorizontal: 16,
                    paddingVertical: 12,
                    borderBottomWidth: 1,
                    borderBottomColor: colors.border,
                  }}
                >
                  <Text style={{ color: colors.textMuted, fontSize: 11, marginBottom: 4 }}>
                    Remote Address
                  </Text>
                  <TextInput
                    value={host}
                    onChangeText={setHost}
                    placeholder="myhost.example.com"
                    placeholderTextColor={colors.textMuted}
                    autoCapitalize="none"
                    autoCorrect={false}
                    keyboardType="url"
                    style={{ color: colors.text, fontSize: 16, padding: 0 }}
                  />
                </View>
                {/* Port */}
                <View
                  style={{
                    paddingHorizontal: 16,
                    paddingVertical: 12,
                    borderBottomWidth: 1,
                    borderBottomColor: colors.border,
                  }}
                >
                  <Text style={{ color: colors.textMuted, fontSize: 11, marginBottom: 4 }}>
                    Port
                  </Text>
                  <TextInput
                    value={port}
                    onChangeText={setPort}
                    placeholder="8765"
                    placeholderTextColor={colors.textMuted}
                    keyboardType="number-pad"
                    style={{ color: colors.text, fontSize: 16, padding: 0 }}
                  />
                </View>
                {/* MAC Address for Wake-on-LAN */}
                <View style={{ paddingHorizontal: 16, paddingVertical: 12 }}>
                  <Text style={{ color: colors.textMuted, fontSize: 11, marginBottom: 4 }}>
                    MAC Address (Wake-on-LAN)
                  </Text>
                  <TextInput
                    value={macAddress}
                    onChangeText={setMacAddress}
                    placeholder="6a:8a:e7:b3:8e:5c"
                    placeholderTextColor={colors.textMuted}
                    autoCapitalize="none"
                    autoCorrect={false}
                    style={{ color: colors.text, fontSize: 16, padding: 0 }}
                  />
                </View>
              </View>
              {/* Save button */}
              <Pressable
                onPress={handleSave}
                disabled={!isFormValid}
                style={{
                  borderRadius: 16,
                  paddingVertical: 16,
                  alignItems: "center",
                  marginBottom: 12,
                  backgroundColor: isFormValid ? colors.accent : colors.bgTertiary,
                }}
              >
                <Text
                  style={{
                    fontSize: 16,
                    fontWeight: "600",
                    color: isFormValid ? "#FFF" : colors.textMuted,
                  }}
                >
                  {saved ? "Saved!" : "Save Configuration"}
                </Text>
              </Pressable>
              {/* Wake-on-LAN button */}
              <Pressable
                onPress={handleWake}
                disabled={waking || (!macAddress.trim() && !serverConfig?.macAddress)}
                style={{
                  borderRadius: 16,
                  paddingVertical: 16,
                  alignItems: "center",
                  marginBottom: 12,
                  backgroundColor:
                    macAddress.trim() || serverConfig?.macAddress
                      ? "#FF950033"
                      : colors.bgTertiary,
                }}
              >
                <Text
                  style={{
                    fontSize: 16,
                    fontWeight: "600",
                    color:
                      macAddress.trim() || serverConfig?.macAddress
                        ? "#FF9500"
                        : colors.textMuted,
                  }}
                >
                  {waking ? "Sending..." : "Wake Mac (WoL)"}
                </Text>
              </Pressable>
              {/* Connect/Disconnect button */}
              <Pressable
                onPress={handleConnect}
                disabled={!serverConfig}
                style={{
                  borderRadius: 16,
                  paddingVertical: 16,
                  alignItems: "center",
                  backgroundColor:
                    status === "connected"
                      ? colors.danger + "33"
                      : "#2ED57333",
                }}
              >
                <Text
                  style={{
                    fontSize: 16,
                    fontWeight: "600",
                    color: status === "connected" ? colors.danger : "#2ED573",
                  }}
                >
                  {status === "connected"
                    ? "Disconnect"
                    : status === "connecting"
                    ? "Connecting..."
                    : "Connect"}
                </Text>
              </Pressable>
            </View>
          </ScrollView>
        </TouchableWithoutFeedback>
      </KeyboardAvoidingView>
    </SafeAreaView>
  );
}