Skip to content

Commit

Permalink
lib: Port cockpit-components-file-autocomplete.jsx to fsinfo
Browse files Browse the repository at this point in the history
This was the last user of the old fslist1 channel in all known Cockpit
projects.

Part of #19999
  • Loading branch information
martinpitt committed Oct 1, 2024
1 parent da34590 commit 03fcd56
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
45 changes: 17 additions & 28 deletions pkg/lib/cockpit-components-file-autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { Select, SelectOption } from "@patternfly/react-core/dist/esm/deprecated
import PropTypes from "prop-types";
import { debounce } from 'throttle-debounce';

import { fsinfo } from "cockpit/fsinfo";

const _ = cockpit.gettext;

export class FileAutoComplete extends React.Component {
Expand Down Expand Up @@ -58,7 +60,7 @@ export class FileAutoComplete extends React.Component {
path = value.slice(0, value.length - 1);

const match = this.state.displayFiles
.find(entry => (entry.type == 'directory' && entry.path == path + '/') || (entry.type == 'file' && entry.path == path));
.find(entry => (entry.type == 'dir' && entry.path == path + '/') || (entry.type == 'reg' && entry.path == path));

if (match) {
// If match file path is a prefix of another file, do not update current directory,
Expand All @@ -67,7 +69,7 @@ export class FileAutoComplete extends React.Component {
const isPrefix = this.state.displayFiles.filter(entry => entry.path.startsWith(value)).length > 1;
// If the inserted string corresponds to a directory listed in the results
// update the current directory and refetch results
if (match.type == 'directory' && !isPrefix)
if (match.type == 'dir' && !isPrefix)
cb(match.path);
else
this.setState({ value: match.path });
Expand All @@ -91,38 +93,25 @@ export class FileAutoComplete extends React.Component {

onCreateOption(newValue) {
this.setState(prevState => ({
displayFiles: [...prevState.displayFiles, { type: "file", path: newValue }]
displayFiles: [...prevState.displayFiles, { type: "reg", path: newValue }]
}));
}

updateFiles(path) {
if (this.state.directory == path)
return;

const channel = cockpit.channel({
payload: "fslist1",
path,
superuser: this.props.superuser,
watch: false,
});
const results = [];

channel.addEventListener("ready", () => {
this.finishUpdate(results, null, path);
});

channel.addEventListener("close", (ev, data) => {
this.finishUpdate(results, data.message, path);
});

channel.addEventListener("message", (ev, data) => {
const item = JSON.parse(data);
if (item && item.path && item.event == 'present' &&
(!this.props.onlyDirectories || item.type == 'directory')) {
item.path = item.path + (item.type == 'directory' ? '/' : '');
results.push(item);
}
});
fsinfo(path, ['type', 'entries'], { superuser: this.props.superuser })
.then(info => {
const results = [];
for (const name in info.entries ?? {}) {
const type = info.entries[name].type;
if (!this.props.onlyDirectories || type == 'dir')
results.push({ type, path: name + (type == 'dir' ? '/' : '') });
}
this.finishUpdate(results, null, path);
})
.catch(error => this.finishUpdate([], error.message, path));
}

finishUpdate(results, error, directory) {
Expand All @@ -137,7 +126,7 @@ export class FileAutoComplete extends React.Component {

if (directory) {
listItems.unshift({
type: "directory",
type: "dir",
path: directory
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/verify/check-shell-keys
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ session optional pam_ssh_add.so
b.wait_in_text("#credentials-modal .pf-v5-c-select__menu-item.pf-m-disabled", "No such file or directory")
b.focus("#credentials-modal .pf-v5-c-select__toggle-typeahead input")
b.key("Backspace", 5)
b.wait_visible("#credentials-modal .pf-v5-c-select__menu-item.directory:contains('/var/lib/')")
b.wait_visible("#credentials-modal .pf-v5-c-select__menu-item.dir:contains('/var/lib/')")
b.click("#credentials-modal .pf-v5-c-select__toggle-clear")
b.wait_val("#credentials-modal .pf-v5-c-select__toggle-typeahead input", "")

Expand Down

0 comments on commit 03fcd56

Please sign in to comment.