Skip to content

Commit 821dc72

Browse files
mudlerlocalai-org-maint-bot
authored andcommitted
feat(ui): add useP2PMode hook and embedded prop to Nodes and P2P pages
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
1 parent 849a4a6 commit 821dc72

3 files changed

Lines changed: 68 additions & 33 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useState, useEffect, useCallback } from 'react'
2+
import { p2pApi } from '../utils/api'
3+
4+
// useP2PMode reports whether p2p / swarm mode is available, mirroring
5+
// useDistributedMode. Availability is "a network token exists" (the same signal
6+
// the standalone P2P page used). One-shot probe on mount plus a manual refetch.
7+
//
8+
// Returns:
9+
// enabled — true when a non-empty network token is present
10+
// loading — true until the first probe completes
11+
// refetch — manual trigger to re-run the probe
12+
export function useP2PMode() {
13+
const [enabled, setEnabled] = useState(false)
14+
const [loading, setLoading] = useState(true)
15+
16+
const probe = useCallback(async () => {
17+
setLoading(true)
18+
try {
19+
const token = await p2pApi.getToken()
20+
setEnabled(!!(token && String(token).trim()))
21+
} catch {
22+
setEnabled(false)
23+
} finally {
24+
setLoading(false)
25+
}
26+
}, [])
27+
28+
useEffect(() => { probe() }, [probe])
29+
30+
return { enabled, loading, refetch: probe }
31+
}

core/http/react-ui/src/pages/Nodes.jsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function WorkerHintCard({ addToast, activeTab, hasWorkers }) {
107107
)
108108
}
109109

110-
export default function Nodes() {
110+
export default function Nodes({ embedded = false }) {
111111
const { addToast } = useOutletContext()
112112
const { t } = useTranslation('admin')
113113
const [nodesList, setNodesList] = useState([])
@@ -309,16 +309,18 @@ export default function Nodes() {
309309
: activeTab === 'agent' ? agentNodes : backendNodes
310310

311311
return (
312-
<div className="page page--wide">
313-
<PageHeader
314-
title={
315-
<>
316-
<i className="fas fa-network-wired" style={{ marginRight: 'var(--spacing-sm)' }} />
317-
{t('nodes.title')}
318-
</>
319-
}
320-
supporting={t('nodes.subtitle')}
321-
/>
312+
<div className={embedded ? '' : 'page page--wide'}>
313+
{!embedded && (
314+
<PageHeader
315+
title={
316+
<>
317+
<i className="fas fa-network-wired" style={{ marginRight: 'var(--spacing-sm)' }} />
318+
{t('nodes.title')}
319+
</>
320+
}
321+
supporting={t('nodes.subtitle')}
322+
/>
323+
)}
322324

323325
<ClusterPulse nodes={nodesList} />
324326
<AttentionCallout nodes={nodesList} onApprove={handleApprove} />

core/http/react-ui/src/pages/P2P.jsx

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function StepNumber({ n, bg, color }) {
103103
)
104104
}
105105

106-
export default function P2P() {
106+
export default function P2P({ embedded = false }) {
107107
const { addToast } = useOutletContext()
108108
const { t } = useTranslation('admin')
109109
const [workers, setWorkers] = useState([])
@@ -173,7 +173,7 @@ export default function P2P() {
173173

174174
if (loading) {
175175
return (
176-
<div className="page page--narrow" style={{ display: 'flex', justifyContent: 'center', padding: 'var(--spacing-xl)' }}>
176+
<div className={embedded ? '' : 'page page--narrow'} style={{ display: 'flex', justifyContent: 'center', padding: 'var(--spacing-xl)' }}>
177177
<LoadingSpinner size="lg" />
178178
</div>
179179
)
@@ -182,7 +182,7 @@ export default function P2P() {
182182
// ── P2P Disabled ──
183183
if (!enabled) {
184184
return (
185-
<div className="page page--narrow">
185+
<div className={embedded ? '' : 'page page--narrow'}>
186186
<div style={{ textAlign: 'center', padding: 'var(--spacing-xl) 0' }}>
187187
<i className="fas fa-network-wired" style={{ fontSize: '3rem', color: 'var(--color-primary)', marginBottom: 'var(--spacing-md)' }} />
188188
<h1 style={{ fontSize: '1.5rem', fontWeight: 600, marginBottom: 'var(--spacing-sm)' }}>
@@ -295,25 +295,27 @@ export default function P2P() {
295295
const mlxTotal = stats.mlx_workers?.total ?? 0
296296

297297
return (
298-
<div className="page page--narrow">
299-
<PageHeader
300-
title={
301-
<>
302-
<i className="fas fa-circle-nodes" style={{ marginRight: 'var(--spacing-sm)' }} />
303-
{t('p2p.title')}
304-
</>
305-
}
306-
supporting={
307-
<>
308-
{t('p2p.subtitle')}
309-
{' '}
310-
<a href="https://localai.io/features/distribute/" target="_blank" rel="noopener noreferrer"
311-
style={{ color: 'var(--color-primary)' }}>
312-
<i className="fas fa-circle-info" />
313-
</a>
314-
</>
315-
}
316-
/>
298+
<div className={embedded ? '' : 'page page--narrow'}>
299+
{!embedded && (
300+
<PageHeader
301+
title={
302+
<>
303+
<i className="fas fa-circle-nodes" style={{ marginRight: 'var(--spacing-sm)' }} />
304+
{t('p2p.title')}
305+
</>
306+
}
307+
supporting={
308+
<>
309+
{t('p2p.subtitle')}
310+
{' '}
311+
<a href="https://localai.io/features/distribute/" target="_blank" rel="noopener noreferrer"
312+
style={{ color: 'var(--color-primary)' }}>
313+
<i className="fas fa-circle-info" />
314+
</a>
315+
</>
316+
}
317+
/>
318+
)}
317319

318320
{/* Network Token */}
319321
<div style={{

0 commit comments

Comments
 (0)