From f41ffda3e0dc2ca322cb3297865031d0e576f567 Mon Sep 17 00:00:00 2001
From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
Date: Tue, 16 Mar 2021 10:49:19 -0700
Subject: [PATCH] Update types: attachments may not have `url` property
---
ts/components/conversation/Image.stories.tsx | 8 --------
ts/components/conversation/Image.tsx | 2 +-
ts/components/conversation/Message.stories.tsx | 4 ----
ts/components/conversation/MessageAudio.tsx | 13 +++++++++++++
ts/types/Attachment.ts | 8 +++++---
5 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/ts/components/conversation/Image.stories.tsx b/ts/components/conversation/Image.stories.tsx
index a5e3f8fe1..ce31b1e46 100644
--- a/ts/components/conversation/Image.stories.tsx
+++ b/ts/components/conversation/Image.stories.tsx
@@ -186,8 +186,6 @@ story.add('Blurhash', () => {
const props = {
...defaultProps,
blurHash: 'thisisafakeblurhashthatwasmadeup',
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- url: undefined as any,
};
return ;
@@ -198,8 +196,6 @@ story.add('undefined blurHash (light)', () => {
const props = {
...defaultProps,
blurHash: undefined,
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- url: undefined as any,
theme: ThemeType.light,
};
@@ -211,8 +207,6 @@ story.add('undefined blurHash (dark)', () => {
const props = {
...defaultProps,
blurHash: undefined,
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- url: undefined as any,
theme: ThemeType.dark,
};
@@ -225,8 +219,6 @@ story.add('Missing Image', () => {
...defaultProps,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attachment: undefined as any,
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- url: undefined as any,
};
return ;
diff --git a/ts/components/conversation/Image.tsx b/ts/components/conversation/Image.tsx
index 874a7e196..e196acd23 100644
--- a/ts/components/conversation/Image.tsx
+++ b/ts/components/conversation/Image.tsx
@@ -12,7 +12,7 @@ import { AttachmentType, hasNotDownloaded } from '../../types/Attachment';
export type Props = {
alt: string;
attachment: AttachmentType;
- url: string;
+ url?: string;
height?: number;
width?: number;
diff --git a/ts/components/conversation/Message.stories.tsx b/ts/components/conversation/Message.stories.tsx
index bc88775df..81b4a69e5 100644
--- a/ts/components/conversation/Message.stories.tsx
+++ b/ts/components/conversation/Message.stories.tsx
@@ -759,8 +759,6 @@ story.add('Audio with Not Downloaded Attachment', () => {
{
contentType: AUDIO_MP3,
fileName: 'incompetech-com-Agnus-Dei-X.mp3',
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- url: undefined as any,
},
],
status: 'sent',
@@ -775,8 +773,6 @@ story.add('Audio with Pending Attachment', () => {
{
contentType: AUDIO_MP3,
fileName: 'incompetech-com-Agnus-Dei-X.mp3',
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- url: undefined as any,
pending: true,
},
],
diff --git a/ts/components/conversation/MessageAudio.tsx b/ts/components/conversation/MessageAudio.tsx
index 849520a36..8d0ece1dd 100644
--- a/ts/components/conversation/MessageAudio.tsx
+++ b/ts/components/conversation/MessageAudio.tsx
@@ -256,6 +256,13 @@ export const MessageAudio: React.FC = (props: Props) => {
(async () => {
try {
+ if (!attachment.url) {
+ throw new Error(
+ 'Expected attachment url in the MessageAudio with ' +
+ `state: ${state}`
+ );
+ }
+
const { peaks: newPeaks, duration: newDuration } = await loadAudio({
audioContext,
waveformCache,
@@ -374,6 +381,12 @@ export const MessageAudio: React.FC = (props: Props) => {
audio.pause();
}
+ if (!attachment.url) {
+ throw new Error(
+ 'Expected attachment url in the MessageAudio with ' +
+ `state: ${state}`
+ );
+ }
audio.src = attachment.url;
}
};
diff --git a/ts/types/Attachment.ts b/ts/types/Attachment.ts
index 11326ad98..456adb452 100644
--- a/ts/types/Attachment.ts
+++ b/ts/types/Attachment.ts
@@ -28,7 +28,7 @@ export type AttachmentType = {
/** Not included in protobuf, needs to be pulled from flags */
isVoiceMessage?: boolean;
/** For messages not already on disk, this will be a data url */
- url: string;
+ url?: string;
size?: number;
fileSize?: string;
pending?: boolean;
@@ -107,7 +107,9 @@ export function canDisplayImage(
);
}
-export function getThumbnailUrl(attachment: AttachmentType): string {
+export function getThumbnailUrl(
+ attachment: AttachmentType
+): string | undefined {
if (attachment.thumbnail) {
return attachment.thumbnail.url;
}
@@ -115,7 +117,7 @@ export function getThumbnailUrl(attachment: AttachmentType): string {
return getUrl(attachment);
}
-export function getUrl(attachment: AttachmentType): string {
+export function getUrl(attachment: AttachmentType): string | undefined {
if (attachment.screenshot) {
return attachment.screenshot.url;
}