mirror of
https://github.com/lumina-rocks/lumina.git
synced 2026-06-04 17:51:16 +02:00
feat: implement custom relay management and add relay addition functionality (#73)
Co-authored-by: highperfocused <highperfocused@pm.me>
This commit is contained in:
118
components/AddRelaySheet.tsx
Normal file
118
components/AddRelaySheet.tsx
Normal file
@@ -0,0 +1,118 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useNostr } from "nostr-react";
|
||||
import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { PlusCircle } from "lucide-react";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
|
||||
interface AddRelaySheetProps {
|
||||
onRelayAdded: () => void;
|
||||
}
|
||||
|
||||
export function AddRelaySheet({ onRelayAdded }: AddRelaySheetProps) {
|
||||
const [relayUrl, setRelayUrl] = useState("");
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const { toast } = useToast();
|
||||
const { connectedRelays } = useNostr();
|
||||
|
||||
const handleAddRelay = () => {
|
||||
// Basic validation
|
||||
if (!relayUrl) {
|
||||
toast({
|
||||
title: "Invalid relay URL",
|
||||
description: "Please enter a relay URL",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Format URL
|
||||
let formattedUrl = relayUrl;
|
||||
if (!formattedUrl.startsWith("wss://")) {
|
||||
formattedUrl = `wss://${formattedUrl}`;
|
||||
}
|
||||
|
||||
// Check if relay already exists in connected relays
|
||||
const existingRelays = connectedRelays?.map(relay => relay.url) || [];
|
||||
if (existingRelays.includes(formattedUrl)) {
|
||||
toast({
|
||||
title: "Relay already exists",
|
||||
description: "This relay is already in your list",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Get existing custom relays from localStorage
|
||||
const customRelays = JSON.parse(localStorage.getItem("customRelays") || "[]");
|
||||
|
||||
// Add new relay to the list if not already in custom relays
|
||||
if (!customRelays.includes(formattedUrl)) {
|
||||
customRelays.push(formattedUrl);
|
||||
localStorage.setItem("customRelays", JSON.stringify(customRelays));
|
||||
|
||||
toast({
|
||||
title: "Relay added",
|
||||
description: "The relay has been added to your list. Refresh to connect.",
|
||||
variant: "default",
|
||||
});
|
||||
|
||||
// Reset form and close sheet
|
||||
setRelayUrl("");
|
||||
setIsOpen(false);
|
||||
|
||||
// Call the callback to notify parent component
|
||||
onRelayAdded();
|
||||
} else {
|
||||
toast({
|
||||
title: "Relay already exists",
|
||||
description: "This relay is already in your custom list",
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Sheet open={isOpen} onOpenChange={setIsOpen}>
|
||||
<SheetTrigger asChild>
|
||||
<Button variant="outline" className="gap-2">
|
||||
<PlusCircle className="h-4 w-4" />
|
||||
Add Relay
|
||||
</Button>
|
||||
</SheetTrigger>
|
||||
<SheetContent>
|
||||
<SheetHeader>
|
||||
<SheetTitle>Add New Relay</SheetTitle>
|
||||
<SheetDescription>
|
||||
Enter the URL of a Nostr relay to add to your connection list.
|
||||
</SheetDescription>
|
||||
</SheetHeader>
|
||||
<div className="grid gap-4 py-4">
|
||||
<div className="space-y-2">
|
||||
<Input
|
||||
id="relay-url"
|
||||
placeholder="wss://relay.example.com"
|
||||
value={relayUrl}
|
||||
onChange={(e) => setRelayUrl(e.target.value)}
|
||||
className="w-full"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Relay URLs typically start with wss:// but you can omit it if needed.
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex justify-end space-x-2 pt-4">
|
||||
<Button variant="outline" onClick={() => setIsOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleAddRelay}>
|
||||
Add Relay
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
}
|
||||
85
components/ManageCustomRelays.tsx
Normal file
85
components/ManageCustomRelays.tsx
Normal file
@@ -0,0 +1,85 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useNostr } from "nostr-react";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Trash2 } from "lucide-react";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
|
||||
export function ManageCustomRelays() {
|
||||
const [customRelays, setCustomRelays] = useState<string[]>([]);
|
||||
const { toast } = useToast();
|
||||
const { connectedRelays } = useNostr();
|
||||
|
||||
useEffect(() => {
|
||||
// Load custom relays from localStorage
|
||||
const storedRelays = JSON.parse(localStorage.getItem("customRelays") || "[]");
|
||||
setCustomRelays(storedRelays);
|
||||
}, []);
|
||||
|
||||
const handleRemoveRelay = (relayUrl: string) => {
|
||||
// Filter out the relay to remove
|
||||
const updatedRelays = customRelays.filter(url => url !== relayUrl);
|
||||
|
||||
// Update state and localStorage
|
||||
setCustomRelays(updatedRelays);
|
||||
localStorage.setItem("customRelays", JSON.stringify(updatedRelays));
|
||||
|
||||
toast({
|
||||
title: "Relay removed",
|
||||
description: "The relay has been removed from your list. Refresh to apply changes.",
|
||||
variant: "default",
|
||||
});
|
||||
};
|
||||
|
||||
// Check if a relay is currently connected
|
||||
const isConnected = (relayUrl: string) => {
|
||||
return connectedRelays?.some(relay => relay.url === relayUrl && relay.status === 1) || false;
|
||||
};
|
||||
|
||||
if (customRelays.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mt-6">
|
||||
<CardHeader>
|
||||
<CardTitle>Custom Relays</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{customRelays.map((relayUrl) => (
|
||||
<div key={relayUrl} className="flex items-center justify-between p-3 rounded-md border bg-card hover:bg-accent/50 transition-colors">
|
||||
<div className="flex items-center space-x-3 overflow-hidden">
|
||||
<div>
|
||||
<p className="font-medium truncate">{relayUrl}</p>
|
||||
<div className="mt-1">
|
||||
{isConnected(relayUrl) ? (
|
||||
<Badge className="bg-green-500">Connected</Badge>
|
||||
) : (
|
||||
<Badge variant="outline">Not Connected</Badge>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => handleRemoveRelay(relayUrl)}
|
||||
className="text-destructive hover:text-destructive hover:bg-destructive/10"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
<span className="sr-only">Remove</span>
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
<p className="text-xs text-muted-foreground mt-2">
|
||||
Note: Refresh the page after adding or removing relays to apply changes to connections.
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
140
components/ui/sheet.tsx
Normal file
140
components/ui/sheet.tsx
Normal file
@@ -0,0 +1,140 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as SheetPrimitive from "@radix-ui/react-dialog"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
import { X } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const Sheet = SheetPrimitive.Root
|
||||
|
||||
const SheetTrigger = SheetPrimitive.Trigger
|
||||
|
||||
const SheetClose = SheetPrimitive.Close
|
||||
|
||||
const SheetPortal = SheetPrimitive.Portal
|
||||
|
||||
const SheetOverlay = React.forwardRef<
|
||||
React.ElementRef<typeof SheetPrimitive.Overlay>,
|
||||
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<SheetPrimitive.Overlay
|
||||
className={cn(
|
||||
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
ref={ref}
|
||||
/>
|
||||
))
|
||||
SheetOverlay.displayName = SheetPrimitive.Overlay.displayName
|
||||
|
||||
const sheetVariants = cva(
|
||||
"fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500",
|
||||
{
|
||||
variants: {
|
||||
side: {
|
||||
top: "inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top",
|
||||
bottom:
|
||||
"inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",
|
||||
left: "inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm",
|
||||
right:
|
||||
"inset-y-0 right-0 h-full w-3/4 border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
side: "right",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
interface SheetContentProps
|
||||
extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
|
||||
VariantProps<typeof sheetVariants> {}
|
||||
|
||||
const SheetContent = React.forwardRef<
|
||||
React.ElementRef<typeof SheetPrimitive.Content>,
|
||||
SheetContentProps
|
||||
>(({ side = "right", className, children, ...props }, ref) => (
|
||||
<SheetPortal>
|
||||
<SheetOverlay />
|
||||
<SheetPrimitive.Content
|
||||
ref={ref}
|
||||
className={cn(sheetVariants({ side }), className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<SheetPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-secondary">
|
||||
<X className="h-4 w-4" />
|
||||
<span className="sr-only">Close</span>
|
||||
</SheetPrimitive.Close>
|
||||
</SheetPrimitive.Content>
|
||||
</SheetPortal>
|
||||
))
|
||||
SheetContent.displayName = SheetPrimitive.Content.displayName
|
||||
|
||||
const SheetHeader = ({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) => (
|
||||
<div
|
||||
className={cn(
|
||||
"flex flex-col space-y-2 text-center sm:text-left",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
SheetHeader.displayName = "SheetHeader"
|
||||
|
||||
const SheetFooter = ({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) => (
|
||||
<div
|
||||
className={cn(
|
||||
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
SheetFooter.displayName = "SheetFooter"
|
||||
|
||||
const SheetTitle = React.forwardRef<
|
||||
React.ElementRef<typeof SheetPrimitive.Title>,
|
||||
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Title>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<SheetPrimitive.Title
|
||||
ref={ref}
|
||||
className={cn("text-lg font-semibold text-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
SheetTitle.displayName = SheetPrimitive.Title.displayName
|
||||
|
||||
const SheetDescription = React.forwardRef<
|
||||
React.ElementRef<typeof SheetPrimitive.Description>,
|
||||
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Description>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<SheetPrimitive.Description
|
||||
ref={ref}
|
||||
className={cn("text-sm text-muted-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
SheetDescription.displayName = SheetPrimitive.Description.displayName
|
||||
|
||||
export {
|
||||
Sheet,
|
||||
SheetPortal,
|
||||
SheetOverlay,
|
||||
SheetTrigger,
|
||||
SheetClose,
|
||||
SheetContent,
|
||||
SheetHeader,
|
||||
SheetFooter,
|
||||
SheetTitle,
|
||||
SheetDescription,
|
||||
}
|
||||
Reference in New Issue
Block a user