import { DenormalizedAttachmentResource, DenormalizedMessageAttachmentResource, DenormalizedGiphyAttachmentResource, DenormalizedExpandedLinkAttachmentResource, } from '../types/resources/denormalized_attachment_resource' import { DenormalizedAttachmentResourceForCreate, DenormalizedMessageAttachmentResourceForCreate, DenormalizedGiphyAttachmentResourceForCreate, } from '../types/resources/denormalized_attachment_resource_for_create' /** * Converts an array of DenormalizedAttachmentResourceForCreate to DenormalizedAttachmentResource. * This is used to create optimistic UI attachments that match the server format. */ export function convertAttachmentsForCreate( attachments: DenormalizedAttachmentResourceForCreate[] ): DenormalizedAttachmentResource[] { return attachments.map(convertAttachmentForCreate) } /** * Converts a single DenormalizedAttachmentResourceForCreate to DenormalizedAttachmentResource. */ export function convertAttachmentForCreate( attachment: DenormalizedAttachmentResourceForCreate ): DenormalizedAttachmentResource { switch (attachment.type) { case 'MessageAttachment': return convertMessageAttachmentForCreate(attachment) case 'giphy': return convertGiphyAttachmentForCreate(attachment) case 'ExpandedLink': // ExpandedLink is already in the correct format return attachment as DenormalizedExpandedLinkAttachmentResource default: // This should never happen with proper TypeScript typing throw new Error(`Unknown attachment type: ${(attachment as any).type}`) } } /** * Converts a MessageAttachment from "for create" format to denormalized format. */ function convertMessageAttachmentForCreate( attachment: DenormalizedMessageAttachmentResourceForCreate ): DenormalizedMessageAttachmentResource { return { type: 'MessageAttachment', id: attachment.id, attributes: { filename: attachment.file.name, key: '', // Will be filled by server contentType: attachment.file.type, byteSize: attachment.file.size, checksum: '', // Will be filled by server url: attachment.file.uri, // Use local file URI for optimistic UI metadata: { width: attachment.file.width, height: attachment.file.height, analyzed: false, identified: false, }, }, } } /** * Converts a GiphyAttachment from "for create" format to denormalized format. */ function convertGiphyAttachmentForCreate( attachment: DenormalizedGiphyAttachmentResourceForCreate ): DenormalizedGiphyAttachmentResource { return { type: 'giphy', id: attachment.id, title: attachment.title, originalGiphyTitle: attachment.original_giphy_title, titleLink: attachment.title_link, thumbUrl: attachment.thumb_url, giphy: { original: attachment.giphy.original, fixedHeight: attachment.giphy.fixed_height, fixedHeightStill: attachment.giphy.fixed_height_still, fixedHeightDownsampled: attachment.giphy.fixed_height_downsampled, fixedWidth: attachment.giphy.fixed_width, fixedWidthStill: attachment.giphy.fixed_width_still, fixedWidthDownsampled: attachment.giphy.fixed_width_downsampled, }, } }