Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ Typescript Cleanup #118

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions example/src/ConversationScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ import { PermissionStatus } from "expo-modules-core";
import type { DocumentPickerAsset } from "expo-document-picker";
import type { ImagePickerAsset } from "expo-image-picker";

type Attachment = {
file?: DocumentPickerAsset
image?: ImagePickerAsset
}

/// Show the messages in a conversation.
export default function ConversationScreen({
route,
}: NativeStackScreenProps<NavigationParamList, "conversation">) {
let { topic } = route.params;
let messageListRef = useRef<FlatList>();
let messageListRef = useRef<FlatList>(null);
let {
data: messages,
refetch: refreshMessages,
Expand All @@ -52,8 +57,7 @@ export default function ConversationScreen({
let [replyingTo, setReplyingTo] = useState<string | null>(null);
let [text, setText] = useState("");
let [isShowingAttachmentModal, setShowingAttachmentModal] = useState(false);
let [attachment, setAttachment] = useState<
{ image: ImagePickerAsset } | { file: DocumentPickerAsset } | null
let [attachment, setAttachment] = useState<Attachment | null
>(null);
let [isAttachmentPreviewing, setAttachmentPreviewing] = useState(false);
let [isSending, setSending] = useState(false);
Expand Down Expand Up @@ -255,16 +259,15 @@ function AttachmentPreviewModal({
onRequestClose,
}: {
attachment:
| { image: ImagePickerAsset }
| { file: DocumentPickerAsset }
Attachment
| null;
visible: boolean;
onRequestClose: () => void;
}) {
let isImage = attachment?.image?.type === "image";
return (
<CenteredModal visible={visible} onRequestClose={onRequestClose}>
{isImage && (
{(isImage && attachment?.image) && (
<Image
source={attachment.image}
style={{
Expand Down Expand Up @@ -306,7 +309,7 @@ function AttachmentInputHeader({
onRemove,
}: {
topic: string;
attachment: { file: DocumentPickerAsset } | { image: ImagePickerAsset };
attachment: Attachment;
onPress: () => void;
onRemove: () => void;
}) {
Expand All @@ -323,7 +326,7 @@ function AttachmentInputHeader({
}}
>
<TouchableOpacity onPress={onPress}>
{isImage && (
{(isImage && attachment?.image) && (
<Image
source={attachment.image}
style={{
Expand Down Expand Up @@ -609,7 +612,7 @@ function AttachmentModal({
copyToCacheDirectory: true,
multiple: false,
});
if (result.type === "success" && result.assets?.length) {
if (!result.canceled && result.assets?.length) {
onAttachedFile(result.assets[0]);
}
}}
Expand Down
2 changes: 1 addition & 1 deletion example/src/HomeScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NavigationParamList } from "./Navigation";
import { NativeStackScreenProps } from "@react-navigation/native-stack";
import { useXmtp } from "./XmtpContext";
import { useConversationList, useLastMessage, useMessages } from "./hooks";
import { useConversationList, useMessages } from "./hooks";
import {
Button,
FlatList,
Expand Down
4 changes: 2 additions & 2 deletions example/src/XmtpContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext, useContext, useMemo, useState } from "react";
import {createContext, FC, useContext, useMemo, useState} from "react";
import * as XMTP from "xmtp-react-native-sdk";

const XmtpContext = createContext<{
Expand All @@ -9,7 +9,7 @@ const XmtpContext = createContext<{
setClient: () => {},
});
export const useXmtp = () => useContext(XmtpContext);
export function XmtpContextProvider({ children }) {
export const XmtpContextProvider: FC<any> = ({ children }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think FC<PropsWithChildren> would work better here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh yes, I missed that..

let [client, setClient] = useState<XMTP.Client | null>(null);
let context = useMemo(() => ({ client, setClient }), [client, setClient]);
return (
Expand Down
2 changes: 1 addition & 1 deletion example/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export function usePrepareRemoteAttachment({
fileUri,
mimeType,
}: {
fileUri?: `file://${string}`;
fileUri?: string;
mimeType?: string;
}): {
remoteAttachment: RemoteAttachmentContent | undefined;
Expand Down
2 changes: 1 addition & 1 deletion example/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function uploadFile(
return url;
}

export async function downloadFile(url: string): Promise<`file://${string}`> {
export async function downloadFile(url: string): Promise<string> {
console.log("downloading from", url);
let res = await ReactNativeBlobUtil.config({ fileCache: true }).fetch(
"GET",
Expand Down