Debian Codegraph @

pkg/playground/terminal.tsx

raw
1 /*
2 * Copyright (C) 2025 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React, { useState } from "react";
8 import { createRoot, Container } from 'react-dom/client';
9
10 import '../lib/patternfly/patternfly-6-cockpit.scss';
11
12 import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
13 import { ToggleGroup, ToggleGroupItem } from "@patternfly/react-core/dist/esm/components/ToggleGroup/index.js";
14 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
15 import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js";
16 import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js";
17
18 import { useObject } from 'hooks';
19 import { Terminal, TerminalState } from 'cockpit-components-terminal';
20
21 import 'cockpit-dark-theme'; // once per page
22 import 'page.scss';
23
24 function create_channel() {
25 return cockpit.channel({
26 payload: "stream",
27 spawn: ["/bin/bash"],
28 environ: [
29 "TERM=xterm-256color",
30 ],
31 directory: "/",
32 pty: true,
33 });
34 }
35
36 function create_states(n: number): TerminalState[] {
37 const states = [];
38 for (let i = 0; i < n; i++)
39 states.push(new TerminalState(create_channel()));
40 return states;
41 }
42
43 function close_states(states: TerminalState[]) {
44 states.forEach(s => s.close());
45 }
46
47 const TabbedTerminal = () => {
48 const num_terminals = 4;
49 const states = useObject(() => create_states(num_terminals), states => close_states(states), []);
50 const [tab, setTab] = useState<number>(0);
51
52 function make_toggles() {
53 const res = [];
54
55 for (let i = 0; i < num_terminals; i++) {
56 const t = i;
57 res.push(<ToggleGroupItem
58 key={t}
59 text={"Terminal " + String(t + 1)}
60 isSelected={tab == t}
61 onChange={() => setTab(t)}
62 />);
63 }
64
65 return res;
66 }
67
68 return (
69 <PageSection isFilled hasBodyWrapper={false}>
70 <Split>
71 <SplitItem>
72 <ToggleGroup>
73 {make_toggles()}
74 </ToggleGroup>
75 </SplitItem>
76 <SplitItem isFilled />
77 <SplitItem>
78 <Button variant="secondary" onClick={() => states[tab].resetChannel(create_channel())}>
79 Reset
80 </Button>
81 </SplitItem>
82 </Split>
83 <div id="terminal" style={{ height: "100%" }}>
84 <Terminal
85 parentId="terminal"
86 state={states[tab]}
87 cols={80}
88 rows={40}
89 />
90 </div>
91 </PageSection>
92 );
93 };
94
95 const TerminalDemo = () => {
96 const [showTerminal, setShowTerminal] = useState(true);
97
98 return (
99 <Page isContentFilled className="no-masthead-sidebar">
100 <PageSection>
101 <Switch
102 label="Show terminals"
103 isChecked={showTerminal}
104 onChange={(_event: React.FormEvent<HTMLInputElement>, checked: boolean) => setShowTerminal(checked)}
105 />
106 </PageSection>
107 { showTerminal && <TabbedTerminal /> }
108 </Page>
109 );
110 };
111
112 function init_app(rootElement: Container) {
113 const root = createRoot(rootElement);
114 root.render(<TerminalDemo />);
115 }
116
117 document.addEventListener("DOMContentLoaded", function() {
118 cockpit.transport.wait(function() {
119 init_app(document.getElementById('app')!);
120 });
121 });