Make Nostr git servers configurable

This commit is contained in:
Alex Gleason committed 2025-10-14 20:53:32 -05:00
1 parent e4c3fc8b05
commit ab91a56da6
13 files changed
+145 -22

No files matched your search

+95 -2
View File
@@ -1,8 +1,9 @@
import { ArrowLeft, Wifi, Users, UserPlus, LogOut, Trash2, User, Plus } from 'lucide-react';
import { ArrowLeft, Wifi, Users, UserPlus, LogOut, Trash2, User, Plus, Server, X } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Skeleton } from '@/components/ui/skeleton';
import { RelaySelector } from '@/components/RelaySelector';
@@ -13,6 +14,7 @@ import { useNavigate } from 'react-router-dom';
import { useLoggedInAccounts, type Account } from '@/hooks/useLoggedInAccounts';
import { genUserName } from '@/lib/genUserName';
import { useState } from 'react';
import { useAppContext } from '@/hooks/useAppContext';
export function NostrSettings() {
const { t } = useTranslation();
@@ -21,6 +23,8 @@ export function NostrSettings() {
const { authors, currentUser, setLogin, removeLogin, isLoading } = useLoggedInAccounts();
const [showLoginDialog, setShowLoginDialog] = useState(false);
const [showSignupDialog, setShowSignupDialog] = useState(false);
const { config, updateConfig } = useAppContext();
const [newServerHostname, setNewServerHostname] = useState('');
const handleSwitchAccount = (accountId: string) => {
setLogin(accountId);
@@ -42,6 +46,30 @@ export function NostrSettings() {
return account.metadata?.name ?? genUserName(account.pubkey);
};
const handleAddServer = () => {
const hostname = newServerHostname.trim();
if (!hostname) return;
// Basic validation - check if it's a valid hostname format
if (!/^[a-zA-Z0-9][a-zA-Z0-9-_.]*[a-zA-Z0-9]$/.test(hostname)) {
return;
}
updateConfig((current) => ({
...current,
ngitServers: [...(current.ngitServers ?? config.ngitServers), hostname],
}));
setNewServerHostname('');
};
const handleRemoveServer = (hostname: string) => {
updateConfig((current) => ({
...current,
ngitServers: (current.ngitServers ?? config.ngitServers).filter(s => s !== hostname),
}));
};
return (
<div className="p-6 space-y-6">
{isMobile && (
@@ -202,6 +230,71 @@ export function NostrSettings() {
</div>
</CardContent>
</Card>
{/* Nostr Git Servers */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Server className="h-5 w-5" />
{t('nostrGitServers')}
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* Server List */}
<div className="space-y-2">
{config.ngitServers.map((hostname) => (
<div
key={hostname}
className="flex items-center gap-2 p-2 rounded-md border bg-muted/20"
>
<Server className="h-4 w-4 text-muted-foreground shrink-0" />
<span className="font-mono text-sm flex-1 truncate">
{hostname}
</span>
<Button
variant="ghost"
size="icon"
onClick={() => handleRemoveServer(hostname)}
className="h-8 w-8 p-0 text-muted-foreground hover:text-destructive hover:bg-transparent shrink-0"
disabled={config.ngitServers.length <= 1}
>
<X className="h-4 w-4" />
</Button>
</div>
))}
</div>
{/* Add Server Form */}
<div className="flex gap-2">
<div className="flex-1">
<Label htmlFor="new-server-hostname" className="sr-only">
{t('serverHostname')}
</Label>
<Input
id="new-server-hostname"
placeholder={t('enterHostname')}
value={newServerHostname}
onChange={(e) => setNewServerHostname(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleAddServer();
}
}}
/>
</div>
<Button
onClick={handleAddServer}
disabled={!newServerHostname.trim()}
variant="outline"
size="sm"
className="h-10 shrink-0"
>
<Plus className="h-4 w-4 mr-2" />
{t('addServer')}
</Button>
</div>
</CardContent>
</Card>
</div>
{/* Login Dialog */}