Initial commit

This commit is contained in:
2025-09-14 13:09:39 -07:00
commit 9dcc0868de
20 changed files with 956 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
import React, { useState, useCallback, useRef, useEffect } from 'react';
import { type Service } from '../types';
import { generateServiceDescription } from '../services/geminiService';
import { Globe, Loader2 } from 'lucide-react';
interface AddServiceModalProps {
onClose: () => void;
onAddService: (service: Omit<Service, 'id'>) => void;
}
const AddServiceModal: React.FC<AddServiceModalProps> = ({ onClose, onAddService }) => {
const [name, setName] = useState('');
const [url, setUrl] = useState('');
const [description, setDescription] = useState('');
const [isFetchingDesc, setIsFetchingDesc] = useState(false);
const modalRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(event.target as Node)) {
onClose();
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [onClose]);
const handleFetchDescription = useCallback(async () => {
if (name.trim().length > 2 && process.env.API_KEY) {
setIsFetchingDesc(true);
const desc = await generateServiceDescription(name);
setDescription(desc);
setIsFetchingDesc(false);
}
}, [name]);
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (name && url) {
onAddService({
name,
url,
description: description || `A local service at ${url}`,
icon: Globe,
});
onClose();
}
};
return (
<div className="fixed inset-0 bg-black bg-opacity-70 z-50 flex justify-center items-center p-4">
<div ref={modalRef} className="bg-gray-800 rounded-lg shadow-xl w-full max-w-md p-6 border border-gray-700">
<h2 className="text-2xl font-bold mb-6 text-white">Add a New Service</h2>
<form onSubmit={handleSubmit}>
<div className="space-y-4">
<div>
<label htmlFor="name" className="block text-sm font-medium text-gray-300">
Service Name
</label>
<input
type="text"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
onBlur={handleFetchDescription}
className="mt-1 block w-full bg-gray-900 border border-gray-600 rounded-md shadow-sm py-2 px-3 text-white focus:outline-none focus:ring-teal-500 focus:border-teal-500 sm:text-sm"
placeholder="e.g., Home Assistant"
required
/>
</div>
<div>
<label htmlFor="url" className="block text-sm font-medium text-gray-300">
URL (including http/https)
</label>
<input
type="text"
id="url"
value={url}
onChange={(e) => setUrl(e.target.value)}
className="mt-1 block w-full bg-gray-900 border border-gray-600 rounded-md shadow-sm py-2 px-3 text-white focus:outline-none focus:ring-teal-500 focus:border-teal-500 sm:text-sm"
placeholder="http://192.168.1.100:8123"
required
/>
</div>
<div>
<label htmlFor="description" className="block text-sm font-medium text-gray-300">
Description
</label>
<div className="relative">
<textarea
id="description"
rows={3}
value={description}
onChange={(e) => setDescription(e.target.value)}
className="mt-1 block w-full bg-gray-900 border border-gray-600 rounded-md shadow-sm py-2 px-3 text-white focus:outline-none focus:ring-teal-500 focus:border-teal-500 sm:text-sm"
placeholder="A short description of the service..."
/>
{isFetchingDesc && (
<div className="absolute inset-y-0 right-3 flex items-center">
<Loader2 className="h-5 w-5 text-teal-400 animate-spin" />
</div>
)}
</div>
</div>
</div>
<div className="mt-8 flex justify-end space-x-3">
<button
type="button"
onClick={onClose}
className="px-4 py-2 text-sm font-medium text-gray-300 bg-gray-700 rounded-md hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-gray-500"
>
Cancel
</button>
<button
type="submit"
className="px-4 py-2 text-sm font-medium text-white bg-teal-500 rounded-md shadow-sm hover:bg-teal-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-teal-500"
>
Add Service
</button>
</div>
</form>
</div>
</div>
);
};
export default AddServiceModal;

44
components/Header.tsx Normal file
View File

