fleet/frontend/components/FileDetails/FileDetails.tsx
Jahziel Villasana-Espinoza ac4ec2ff27
FMA version rollback (#40038)
- **Gitops specify FMA rollback version (#39582)**
- **Fleet UI: Show versions options for FMA installers (#39583)**
- **rollback: DB and core implementation (#39650)**

<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #31919 

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

## Testing

- [x] Added/updated automated tests
- [x] Where appropriate, [automated tests simulate multiple hosts and
test for host
isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing)
(updates to one hosts's records do not affect another)
- [x] QA'd all new/changed functionality manually

---------

Co-authored-by: Jonathan Katz <44128041+jkatz01@users.noreply.github.com>
Co-authored-by: RachelElysia <71795832+RachelElysia@users.noreply.github.com>
Co-authored-by: Carlo DiCelico <carlo@fleetdm.com>
2026-02-24 14:00:32 -05:00

161 lines
4.4 KiB
TypeScript

import React from "react";
import classnames from "classnames";
import { IFileDetails } from "utilities/file/fileUtils";
import Button from "components/buttons/Button";
import { ISupportedGraphicNames } from "components/FileUploader/FileUploader";
import Graphic from "components/Graphic";
import Icon from "components/Icon";
import GitOpsModeTooltipWrapper from "components/GitOpsModeTooltipWrapper";
export type IFileDetailsSupportedGraphicNames =
| ISupportedGraphicNames
| "app-store"; // For VPP apps (non-editable)
interface IFileDetailsProps {
graphicNames:
| IFileDetailsSupportedGraphicNames
| IFileDetailsSupportedGraphicNames[];
fileDetails: IFileDetails;
canEdit: boolean;
/** If present, will default to a custom editor section instead of edit icon */
customEditor?: () => React.ReactNode;
/** If present, will show a trash icon */
onDeleteFile?: () => void;
onFileSelect?: (e: React.ChangeEvent<HTMLInputElement>) => void;
accept?: string;
progress?: number;
/** Set to false for one instance we allow users to edit a file as it shows them the YAML */
gitopsCompatible?: boolean;
gitOpsModeEnabled?: boolean;
}
const baseClass = "file-details";
const FileDetails = ({
graphicNames,
fileDetails,
canEdit,
customEditor,
onDeleteFile,
onFileSelect,
accept,
progress,
gitopsCompatible = true,
gitOpsModeEnabled = false,
}: IFileDetailsProps) => {
const inputRef = React.useRef<HTMLInputElement | null>(null);
const handleClickEdit = (disabled?: boolean) => {
if (disabled) return;
inputRef.current?.click();
};
const infoClasses = classnames(`${baseClass}__info`, {
[`${baseClass}__info--disabled-by-gitops-mode`]:
gitOpsModeEnabled && gitopsCompatible,
});
const renderEditButton = (disabled?: boolean) => {
if (customEditor) {
return (
<div
onClick={(e) => {
e.stopPropagation();
}}
>
{customEditor()}
</div>
);
}
return (
<div className={`${baseClass}__edit`}>
<Button
disabled={disabled}
className={`${baseClass}__edit-button`}
variant="icon"
onClick={() => handleClickEdit(disabled)}
title="Replace file"
>
<Icon name="pencil" color="ui-fleet-black-75" />
</Button>
<input
ref={inputRef}
type="file"
accept={accept}
onChange={onFileSelect}
className="file-input-visually-hidden"
/>
</div>
);
};
return (
<div className={baseClass}>
{/* disabling at this level preserves funcitonality of GitOpsModeTooltipWrapper around the edit icon */}
<div className={infoClasses}>
<Graphic
name={
typeof graphicNames === "string" ? graphicNames : graphicNames[0]
}
/>
<div className={`${baseClass}__content`}>
<div className={`${baseClass}__name`}>{fileDetails.name}</div>
{fileDetails.description && (
<div className={`${baseClass}__description`}>
{fileDetails.description}
</div>
)}
</div>
</div>
{!progress &&
canEdit &&
onFileSelect &&
(gitopsCompatible ? (
<GitOpsModeTooltipWrapper
position="left"
tipOffset={4}
renderChildren={(disableChildren) =>
renderEditButton(disableChildren)
}
/>
) : (
renderEditButton()
))}
{!progress && onDeleteFile && (
<div className={`${baseClass}__delete`}>
<Button
className={`${baseClass}__delete-button`}
variant="icon"
onClick={onDeleteFile}
>
<label htmlFor="delete-file">
<Icon name="trash" color="ui-fleet-black-75" />
</label>
</Button>
</div>
)}
{!!progress && (
<div className={`${baseClass}__progress-wrapper`}>
<div className={`${baseClass}__progress-bar`}>
<div
className={`${baseClass}__progress-bar--uploaded`}
style={{
width: `${progress * 100}%`,
}}
title="upload progress bar"
/>
</div>
<div className={`${baseClass}__progress-text`}>
{Math.round(progress * 100)}%
</div>
</div>
)}
</div>
);
};
export default FileDetails;