Skip to content

Commit 2a7177f

Browse files
committed
ComposeMenu [nfc]: Use our own new type definition for attachments
Soon, we'd like to also use the `insertAttachments` prop not just for react-native-document-picker payloads, but also for react-native-image-picker payloads. Better to define our own type that's meant to accommodate both kinds, rather than, e.g., using react-native-document-picker's type and fudging image-picker payloads to flow through that type. While we're at it, trim down the type to just what `insertAttachments` currently uses: the url [1] and a nullable name for the file. The `type` property in particular smells like something that might not be easy to make uniform across document-picker and image-picker payloads; there's no comment linking to a spec that says what the possible values of `type` mean, for example. [1] renamed from "uri"; see style guide
1 parent 3b1109d commit 2a7177f

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/compose/ComposeBox.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import React, {
1010
forwardRef,
1111
} from 'react';
1212
import { Platform, View } from 'react-native';
13-
import type { DocumentPickerResponse } from 'react-native-document-picker';
1413
import type { LayoutEvent } from 'react-native/Libraries/Types/CoreEventTypes';
1514
import { SafeAreaView } from 'react-native-safe-area-context';
1615
import invariant from 'invariant';
@@ -73,6 +72,7 @@ import useUncontrolledInput from '../useUncontrolledInput';
7372
import { tryFetch } from '../message/fetchActions';
7473
import { getMessageUrl } from '../utils/internalLinks';
7574
import * as logging from '../utils/logging';
75+
import type { Attachment } from './ComposeMenu';
7676

7777
/* eslint-disable no-shadow */
7878

@@ -320,7 +320,7 @@ const ComposeBox: React$AbstractComponent<Props, ImperativeHandle> = forwardRef(
320320

321321
const [numUploading, setNumUploading] = useState<number>(0);
322322
const insertAttachments = useCallback(
323-
async (attachments: $ReadOnlyArray<DocumentPickerResponse>) => {
323+
async (attachments: $ReadOnlyArray<Attachment>) => {
324324
setNumUploading(n => n + 1);
325325
try {
326326
const fileNames: string[] = [];
@@ -338,7 +338,7 @@ const ComposeBox: React$AbstractComponent<Props, ImperativeHandle> = forwardRef(
338338
const placeholder = placeholders[i];
339339
let response = null;
340340
try {
341-
response = await api.uploadFile(auth, attachments[i].uri, fileName);
341+
response = await api.uploadFile(auth, attachments[i].url, fileName);
342342
} catch {
343343
showToast(_('Failed to upload file: {fileName}', { fileName }));
344344
setMessageInputValue(state =>

src/compose/ComposeMenu.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import { androidEnsureStoragePermission } from '../lightbox/download';
2020
import { ThemeContext } from '../styles/theme';
2121
import type { SpecificIconType } from '../common/Icons';
2222

23+
export type Attachment = {|
24+
+name: string | null,
25+
+url: string,
26+
|};
27+
2328
type Props = $ReadOnly<{|
2429
destinationNarrow: Narrow,
25-
insertAttachments: ($ReadOnlyArray<DocumentPickerResponse>) => Promise<void>,
30+
insertAttachments: ($ReadOnlyArray<Attachment>) => Promise<void>,
2631
insertVideoCallLink: (() => void) | null,
2732
|}>;
2833

@@ -221,7 +226,7 @@ export default function ComposeMenu(props: Props): Node {
221226
return;
222227
}
223228

224-
insertAttachments(response);
229+
insertAttachments(response.map(a => ({ name: a.name, url: a.uri })));
225230
}, [_, insertAttachments]);
226231

227232
const styles = useMemo(

0 commit comments

Comments
 (0)