| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import '../lib/patternfly/patternfly-6-cockpit.scss'; |
| 6 |
import "./network-anaconda.scss"; |
| 7 |
import cockpit from "cockpit"; |
| 8 |
import 'cockpit-dark-theme'; // once per page |
| 9 |
import React, { useRef } from 'react'; |
| 10 |
import { createRoot } from "react-dom/client"; |
| 11 |
|
| 12 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 13 |
import { ExclamationCircleIcon } from "@patternfly/react-icons"; |
| 14 |
|
| 15 |
import { EmptyStatePanel } from "cockpit-components-empty-state.jsx"; |
| 16 |
import { ModelContext } from './model-context.jsx'; |
| 17 |
import { NetworkInterfacePage } from './network-interface.jsx'; |
| 18 |
import { NetworkPage } from './network-main.jsx'; |
| 19 |
import { UsageMonitor } from './helpers.js'; |
| 20 |
|
| 21 |
import * as service from 'service.js'; |
| 22 |
import { init as initDialogs, NetworkManagerModel } from './interfaces.js'; |
| 23 |
import { superuser } from 'superuser'; |
| 24 |
import { PlotState } from 'plot'; |
| 25 |
|
| 26 |
import { useObject, useEvent, usePageLocation } from "hooks"; |
| 27 |
import { WithDialogs } from "dialogs.jsx"; |
| 28 |
|
| 29 |
const _ = cockpit.gettext; |
| 30 |
|
| 31 |
const App = () => { |
| 32 |
const nmService = useObject(() => service.proxy("NetworkManager"), |
| 33 |
null, |
| 34 |
[]); |
| 35 |
useEvent(nmService, "changed"); |
| 36 |
|
| 37 |
const model = useObject(() => new NetworkManagerModel(), null, []); |
| 38 |
useEvent(model, "changed"); |
| 39 |
|
| 40 |
const nmRunning_ref = useRef(undefined); |
| 41 |
useEvent(model.client, "owner", (event, owner) => { nmRunning_ref.current = owner !== null }); |
| 42 |
|
| 43 |
const { path } = usePageLocation(); |
| 44 |
|
| 45 |
useEvent(superuser, "changed"); |
| 46 |
|
| 47 |
const usage_monitor = useObject(() => new UsageMonitor(), null, []); |
| 48 |
const plot_state_main = useObject(() => new PlotState(), null, []); |
| 49 |
const plot_state_iface = useObject(() => new PlotState(), null, []); |
| 50 |
|
| 51 |
if (model.curtain == 'testing' || model.curtain == 'restoring') { |
| 52 |
return <EmptyStatePanel loading title={model.curtain == 'testing' ? _("Testing connection") : _("Restoring connection")} />; |
| 53 |
} |
| 54 |
|
| 55 |
if (model.ready === undefined) |
| 56 |
return <EmptyStatePanel loading />; |
| 57 |
|
| 58 |
/* Show EmptyStatePanel when nm is not running */ |
| 59 |
if (!nmRunning_ref.current) { |
| 60 |
if (nmService.enabled) { |
| 61 |
return ( |
| 62 |
<div id="networking-nm-crashed"> |
| 63 |
<EmptyStatePanel icon={ ExclamationCircleIcon } |
| 64 |
title={ _("NetworkManager is not running") } |
| 65 |
action={nmService.exists ? _("Start service") : null} |
| 66 |
onAction={ nmService.start } |
| 67 |
secondary={ |
| 68 |
<Button component="a" |
| 69 |
variant="secondary" |
| 70 |
onClick={() => cockpit.jump("/system/services#/NetworkManager.service", cockpit.transport.host)}> |
| 71 |
{_("Troubleshoot…")} |
| 72 |
</Button> |
| 73 |
} /> |
| 74 |
</div> |
| 75 |
); |
| 76 |
} else if (!nmService.exists) { |
| 77 |
return ( |
| 78 |
<div id="networking-nm-not-found"> |
| 79 |
<EmptyStatePanel icon={ ExclamationCircleIcon } |
| 80 |
title={ _("NetworkManager is not installed") } /> |
| 81 |
|
| 82 |
</div> |
| 83 |
); |
| 84 |
} else { |
| 85 |
return ( |
| 86 |
<div id="networking-nm-disabled"> |
| 87 |
<EmptyStatePanel icon={ ExclamationCircleIcon } |
| 88 |
title={ _("Network devices and graphs require NetworkManager") } |
| 89 |
action={nmService.exists ? _("Enable service") : null} |
| 90 |
onAction={() => { |
| 91 |
nmService.enable(); |
| 92 |
nmService.start(); |
| 93 |
}} /> |
| 94 |
|
| 95 |
</div> |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
const interfaces = model.list_interfaces(); |
| 101 |
|
| 102 |
/* At this point NM is running and the model is ready */ |
| 103 |
if (path.length == 0) { |
| 104 |
return ( |
| 105 |
<ModelContext.Provider value={model}> |
| 106 |
<WithDialogs key="1"> |
| 107 |
<NetworkPage privileged={superuser.allowed} |
| 108 |
operationInProgress={model.operationInProgress} |
| 109 |
usage_monitor={usage_monitor} |
| 110 |
plot_state={plot_state_main} |
| 111 |
interfaces={interfaces} /> |
| 112 |
</WithDialogs> |
| 113 |
</ModelContext.Provider> |
| 114 |
); |
| 115 |
} else if (path.length == 1) { |
| 116 |
const iface = interfaces.find(iface => iface.Name == path[0]); |
| 117 |
|
| 118 |
if (iface) { |
| 119 |
return ( |
| 120 |
<ModelContext.Provider value={model}> |
| 121 |
<WithDialogs key="2"> |
| 122 |
<NetworkInterfacePage privileged={superuser.allowed} |
| 123 |
operationInProgress={model.operationInProgress} |
| 124 |
usage_monitor={usage_monitor} |
| 125 |
plot_state={plot_state_iface} |
| 126 |
interfaces={interfaces} |
| 127 |
iface={iface} /> |
| 128 |
</WithDialogs> |
| 129 |
</ModelContext.Provider> |
| 130 |
); |
| 131 |
} else { |
| 132 |
return ( |
| 133 |
<EmptyStatePanel icon={ ExclamationCircleIcon } |
| 134 |
title={cockpit.format(_("Network interface '$0' not found"), path[0])} |
| 135 |
action={_("Return to overview page")} |
| 136 |
actionVariant="link" |
| 137 |
onAction={() => cockpit.location.go("/")} |
| 138 |
/> |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return null; |
| 144 |
}; |
| 145 |
|
| 146 |
function init() { |
| 147 |
initDialogs(); |
| 148 |
const root = createRoot(document.getElementById("network-page")); |
| 149 |
root.render(<App />); |
| 150 |
} |
| 151 |
|
| 152 |
document.addEventListener("DOMContentLoaded", init); |