Skip to content

Commit 000dfc6

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 6109655 commit 000dfc6

3 files changed

Lines changed: 62 additions & 27 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
@@ -89,7 +89,7 @@ function WorkerHintCard({ addToast, activeTab, hasWorkers }) {
8989
)
9090
}
9191

92-
export default function Nodes() {
92+
export default function Nodes({ embedded = false }) {
9393
const { addToast } = useOutletContext()
9494
const { t } = useTranslation('admin')
9595
const [nodesList, setNodesList] = useState([])
@@ -282,16 +282,18 @@ export default function Nodes() {
282282
: activeTab === 'agent' ? agentNodes : backendNodes
283283

284284
return (
285-
<div className="page page--wide">
286-
<PageHeader
287-
title={
288-
<>
289-
<i className="fas fa-network-wired icon-before" />
290-
{t('nodes.title')}
291-
</>
292-
}
293-
supporting={t('nodes.subtitle')}
294-
/>
285+
<div className={embedded ? '' : 'page page--wide'}>
286+
{!embedded && (
287+
<PageHeader
288+
title={
289+
<>
290+
<i className="fas fa-network-wired icon-before" />
291+
{t('nodes.title')}
292+
</>
293+
}
294+
supporting={t('nodes.subtitle')}
295+
/>
296+
)}
295297

296298
<ClusterPulse nodes={nodesList} />
297299
<AttentionCallout nodes={nodesList} onApprove={handleApprove} />

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function NoNodes({ icon, title, hint }) {
6969
)
7070
}
7171

72-
export default function P2P() {
72+
export default function P2P({ embedded = false }) {
7373
const { addToast } = useOutletContext()
7474
const { t } = useTranslation('admin')
7575
const [workers, setWorkers] = useState([])
@@ -145,7 +145,7 @@ export default function P2P() {
145145

146146
if (loading) {
147147
return (
148-
<div className="page page--narrow loading-center">
148+
<div className={embedded ? 'loading-center' : 'page page--narrow loading-center'}>
149149
<LoadingSpinner size="lg" />
150150
</div>
151151
)
@@ -154,7 +154,7 @@ export default function P2P() {
154154
// ── P2P Disabled ──
155155
if (!enabled) {
156156
return (
157-
<div className="page page--narrow">
157+
<div className={embedded ? '' : 'page page--narrow'}>
158158
<div className="p2p-hero">
159159
<i className="fas fa-network-wired" />
160160
<h1>P2P distribution not enabled</h1>
@@ -243,19 +243,21 @@ export default function P2P() {
243243
const mlxTotal = stats.mlx_workers?.total ?? 0
244244

245245
return (
246-
<div className="page page--narrow">
247-
<PageHeader
248-
title={<><i className="fas fa-circle-nodes fa-circle-info" /> {t('p2p.title')}</>}
249-
supporting={
250-
<>
251-
{t('p2p.subtitle')}
252-
{' '}
253-
<a href="https://localai.io/features/distribute/" target="_blank" rel="noopener noreferrer">
254-
<i />
255-
</a>
256-
</>
257-
}
258-
/>
246+
<div className={embedded ? '' : 'page page--narrow'}>
247+
{!embedded && (
248+
<PageHeader
249+
title={<><i className="fas fa-circle-nodes fa-circle-info" /> {t('p2p.title')}</>}
250+
supporting={
251+
<>
252+
{t('p2p.subtitle')}
253+
{' '}
254+
<a href="https://localai.io/features/distribute/" target="_blank" rel="noopener noreferrer">
255+
<i />
256+
</a>
257+
</>
258+
}
259+
/>
260+
)}
259261

260262
<section className="p2p-token">
261263
<div className="hstack hstack--nowrap tone-warning">

0 commit comments

Comments
 (0)