Skip to content

Commit

Permalink
switch from alert to modal
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmatus committed Dec 10, 2024
1 parent 5944698 commit cd03e71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 58 deletions.
6 changes: 2 additions & 4 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ interface Alert {
title: string,
variant: AlertVariant,
detail?: string,
actionLinks?: React.ReactNode,
}

export interface FolderFileInfo extends FileInfo {
Expand Down Expand Up @@ -169,8 +168,8 @@ export const Application = () => {
if (loading)
return <EmptyStatePanel loading />;

const addAlert = (title: string, variant: AlertVariant, key: string, detail?: string, actionLinks?: React.ReactNode) => {
setAlerts(prevAlerts => [...prevAlerts, { title, variant, key, ...detail && { detail }, ...actionLinks && { actionLinks }, }]);
const addAlert = (title: string, variant: AlertVariant, key: string, detail?: string) => {
setAlerts(prevAlerts => [...prevAlerts, { title, variant, key, ...detail && { detail }, }]);
};
const removeAlert = (key: string) => setAlerts(prevAlerts => prevAlerts.filter(alert => alert.key !== key));

Expand All @@ -183,7 +182,6 @@ export const Application = () => {
<Alert
variant={alert.variant}
title={alert.title}
actionLinks={alert.actionLinks}
actionClose={
<AlertActionCloseButton
title={alert.title}
Expand Down
2 changes: 1 addition & 1 deletion src/files-card-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export const FilesCardBody = ({
if (e.ctrlKey && !e.shiftKey && !e.altKey && clipboard.files.length > 0 &&
!(e.target instanceof HTMLInputElement)) {
e.preventDefault();
pasteFromClipboard(clipboard, cwdInfo, path, addAlert);
pasteFromClipboard(clipboard, cwdInfo, path, dialogs, addAlert);
}
break;

Expand Down
72 changes: 19 additions & 53 deletions src/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/

import React, { useContext } from "react";
import React from "react";

import { AlertActionLink, AlertVariant } from "@patternfly/react-core/dist/esm/components/Alert";
import { AlertVariant } from "@patternfly/react-core/dist/esm/components/Alert";

import cockpit from "cockpit";
import type { FileInfo } from "cockpit/fsinfo";
import { useDialogs, type Dialogs } from 'dialogs';
import type { Dialogs } from 'dialogs';

import { FilesContext, type ClipboardInfo, type FolderFileInfo } from "./app";
import type { ClipboardInfo, FolderFileInfo } from "./app";
import { show_copy_paste_as_owner } from "./dialogs/copyPasteOwnership.tsx";
import { show_create_file_dialog } from './dialogs/create-file.tsx';
import { confirm_delete } from './dialogs/delete.tsx';
import { edit_file, MAX_EDITOR_FILE_SIZE } from './dialogs/editor.tsx';
Expand All @@ -45,72 +46,37 @@ type MenuItem = { type: "divider" } | {
className?: string;
};

function isForeignDestination(cwdInfo: FileInfo | null, clipboard: ClipboardInfo) {
if (cwdInfo === null) {
return false;
}

function isForeignDestination(cwdInfo: FileInfo, clipboard: ClipboardInfo) {
return !clipboard.files.every(file => file.user === cwdInfo.user && file.group === cwdInfo.group);
}

const PasteSuperUserAction = ({ clipboard, path } : {
clipboard: ClipboardInfo,
path: string,
}) => {
const { cwdInfo } = useContext(FilesContext);
const dialogs = useDialogs();

if (!cwdInfo || cwdInfo?.entries === undefined)
return;

const pastedFiles: FileInfo[] = [];
for (const [filename, file] of Object.entries(cwdInfo.entries)) {
if (clipboard.files.find(clipFile => clipFile.name === filename)) {
pastedFiles.push(file);
}
}

return (
<>
<AlertActionLink
component="a"
onClick={() => edit_permissions(dialogs, pastedFiles[0], path)}
>
{_("Change permissions")}
</AlertActionLink>
</>
);
}

export async function pasteFromClipboard(
clipboard: ClipboardInfo,
cwdInfo: FileInfo | null,
path: string,
addAlert: (title: string, variant: AlertVariant, key: string, detail?: string, actionLinks?: React.ReactNode) => void,
dialogs: Dialogs,
addAlert: (title: string, variant: AlertVariant, key: string, detail?: string) => void,
) {
const existingFiles = clipboard.files.filter(sourcePath => cwdInfo?.entries?.[sourcePath.name]);


if (existingFiles.length > 0) {
addAlert(_("Pasting failed"), AlertVariant.danger, "paste-error",
cockpit.format(_("\"$0\" exists, not overwriting with paste."),
existingFiles.map(file => file.name).join(", ")));
return;
}

if (isForeignDestination(cwdInfo, clipboard) && cwdInfo !== null) {
addAlert(cockpit.format(_("Files pasted as user $0"), cwdInfo?.user), AlertVariant.warning,
"paste-superuser", undefined, <PasteSuperUserAction clipboard={clipboard} path={path} />);
if (cwdInfo !== null && isForeignDestination(cwdInfo, clipboard)) {
show_copy_paste_as_owner(dialogs, clipboard, path);
} else {
cockpit.spawn([
"cp",
"-R",
...clipboard.files.map(file => clipboard.path + file.name),
path
])
.catch(err => addAlert(err.message, AlertVariant.danger, `${new Date().getTime()}`));
}

const srcPath = clipboard.path;
cockpit.spawn([
"cp",
"-R",
...clipboard.files.map(file => srcPath + file.name),
path
], { superuser: "try" })
.catch(err => addAlert(err.message, AlertVariant.danger, `${new Date().getTime()}`));
}

export function get_menu_items(
Expand All @@ -131,7 +97,7 @@ export function get_menu_items(
id: "paste-item",
title: _("Paste"),
isDisabled: clipboard.files?.length === 0,
onClick: () => pasteFromClipboard(clipboard, cwdInfo, path, addAlert),
onClick: () => pasteFromClipboard(clipboard, cwdInfo, path, dialogs, addAlert),
},
{ type: "divider" },
{
Expand Down

0 comments on commit cd03e71

Please sign in to comment.