feat: subnet printer discovery (Feature 3)
- New GET /api/system/printers/scan SSE endpoint: parallel async TCP scan of a /24 subnet, streams found/progress/done events - New GET /api/system/printers/scan-hints: returns auto-detected local subnets from socket.getaddrinfo, excludes loopback/docker ranges - FindPrintersModal in Settings > Print: shows subnet+port inputs, live progress bar, found printers list; selecting a result pre-fills the new printer form (user still names it before saving) - "Εύρεση εκτυπωτών" button added next to "+ Νέος εκτυπωτής" Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import React, { useState, useEffect, useRef, useCallback } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import toast from 'react-hot-toast'
|
||||
import client from '../../../api/client'
|
||||
@@ -774,10 +774,174 @@ function PrinterRow({ printer, onEdit, onDelete, onTest, onToggle, testPending }
|
||||
)
|
||||
}
|
||||
|
||||
function FindPrintersModal({ onClose, onSelect }) {
|
||||
const [subnet, setSubnet] = useState('')
|
||||
const [port, setPort] = useState(9100)
|
||||
const [scanning, setScanning] = useState(false)
|
||||
const [found, setFound] = useState([])
|
||||
const [progress, setProgress] = useState(null) // { done, total }
|
||||
const [error, setError] = useState(null)
|
||||
const esRef = useRef(null)
|
||||
|
||||
// Fetch auto-detected subnets on mount
|
||||
useEffect(() => {
|
||||
client.get('/api/system/printers/scan-hints')
|
||||
.then(r => { if (r.data.subnets?.length) setSubnet(r.data.subnets[0]) })
|
||||
.catch(() => {})
|
||||
}, [])
|
||||
|
||||
const stopScan = useCallback(() => {
|
||||
if (esRef.current) { esRef.current.close(); esRef.current = null }
|
||||
setScanning(false)
|
||||
}, [])
|
||||
|
||||
useEffect(() => () => stopScan(), [stopScan])
|
||||
|
||||
function startScan() {
|
||||
stopScan()
|
||||
setFound([])
|
||||
setProgress(null)
|
||||
setError(null)
|
||||
setScanning(true)
|
||||
|
||||
const token = localStorage.getItem('access_token') || ''
|
||||
const params = new URLSearchParams({ subnet, port })
|
||||
// Use fetch with ReadableStream to consume SSE (EventSource doesn't support auth headers)
|
||||
fetch(`/api/system/printers/scan?${params}`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
}).then(async res => {
|
||||
if (!res.ok) { setError('Σφάλμα σύνδεσης'); setScanning(false); return }
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buf = ''
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
buf += decoder.decode(value, { stream: true })
|
||||
const lines = buf.split('\n')
|
||||
buf = lines.pop()
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith('data: ')) continue
|
||||
try {
|
||||
const msg = JSON.parse(line.slice(6))
|
||||
if (msg.type === 'found') setFound(prev => [...prev, { ip: msg.ip, port: msg.port }])
|
||||
else if (msg.type === 'progress') setProgress({ done: msg.done, total: msg.total })
|
||||
else if (msg.type === 'done') setScanning(false)
|
||||
else if (msg.error) { setError(msg.error); setScanning(false) }
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
setScanning(false)
|
||||
}).catch(e => { setError(String(e)); setScanning(false) })
|
||||
}
|
||||
|
||||
const pct = progress ? Math.round((progress.done / progress.total) * 100) : 0
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
position: 'fixed', inset: 0, background: 'rgba(0,0,0,0.45)', zIndex: 1000,
|
||||
display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 20,
|
||||
}} onClick={e => { if (e.target === e.currentTarget) onClose() }}>
|
||||
<div style={{
|
||||
background: 'white', borderRadius: 14, width: '100%', maxWidth: 520,
|
||||
boxShadow: '0 20px 60px rgba(0,0,0,0.2)', overflow: 'hidden',
|
||||
}}>
|
||||
<div style={{ padding: '20px 24px 16px', borderBottom: '1px solid #f0f1f3' }}>
|
||||
<h2 style={{ fontSize: 17, fontWeight: 700, color: '#111315', margin: 0 }}>Εύρεση εκτυπωτών</h2>
|
||||
<p style={{ fontSize: 12, color: '#9ca3af', margin: '4px 0 0' }}>
|
||||
Σκανάρει το δίκτυο για συσκευές που ακούνε στη θύρα εκτύπωσης.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div style={{ padding: '16px 24px', display: 'flex', gap: 10, alignItems: 'flex-end', flexWrap: 'wrap' }}>
|
||||
<div style={{ flex: '1 1 220px', display: 'flex', flexDirection: 'column', gap: 4 }}>
|
||||
<label style={{ fontSize: 11, fontWeight: 600, color: '#6b7280' }}>
|
||||
ΥΠΟΔΙΚΤΥΟ <span style={{ fontWeight: 400, color: '#b8bdc4' }}>(π.χ. 192.168.1.0/24)</span>
|
||||
</label>
|
||||
<input
|
||||
value={subnet}
|
||||
onChange={e => setSubnet(e.target.value)}
|
||||
placeholder="192.168.1.0/24"
|
||||
style={inputStyle}
|
||||
disabled={scanning}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ flex: '0 0 90px', display: 'flex', flexDirection: 'column', gap: 4 }}>
|
||||
<label style={{ fontSize: 11, fontWeight: 600, color: '#6b7280' }}>PORT</label>
|
||||
<input
|
||||
value={port}
|
||||
onChange={e => setPort(parseInt(e.target.value) || 9100)}
|
||||
type="number"
|
||||
style={inputStyle}
|
||||
disabled={scanning}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
onClick={scanning ? stopScan : startScan}
|
||||
style={{
|
||||
...btnPrimary,
|
||||
background: scanning ? '#dc2626' : '#3758c9',
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
{scanning ? 'Διακοπή' : 'Σκανάρισμα'}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{scanning && (
|
||||
<div style={{ padding: '0 24px 12px' }}>
|
||||
<div style={{ height: 6, background: '#f3f4f6', borderRadius: 99, overflow: 'hidden' }}>
|
||||
<div style={{ height: '100%', background: '#3758c9', width: `${pct}%`, transition: 'width 0.3s' }} />
|
||||
</div>
|
||||
<p style={{ fontSize: 11, color: '#9ca3af', marginTop: 4 }}>
|
||||
{progress ? `${progress.done} / ${progress.total} IPs ελέγχθηκαν` : 'Εκκίνηση…'}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<p style={{ padding: '0 24px 12px', fontSize: 12, color: '#dc2626' }}>{error}</p>
|
||||
)}
|
||||
|
||||
<div style={{ minHeight: 120, maxHeight: 280, overflowY: 'auto', borderTop: '1px solid #f0f1f3' }}>
|
||||
{found.length === 0 && !scanning && (
|
||||
<p style={{ padding: '24px', textAlign: 'center', fontSize: 13, color: '#b8bdc4' }}>
|
||||
{progress ? 'Κανένας εκτυπωτής δεν βρέθηκε.' : 'Πατήστε «Σκανάρισμα» για να ξεκινήσει η αναζήτηση.'}
|
||||
</p>
|
||||
)}
|
||||
{found.map(item => (
|
||||
<div key={item.ip} style={{
|
||||
display: 'flex', alignItems: 'center', justifyContent: 'space-between',
|
||||
padding: '12px 24px', borderBottom: '1px solid #f9fafb',
|
||||
}}>
|
||||
<div>
|
||||
<span style={{ fontSize: 14, fontWeight: 600, color: '#111315' }}>{item.ip}</span>
|
||||
<span style={{ fontSize: 12, color: '#9ca3af', marginLeft: 8 }}>:{item.port}</span>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => { onSelect({ ip_address: item.ip, port: item.port }); onClose() }}
|
||||
style={{ ...btnPrimary, height: 30, padding: '0 14px', fontSize: 12 }}
|
||||
>
|
||||
Χρήση
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div style={{ padding: '14px 24px', borderTop: '1px solid #f0f1f3', display: 'flex', justifyContent: 'flex-end' }}>
|
||||
<button onClick={onClose} style={btnSecondary}>Κλείσιμο</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function PrintersSection() {
|
||||
const qc = useQueryClient()
|
||||
const [showNew, setShowNew] = useState(false)
|
||||
const [editingId, setEditingId] = useState(null)
|
||||
const [showFinder, setShowFinder] = useState(false)
|
||||
const [newFormInitial, setNewFormInitial] = useState(null)
|
||||
|
||||
const { data: printers = [], isLoading } = useQuery({
|
||||
queryKey: ['printers-all'],
|
||||
@@ -810,23 +974,41 @@ function PrintersSection() {
|
||||
updateMut.mutate({ id: printer.id, is_active: !printer.is_active })
|
||||
}
|
||||
|
||||
function handleFinderSelect(prefill) {
|
||||
setNewFormInitial({ ...EMPTY_FORM, ...prefill })
|
||||
setShowNew(true)
|
||||
setEditingId(null)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="card divide-y divide-gray-100">
|
||||
{showFinder && (
|
||||
<FindPrintersModal
|
||||
onClose={() => setShowFinder(false)}
|
||||
onSelect={handleFinderSelect}
|
||||
/>
|
||||
)}
|
||||
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '16px 20px' }}>
|
||||
<div>
|
||||
<h2 className="font-semibold text-gray-700">Εκτυπωτές</h2>
|
||||
<p className="text-xs text-gray-400 mt-0.5">Διαχείριση εκτυπωτών του συστήματος</p>
|
||||
</div>
|
||||
<button onClick={() => { setShowNew(v => !v); setEditingId(null) }} style={btnSecondary}>
|
||||
+ Νέος εκτυπωτής
|
||||
</button>
|
||||
<div style={{ display: 'flex', gap: 8 }}>
|
||||
<button onClick={() => setShowFinder(true)} style={{ ...btnSecondary, fontSize: 13 }}>
|
||||
Εύρεση εκτυπωτών
|
||||
</button>
|
||||
<button onClick={() => { setNewFormInitial(null); setShowNew(v => !v); setEditingId(null) }} style={btnSecondary}>
|
||||
+ Νέος εκτυπωτής
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showNew && (
|
||||
<div style={{ padding: '12px 20px' }}>
|
||||
<PrinterForm
|
||||
initial={newFormInitial ?? undefined}
|
||||
onSave={form => createMut.mutate(form)}
|
||||
onCancel={() => setShowNew(false)}
|
||||
onCancel={() => { setShowNew(false); setNewFormInitial(null) }}
|
||||
isPending={createMut.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user