added search response into searchmodal ui, restructured folders
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { useEffect, useRef, useState, type SyntheticEvent } from 'react'
|
||||
import { login } from '../../api/connections'
|
||||
import type { LoginRequest } from '../../types/connection'
|
||||
import '../ui/Modal.scss'
|
||||
|
||||
interface Props {
|
||||
serviceType: string
|
||||
label: string
|
||||
onClose: () => void
|
||||
onSuccess: () => void
|
||||
}
|
||||
|
||||
export function ConnectModal({ serviceType, label, onClose, onSuccess }: Readonly<Props>) {
|
||||
const [appUrl, setAppUrl] = useState('')
|
||||
const [username, setUsername] = useState('')
|
||||
const [password, setPassword] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const firstInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const dialogRef = useRef<HTMLDialogElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const dialog = dialogRef.current
|
||||
if (!dialog) return
|
||||
|
||||
dialog.showModal()
|
||||
firstInputRef.current?.focus()
|
||||
|
||||
const handleCancel = (e: Event) => {
|
||||
e.preventDefault()
|
||||
onClose()
|
||||
}
|
||||
dialog.addEventListener('cancel', handleCancel)
|
||||
return () => dialog.removeEventListener('cancel', handleCancel)
|
||||
}, [onClose])
|
||||
|
||||
const handleSubmit = async (e: SyntheticEvent<HTMLFormElement>) => {
|
||||
e.preventDefault()
|
||||
setError(null)
|
||||
setLoading(true)
|
||||
try {
|
||||
const req: LoginRequest = { appUrl, serviceType, username, password, stayLoggedIn: true }
|
||||
await login(req)
|
||||
onSuccess()
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Login failed')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<dialog className="modal" ref={dialogRef}>
|
||||
<div className="modal__header">
|
||||
<h2 className="modal__title" id="modal-title">Connect to {label}</h2>
|
||||
<button className="modal__close" onClick={onClose} aria-label="Close">×</button>
|
||||
</div>
|
||||
<form className="modal__form" onSubmit={handleSubmit}>
|
||||
<div className="modal__field">
|
||||
<label className="modal__label" htmlFor="appUrl">App URL</label>
|
||||
<input id="appUrl" ref={firstInputRef} className="modal__input" type="url"
|
||||
placeholder="https://homebox.example.com"
|
||||
value={appUrl} onChange={e => setAppUrl(e.target.value)} required />
|
||||
</div>
|
||||
<div className="modal__field">
|
||||
<label className="modal__label" htmlFor="username">Username</label>
|
||||
<input id="username" className="modal__input" type="text"
|
||||
autoComplete="username"
|
||||
value={username} onChange={e => setUsername(e.target.value)} required />
|
||||
</div>
|
||||
<div className="modal__field">
|
||||
<label className="modal__label" htmlFor="password">Password</label>
|
||||
<input id="password" className="modal__input" type="password"
|
||||
autoComplete="current-password"
|
||||
value={password} onChange={e => setPassword(e.target.value)} required />
|
||||
</div>
|
||||
{error && <p className="modal__error">{error}</p>}
|
||||
<button className="modal__submit" type="submit" disabled={loading}>
|
||||
{loading ? 'Connecting…' : 'Connect'}
|
||||
</button>
|
||||
</form>
|
||||
</dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
.dashboard {
|
||||
padding: 40px 40px 60px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
flex: 1;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
padding: 24px 20px 40px;
|
||||
}
|
||||
|
||||
&__header {
|
||||
margin-bottom: 32px;
|
||||
}
|
||||
|
||||
&__title {
|
||||
font-size: 32px;
|
||||
letter-spacing: -0.5px;
|
||||
margin: 0 0 4px;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
font-size: 24px;
|
||||
}
|
||||
}
|
||||
|
||||
&__section-label {
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.08em;
|
||||
text-transform: uppercase;
|
||||
color: var(--text);
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
|
||||
&__cards {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import { useState, useEffect } from "react"
|
||||
import { getStatuses, logout } from "../../api/connections"
|
||||
import type { ConnectionStatus } from "../../types/connection"
|
||||
import { ServiceCard } from "./ServiceCard"
|
||||
import { ConnectModal } from "./ConnectModal"
|
||||
import "./Dashboard.scss"
|
||||
import { SearchModal } from "../search/SearchModal"
|
||||
|
||||
const SERVICES = [
|
||||
{ serviceType: 'HOMEBOX', label: 'Homebox', icon: '📦' },
|
||||
] as const
|
||||
|
||||
export function Dashboard() {
|
||||
const [statuses, setStatuses] = useState<ConnectionStatus[]>([])
|
||||
const [openConnectionModal, setOpenConnectionModal] = useState<string | null>(null)
|
||||
const [openSearchModal, setOpenSearchModal] = useState<string | null>(null)
|
||||
|
||||
const refresh = () => {
|
||||
getStatuses().then(setStatuses).catch(() => { })
|
||||
}
|
||||
|
||||
useEffect(() => { refresh() }, [])
|
||||
|
||||
const handleDisconnect = (serviceType: string) => {
|
||||
logout(serviceType).then(refresh).catch(() => { })
|
||||
}
|
||||
|
||||
const activeConnectionModal = SERVICES.find(s => s.serviceType === openConnectionModal)
|
||||
const activeSearchModal = SERVICES.find(s => s.serviceType === openSearchModal)
|
||||
const activeSearchStatus = statuses.find(s => s.serviceType === openSearchModal)
|
||||
|
||||
return (
|
||||
<div className="dashboard">
|
||||
<div className="dashboard__header">
|
||||
<h1 className="dashboard__title">Vaessl Dashboard</h1>
|
||||
</div>
|
||||
|
||||
<p className="dashboard__section-label">Services</p>
|
||||
<div className="dashboard__cards">
|
||||
{SERVICES.map(({ serviceType, label, icon }) => (
|
||||
<ServiceCard
|
||||
key={serviceType}
|
||||
serviceType={serviceType}
|
||||
label={label}
|
||||
icon={icon}
|
||||
status={statuses.find(s => s.serviceType === serviceType) ?? null}
|
||||
onConnect={() => setOpenConnectionModal(serviceType)}
|
||||
onDisconnect={() => handleDisconnect(serviceType)}
|
||||
onSearch={() => setOpenSearchModal(serviceType)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{activeConnectionModal && (
|
||||
<ConnectModal
|
||||
serviceType={activeConnectionModal.serviceType}
|
||||
label={activeConnectionModal.label}
|
||||
onClose={() => setOpenConnectionModal(null)}
|
||||
onSuccess={() => { setOpenConnectionModal(null); refresh() }}
|
||||
/>
|
||||
)}
|
||||
|
||||
{activeSearchModal && (
|
||||
<SearchModal
|
||||
serviceType={activeSearchModal.serviceType}
|
||||
label={activeSearchModal.label}
|
||||
appUrl={activeSearchStatus?.appUrl ?? ''}
|
||||
username={activeSearchStatus?.username ?? ''}
|
||||
onClose={() => setOpenSearchModal(null) }
|
||||
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
.service-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 20px 24px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 10px;
|
||||
background: var(--bg);
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
|
||||
|
||||
&__info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
&__icon {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 8px;
|
||||
background: var(--accent-bg);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
&__name {
|
||||
font-family: var(--heading);
|
||||
font-size: 17px;
|
||||
font-weight: 500;
|
||||
color: var(--text-h);
|
||||
margin: 0 0 10px;
|
||||
text-align: left;
|
||||
|
||||
}
|
||||
|
||||
&__meta {
|
||||
font-size: 13px;
|
||||
color: var(--text);
|
||||
margin: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
&__badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
padding: 2px 8px;
|
||||
border-radius: 999px;
|
||||
|
||||
&::before {
|
||||
content: '';
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
&--connected {
|
||||
color: #16a34a;
|
||||
|
||||
&::before {
|
||||
background: #16a34a;
|
||||
}
|
||||
}
|
||||
|
||||
&--disconnected {
|
||||
color: var(--text);
|
||||
|
||||
&::before {
|
||||
background: var(--border);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&__actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import type { ConnectionStatus } from '../../types/connection'
|
||||
import { ActionButton } from '../ui/ActionButton'
|
||||
import './ServiceCard.scss'
|
||||
|
||||
interface Props {
|
||||
serviceType: string
|
||||
label: string
|
||||
icon: string
|
||||
status: ConnectionStatus | null
|
||||
onConnect: () => void
|
||||
onDisconnect: () => void
|
||||
onSearch: () => void
|
||||
}
|
||||
|
||||
export function ServiceCard({ serviceType: _serviceType, label, icon, status, onConnect, onDisconnect, onSearch }: Readonly<Props>) {
|
||||
const connected = status?.connected ?? false
|
||||
|
||||
const formatExpiry = (iso: string | null) => {
|
||||
if (!iso) return null
|
||||
const d = new Date(iso)
|
||||
return d.toLocaleDateString(undefined, { dateStyle: 'medium' })
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="service-card">
|
||||
<div className="service-card__info">
|
||||
<div className="service-card__icon">{icon}</div>
|
||||
<div>
|
||||
<p className="service-card__name">{label}</p>
|
||||
<p className="service-card__meta">
|
||||
<span className={`service-card__badge service-card__badge--${connected ? 'connected' : 'disconnected'}`}>
|
||||
{connected ? 'Connected' : 'Not connected'}
|
||||
</span>
|
||||
{connected && status?.username && <span>{status.username}</span>}
|
||||
{connected && status?.expiresAt && (
|
||||
<span>· expires {formatExpiry(status.expiresAt)}</span>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="service-card__actions">
|
||||
<ActionButton variant={connected ? 'disconnect' : 'connect'} onClick={connected ? onDisconnect : onConnect}>
|
||||
{connected ? 'Disconnect' : 'Connect'}
|
||||
</ActionButton>
|
||||
{connected && (<ActionButton variant='search' onClick={onSearch}>Search</ActionButton>)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user