Skip to content

Commit

Permalink
Fixed types in getFileAttachmentViewObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho committed Apr 13, 2023
1 parent f394091 commit d2b4ecb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
18 changes: 10 additions & 8 deletions x-pack/plugins/cases/common/api/cases/comment/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ import * as rt from 'io-ts';
import { MAX_DELETE_FILES } from '../../../constants';
import { limitedArraySchema, NonEmptyString } from '../../../schema';

export const SingleFileAttachmentMetadataRt = rt.type({
name: rt.string,
extension: rt.string,
mimeType: rt.string,
created: rt.string,
});

export const FileAttachmentMetadataRt = rt.type({
files: rt.array(
rt.type({
name: rt.string,
extension: rt.string,
mimeType: rt.string,
created: rt.string,
})
),
files: rt.array(SingleFileAttachmentMetadataRt),
});

export type FileAttachmentMetadata = rt.TypeOf<typeof FileAttachmentMetadataRt>;

export type DownloadableFile = rt.TypeOf<typeof SingleFileAttachmentMetadataRt> & { id: string };

export const FILE_ATTACHMENT_TYPE = '.files';

const MIN_DELETE_IDS = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import React from 'react';

import { EuiLink } from '@elastic/eui';
import type { FileJSON } from '@kbn/shared-ux-file-types';

import type { FileJSON } from '@kbn/shared-ux-file-types';
import * as i18n from './translations';
import { isImage } from './utils';

interface FileNameLinkProps {
file: FileJSON;
file: Pick<FileJSON, 'name' | 'extension' | 'mimeType'>;
showPreview: () => void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useCasesContext } from '../cases_context/use_cases_context';

interface FilePreviewProps {
closePreview: () => void;
selectedFile: FileJSON;
selectedFile: Pick<FileJSON, 'id' | 'name'>;
}

const StyledOverlayMask = styled(EuiOverlayMask)`
Expand Down
13 changes: 5 additions & 8 deletions x-pack/plugins/cases/public/components/files/file_type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/
import React from 'react';

import type { FileJSON } from '@kbn/shared-ux-file-types';

import type { DownloadableFile } from '../../../common/api';
import type {
ExternalReferenceAttachmentType,
ExternalReferenceAttachmentViewProps,
Expand All @@ -24,7 +23,7 @@ import { useFilePreview } from './use_file_preview';
import { FileDeleteButtonIcon } from './file_delete_button_icon';

interface FileAttachmentEventProps {
file: FileJSON;
file: DownloadableFile;
}

const FileAttachmentEvent = ({ file }: FileAttachmentEventProps) => {
Expand Down Expand Up @@ -77,13 +76,11 @@ const getFileAttachmentViewObject = (props: ExternalReferenceAttachmentViewProps
const fileId = props.externalReferenceId;
const caseId = props.caseData.id;

// @ts-ignore
const partialFileJSON = props.externalReferenceMetadata?.files[0] as Partial<FileJSON>;

const fileMetadata = props.externalReferenceMetadata.files[0];
const file = {
id: fileId,
...partialFileJSON,
} as FileJSON;
...fileMetadata,
};

return {
event: <FileAttachmentEvent file={file} />,
Expand Down
11 changes: 6 additions & 5 deletions x-pack/plugins/cases/public/components/files/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* 2.0.
*/

import type { FileJSON } from '@kbn/shared-ux-file-types';

import type { CommentRequestExternalReferenceType } from '../../../common/api';
import type {
CommentRequestExternalReferenceType,
FileAttachmentMetadata,
} from '../../../common/api';

import { FileAttachmentMetadataRt } from '../../../common/api';
import * as i18n from './translations';

export const isImage = (file: FileJSON) => file.mimeType?.startsWith('image/');
export const isImage = (file: { mimeType?: string }) => file.mimeType?.startsWith('image/');

export const parseMimeType = (mimeType: string | undefined) => {
if (typeof mimeType === 'undefined') {
Expand All @@ -30,7 +31,7 @@ export const parseMimeType = (mimeType: string | undefined) => {

export const isValidFileExternalReferenceMetadata = (
externalReferenceMetadata: CommentRequestExternalReferenceType['externalReferenceMetadata']
): boolean => {
): externalReferenceMetadata is FileAttachmentMetadata => {
return (
FileAttachmentMetadataRt.is(externalReferenceMetadata) &&
externalReferenceMetadata?.files?.length >= 1
Expand Down

0 comments on commit d2b4ecb

Please sign in to comment.