From 929f222930ef676b96c9356121ddf9dc7ffed19e Mon Sep 17 00:00:00 2001 From: Allison Karlitskaya Date: Wed, 31 Jan 2024 22:36:38 +0100 Subject: [PATCH] Add hostname to the breadcrumb --- src/cockpit.d.ts | 27 ++++++++++++++++++++ src/navigator-breadcrumbs.tsx | 47 ++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/cockpit.d.ts b/src/cockpit.d.ts index d7fc46f9c..099bf0592 100644 --- a/src/cockpit.d.ts +++ b/src/cockpit.d.ts @@ -104,6 +104,33 @@ declare module 'cockpit' { export const location: Location; + /* === cockpit.dbus ========================== */ + + interface DBusProxyEvents extends EventMap { + changed(changes: { [property: string]: unknown }): void; + } + + interface DBusProxy extends EventSource { + valid: boolean; + [property: string]: unknown; + } + + interface DBusOptions { + bus?: string; + address?: string; + superuser?: "require" | "try"; + track?: boolean; + } + + interface DBusClient { + readonly unique_name: string; + readonly options: DBusOptions; + proxy(interface: string, path: string, options?: { watch?: boolean }): DBusProxy; + close(): void; + } + + function dbus(name: string | null, options?: DBusOptions): DBusClient; + /* === cockpit.file ========================== */ interface FileSyntaxObject { diff --git a/src/navigator-breadcrumbs.tsx b/src/navigator-breadcrumbs.tsx index e3695af77..4ffcfcd97 100644 --- a/src/navigator-breadcrumbs.tsx +++ b/src/navigator-breadcrumbs.tsx @@ -18,28 +18,57 @@ */ import cockpit from "cockpit"; import React from "react"; +import { HddIcon } from "@patternfly/react-icons"; import { Button, Flex, PageBreadcrumb } from "@patternfly/react-core"; +function useHostname() { + const [hostname, setHostname] = React.useState(null); + + React.useEffect(() => { + const client = cockpit.dbus('org.freedesktop.hostname1'); + const hostname1 = client.proxy('org.freedesktop.hostname1', '/org/freedesktop/hostname1'); + + function changed() { + if (hostname1.valid && typeof hostname1.Hostname === 'string') { + setHostname(hostname1.Hostname); + } + } + + hostname1.addEventListener("changed", changed); + return () => { + hostname1.removeEventListener("changed", changed); + client.close(); + }; + }, []); + + return hostname; +} + export function NavigatorBreadcrumbs({ path }: { path: string[] }) { + const hostname = useHostname(); + function navigate(n_parts: number) { cockpit.location.go("/", { path: encodeURIComponent(path.slice(0, n_parts).join("/")) }); } + const fullPath = path.slice(1); + fullPath.unshift(hostname || "server"); + return ( - {path.map((dir, i) => { + {fullPath.map((dir, i) => { return ( - {i !== path.length - 1 && - } - {i === path.length - 1 &&

{dir || "/"}

} + {dir !== "" &&

/

}
);