@@ -0,0 +1,44 @@
import React from 'react';
import { Plus, Network, Scan } from 'lucide-react';
interface HeaderProps {
onAddService: () => void;
onScanNetwork: () => void;
}
const Header: React.FC<HeaderProps> = ({ onAddService, onScanNetwork }) => {
return (
<header className="bg-gray-800/50 backdrop-blur-sm sticky top-0 z-10 shadow-md">
<div className="container mx-auto px-4 sm:px-6 lg:px-8">
<div className="flex items-center justify-between h-20">
<div className="flex items-center">
<Network className="h-8 w-8 text-teal-400" />
<h1 className="ml-3 text-2xl font-bold text-gray-100 tracking-tight">
Local Dashboard
</h1>
</div>
<div className="flex items-center space-x-3">
<button
onClick={onScanNetwork}
type="button"
className="inline-flex items-center px-4 py-2 border border-gray-600 text-sm font-medium rounded-md shadow-sm text-gray-200 bg-gray-700 hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-teal-500 transition-colors"
>
<Scan className="-ml-1 mr-2 h-5 w-5" />
Scan Network
</button>
<button
onClick={onAddService}
type="button"
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-teal-500 hover:bg-teal-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-teal-500 transition-colors"
>
<Plus className="-ml-1 mr-2 h-5 w-5" />
Add Service
</button>
</div>
</div>
</div>
</header>
);
};
export default Header;

View File

@@ -0,0 +1,166 @@
import React, { useRef, useEffect, useState } from 'react';
import { AlertTriangle, Loader2, ScanSearch, ServerCrash } from 'lucide-react';
import { type Service } from '../types';
interface ScanInfoModalProps {
onClose: () => void;
onScanComplete: (services: Omit<Service, 'id'>[]) => void;
}
type ScanStatus = 'idle' | 'scanning' | 'error';
const ScanInfoModal: React.FC<ScanInfoModalProps> = ({ onClose, onScanComplete }) => {
const modalRef = useRef<HTMLDivElement>(null);
const [networkAddress, setNetworkAddress] = useState('192.168.1.0/24');
const [scanStatus, setScanStatus] = useState<ScanStatus>('idle');
const [errorMessage, setErrorMessage] = useState('');
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(event.target as Node)) {
onClose();
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [onClose]);
const handleStartScan = async (e: React.FormEvent) => {
e.preventDefault();
setScanStatus('scanning');
setErrorMessage('');
try {
const response = await fetch('http://localhost:3001/api/scan', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ network: networkAddress }),
});
if (!response.ok) {
// Try to parse a specific error message from the backend, otherwise show a generic one.
const errorData = await response.json().catch(() => ({ error: `Request failed with status: ${response.status}` }));
setErrorMessage(errorData.error || 'An unknown error occurred during the scan.');
setScanStatus('error');
return;
}
const discoveredServices = await response.json();
onScanComplete(discoveredServices);
onClose();
} catch (err) {
// This catch block handles network errors (e.g., server not running)
console.error(err);
setErrorMessage('Scan failed. Is the backend server running? Make sure you have started the server in the `server` directory and that nmap is installed. See README.md for details.');
setScanStatus('error');
}
};
const renderContent = () => {
switch (scanStatus) {
case 'scanning':
return (
<div className="text-center py-8">
<Loader2 className="h-12 w-12 text-teal-400 animate-spin mx-auto" />
<h3 className="mt-4 text-xl font-semibold text-white">Scanning Network</h3>
<p className="text-gray-400 mt-1">
Discovering services on <span className="font-mono text-teal-300">{networkAddress}</span>...
</p>
<p className="text-gray-500 text-sm mt-2">This may take a few minutes depending on your network.</p>
</div>
);
case 'error':
return (
<div>
<div className="flex items-center mb-4">
<div className="p-2 bg-red-500/20 rounded-full mr-4">
<ServerCrash className="h-6 w-6 text-red-400" />
</div>
<h2 className="text-2xl font-bold text-white">Scan Failed</h2>
</div>
<div className="bg-red-900/50 border border-red-700 text-red-200 px-4 py-3 rounded-lg relative" role="alert">
<strong className="font-bold flex items-center"><AlertTriangle className="mr-2"/>Error Details</strong>
<span className="block sm:inline mt-2">{errorMessage}</span>
</div>
<div className="mt-8 flex justify-end space-x-3">
<button
type="button"
onClick={onClose}
className="px-4 py-2 text-sm font-medium text-gray-300 bg-gray-700 rounded-md hover:bg-gray-600"
>
Close
</button>
<button
type="button"
onClick={() => setScanStatus('idle')}
className="px-6 py-2 text-sm font-medium text-white bg-teal-500 rounded-md shadow-sm hover:bg-teal-600"
>
Try Again
</button>
</div>
</div>
);
case 'idle':
default:
return (
<form onSubmit={handleStartScan}>
<div className="flex items-center mb-4">
<div className="p-2 bg-teal-500/20 rounded-full mr-4">
<ScanSearch className="h-6 w-6 text-teal-400" />
</div>
<h2 className="text-2xl font-bold text-white">Scan Your Local Network</h2>
</div>
<div className="text-gray-300 space-y-4">
<p>
Enter your local network address range below to automatically discover web services.
</p>
<div>
<label htmlFor="network-address" className="block text-sm font-medium text-gray-300 mb-1">
Network IP Address (CIDR format)
</label>
<input
type="text"
id="network-address"
value={networkAddress}
onChange={(e) => setNetworkAddress(e.target.value)}
className="mt-1 block w-full bg-gray-900 border border-gray-600 rounded-md shadow-sm py-2 px-3 text-white focus:outline-none focus:ring-teal-500 focus:border-teal-500 sm:text-sm font-mono"
placeholder="e.g., 192.168.1.0/24"
required
/>
</div>
</div>
<div className="mt-8 flex justify-end space-x-3">
<button
type="button"
onClick={onClose}
className="px-4 py-2 text-sm font-medium text-gray-300 bg-gray-700 rounded-md hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-gray-500"
>
Cancel
</button>
<button
type="submit"
className="inline-flex items-center px-6 py-2 text-sm font-medium text-white bg-teal-500 rounded-md shadow-sm hover:bg-teal-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-teal-500"
>
Start Scan
</button>
</div>
</form>
);
}
};
return (
<div className="fixed inset-0 bg-black bg-opacity-70 z-50 flex justify-center items-center p-4">
<div ref={modalRef} className="bg-gray-800 rounded-lg shadow-xl w-full max-w-lg p-6 border border-gray-700 transition-all duration-300">
{renderContent()}
</div>
</div>
);
};
export default ScanInfoModal;

