Skip to content

Commit

Permalink
chore(pf5): migrate shared components
Browse files Browse the repository at this point in the history
  • Loading branch information
tthvo committed Nov 5, 2023
1 parent 7f46aad commit d7c4dd8
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/app/Agent/AgentProbeTemplates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ export const AgentProbeTemplateUploadModal: React.FC<AgentProbeTemplateUploadMod
submitRef={submitRef}
abortRef={abortRef}
uploading={uploading}
dropZoneAccepts={{
'application/xml': ['.xml'],
}}
displayAccepts={['XML']}
onFileSubmit={onFileSubmit}
onFilesChange={onFilesChange}
Expand Down
3 changes: 3 additions & 0 deletions src/app/Archives/ArchiveUploadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ export const ArchiveUploadModal: React.FC<ArchiveUploadModalProps> = ({ onClose,
submitRef={submitRef}
abortRef={abortRef}
uploading={uploading}
dropZoneAccepts={{
'application/octet-stream': ['.jfr'],
}}
displayAccepts={['JFR']}
onFileSubmit={onFileSubmit}
onFilesChange={onFilesChange}
Expand Down
4 changes: 3 additions & 1 deletion src/app/Dashboard/LayoutTemplateUploadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ export const LayoutTemplateUploadModal: React.FC<LayoutTemplateUploadModalProps>
submitRef={submitRef}
abortRef={abortRef}
uploading={uploading}
dropZoneAccepts={['application/json']}
dropZoneAccepts={{
'application/json': ['.json'],
}}
displayAccepts={['JSON']}
onFileSubmit={onFileSubmit}
onFilesChange={onFilesChange}
Expand Down
3 changes: 3 additions & 0 deletions src/app/Events/EventTemplates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ export const EventTemplatesUploadModal: React.FC<EventTemplatesUploadModalProps>
submitRef={submitRef}
abortRef={abortRef}
uploading={uploading}
dropZoneAccepts={{
'application/xml': ['.xml', '.jfc'],
}}
displayAccepts={['XML', 'JFC']}
onFileSubmit={onFileSubmit}
onFilesChange={onFilesChange}
Expand Down
4 changes: 3 additions & 1 deletion src/app/Rules/RulesUploadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ export const RuleUploadModal: React.FC<RuleUploadModalProps> = ({ onClose, ...pr
submitRef={submitRef}
abortRef={abortRef}
uploading={uploading}
dropZoneAccepts={['application/json']}
dropZoneAccepts={{
'application/json': ['.json'],
}}
displayAccepts={['JSON']}
onFileSubmit={onFileSubmit}
onFilesChange={onFilesChange}
Expand Down
5 changes: 4 additions & 1 deletion src/app/SecurityPanel/CertificateUploadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export const CertificateUploadModal: React.FC<CertificateUploadModalProps> = ({
submitRef={submitRef}
abortRef={abortRef}
uploading={uploading}
dropZoneAccepts={['application/x-x509-ca-cert', 'application/pkix-cert']}
dropZoneAccepts={{
'application/x-x509-ca-cert': ['.der'],
'application/pkix-cert': ['.cer'],
}}
displayAccepts={['CER', 'DER']}
onFileSubmit={onFileSubmit}
onFilesChange={onFilesChange}
Expand Down
31 changes: 10 additions & 21 deletions src/app/Shared/Components/FileUploads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { InProgressIcon, UploadIcon } from '@patternfly/react-icons';
import * as React from 'react';
import { Subject } from 'rxjs';
import { DropzoneAccept, FileRejection } from './types';

export type ProgressVariant = 'success' | 'danger' | 'warning';

Expand All @@ -50,7 +51,7 @@ export interface MultiFileUploadProps {
abortRef?: React.RefObject<HTMLDivElement>;
uploading: boolean;
displayAccepts: string[];
dropZoneAccepts?: string[]; // Infer from displayAccepts, if not specified
dropZoneAccepts: DropzoneAccept;
onFilesChange?: (files: FUpload[]) => void;
onFileSubmit: (fileUploads: FUpload[], uploadCallbacks: UploadCallbacks) => void;
titleIcon?: React.ReactNode;
Expand All @@ -76,15 +77,8 @@ export const MultiFileUpload: React.FC<MultiFileUploadProps> = ({
const [fileUploads, setFileUploads] = React.useState<FUpload[]>([]);
const [showCancelPrompt, setShowCancelPrompt] = React.useState(false);

const dzAccept = React.useMemo(() => {
if (dropZoneAccepts && dropZoneAccepts.length) {
return dropZoneAccepts.join(',');
}
return displayAccepts.map((t) => `.${t.toLocaleLowerCase()}`).join(',');
}, [dropZoneAccepts, displayAccepts]);

const handleFileDrop = React.useCallback(
(droppedFiles: File[]) => {
(_, droppedFiles: File[]) => {
setFileUploads((old) => {
// Check for re-uploads
const currentFilenames = old.map((fileUpload) => fileUpload.file.name);
Expand All @@ -108,19 +102,14 @@ export const MultiFileUpload: React.FC<MultiFileUploadProps> = ({
);

const handleFileReject = React.useCallback(
(rejectedFiles: File[]) => {
rejectedFiles.forEach((f) => {
if (!dzAccept.includes(f.type) || f.type === '') {
const message = `Expected file format: ${dzAccept}, but received ${
f.type === '' ? 'unknown type' : f.type
} for ${f.name}`;
notifications.warning(`Incompatible file format`, message);
} else {
notifications.warning(`Failed to load file`, f.name);
}
(fileRejections: FileRejection[]) => {
fileRejections.forEach(({ file, errors }) => {
errors.forEach(({ message }) => {
notifications.warning(`Rejected file: ${file.name}`, message);
});
});
},
[notifications, dzAccept],
[notifications],
);

const handleFileRemove = React.useCallback(
Expand Down Expand Up @@ -272,7 +261,7 @@ export const MultiFileUpload: React.FC<MultiFileUploadProps> = ({
<MultipleFileUpload
onFileDrop={handleFileDrop}
dropzoneProps={{
accept: dzAccept,
accept: dropZoneAccepts,
onDropRejected: handleFileReject,
}}
disabled={uploading}
Expand Down
2 changes: 1 addition & 1 deletion src/app/Shared/Components/SelectTemplateSelectorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const SelectTemplateSelectorForm: React.FC<SelectTemplateSelectorFormProp
);

const handleTemplateSelect = React.useCallback(
(selected: string) => {
(_, selected: string) => {
if (!selected.length) {
onSelect(undefined);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* limitations under the License.
*/

import * as React from 'react';
import { MultipleFileUploadProps } from '@patternfly/react-core';

Check warning on line 18 in src/app/Shared/Components/types.tsx

View workflow job for this annotation

GitHub Actions / eslint-check (18.x)

`@patternfly/react-core` import should occur before import of `react`

Check warning on line 18 in src/app/Shared/Components/types.tsx

View workflow job for this annotation

GitHub Actions / eslint-check (16.x)

`@patternfly/react-core` import should occur before import of `react`

export interface LoadingProps {
spinnerAriaValueText?: string; // Text describing that current loading status or progress
spinnerAriaLabelledBy?: string; // Id of element which describes what is being loaded
Expand All @@ -24,3 +27,12 @@ export interface LoadingProps {
export type DescriptionProps = {
children?: React.ReactNode;
};

export type Unpacked<T> = T extends (infer A)[] ? A : T;

// FIXME: React drop-zone types cannot be imported
export type DropzoneOptions = NonNullable<MultipleFileUploadProps['dropzoneProps']>;

export type FileRejection = Unpacked<Parameters<NonNullable<DropzoneOptions['onDropRejected']>>[0]>;

export type DropzoneAccept = DropzoneOptions['accept'];

0 comments on commit d7c4dd8

Please sign in to comment.