View File

@@ -0,0 +1,50 @@
import React from 'react';
import { type Service } from '../types';
import { Globe, X } from 'lucide-react';
interface ServiceCardProps {
service: Service;
onDelete: (id: string) => void;
}
const ServiceCard: React.FC<ServiceCardProps> = ({ service, onDelete }) => {
const IconComponent = typeof service.icon === 'string' ? Globe : service.icon;
const handleDelete = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
e.stopPropagation();
if (window.confirm(`Are you sure you want to delete ${service.name}?`)) {
onDelete(service.id);
}
};
return (
<a
href={service.url}
target="_blank"
rel="noopener noreferrer"
className="group relative block p-6 bg-gray-800 rounded-lg shadow-lg hover:shadow-2xl hover:bg-gray-700 transition-all duration-300 transform hover:-translate-y-1"
>
<button
onClick={handleDelete}
className="absolute top-2 right-2 p-1.5 bg-gray-700/50 text-gray-400 rounded-full hover:bg-red-500/50 hover:text-white opacity-0 group-hover:opacity-100 transition-opacity focus:opacity-100 z-10"
aria-label={`Delete ${service.name}`}
>
<X className="h-4 w-4" />
</button>
<div className="flex items-center mb-4">
<div className="p-3 bg-gray-900 rounded-lg mr-4">
<IconComponent className="h-6 w-6 text-teal-400" />
</div>
<h3 className="text-lg font-semibold text-gray-100 truncate">{service.name}</h3>
</div>
<p className="text-sm text-gray-400 h-10 overflow-hidden">
{service.description}
</p>
</a>
);
};
export default ServiceCard;