@openpeepshq/rn-components 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,19 +4,30 @@ import {
4
4
  ScrollView,
5
5
  ActivityIndicator,
6
6
  } from 'react-native';
7
- import React, { useEffect } from 'react';
7
+ import React, { useCallback, useEffect, useRef } from 'react';
8
+ import { BottomSheetModal } from '@gorhom/bottom-sheet';
8
9
  import { ThemedView } from '../../components/ui/themed-view';
9
10
  import { MainScreenProps } from '../../components/navigation/types/index';
10
11
  import { useNewConversationStore } from '../../stores/useNewConversationStore';
11
12
  import { TouchableOpacity } from 'react-native';
12
13
  import { useSetPageHeader } from '../../components/layout';
13
- import { SendHorizonalIcon } from '../../components/icons/index';
14
- import { useCreateConversation, useOpenpeeps } from '@openpeepshq/react';
14
+ import { ImageIcon, SendHorizonalIcon } from '../../components/icons/index';
15
+ import {
16
+ canSendDmMessage,
17
+ useCreateConversation,
18
+ useOpenpeeps,
19
+ DM_MAX_ATTACHMENTS,
20
+ } from '@openpeepshq/react';
15
21
  import { ThemedText } from '../../components/ui/themed-text';
16
- import { Profile, PublicProfile } from '@openpeepshq/common';
22
+ import { MediaAttachment, Profile, PublicProfile } from '@openpeepshq/common';
23
+ import { ImagePickerSheet } from '../../components/custom/index';
24
+ import { ComposeAttachments } from '../../components/post/post-form/ComposeAttachments';
17
25
  import { OpenpeepsMarkdownInput } from '../../components/post/post-form/OpenpeepsMarkdownInput';
26
+ import { Button } from '../../components/ui/button';
18
27
  import { useTranslation } from 'react-i18next';
19
28
  import { maxContentLength } from '../../lib/utils';
29
+ import { checkMediaPermissions } from '../../lib/media-permissions';
30
+ import { isAttachmentProcessing } from '../../lib/post';
20
31
 
21
32
  type DraftMessageProps = MainScreenProps<'DraftMessage'>;
22
33
 
@@ -25,18 +36,40 @@ export const DraftMessage = ({ navigation }: DraftMessageProps) => {
25
36
  const { members, clearMembers, contnt, setContt } = useNewConversationStore();
26
37
  const { currentProfile } = useOpenpeeps();
27
38
  const scrollViewRef = React.useRef<ScrollView>(null);
28
- const { message, setMessage, send, submitting } = useCreateConversation({
29
- profiles: members as PublicProfile[],
30
- message: contnt,
31
- skipProfileSelection: true,
32
- onClose: () => {
33
- setContt('');
34
- clearMembers();
35
- navigation.pop();
36
- navigation.pop();
37
- },
39
+ const { message, setMessage, attachments, setAttachments, send, submitting } =
40
+ useCreateConversation({
41
+ profiles: members as PublicProfile[],
42
+ message: contnt,
43
+ skipProfileSelection: true,
44
+ onClose: () => {
45
+ setContt('');
46
+ clearMembers();
47
+ navigation.pop();
48
+ navigation.pop();
49
+ },
50
+ });
51
+ const imagePickerRef = useRef<BottomSheetModal>(null);
52
+ const canSend = canSendDmMessage({
53
+ sending: submitting,
54
+ uploadsPending: attachments.some(isAttachmentProcessing),
55
+ text: message,
56
+ attachments,
38
57
  });
39
58
 
59
+ const openImagePicker = useCallback(async () => {
60
+ const hasPermission = await checkMediaPermissions(t, 'photo');
61
+ if (hasPermission) imagePickerRef.current?.present();
62
+ }, [t]);
63
+
64
+ const addAttachments = useCallback(
65
+ (images: MediaAttachment[]) => {
66
+ setAttachments((current) =>
67
+ [...current, ...images].slice(0, DM_MAX_ATTACHMENTS)
68
+ );
69
+ },
70
+ [setAttachments]
71
+ );
72
+
40
73
  useEffect(() => {
41
74
  if (contnt) setMessage(contnt);
42
75
  }, [contnt, setMessage]);
@@ -65,6 +98,21 @@ export const DraftMessage = ({ navigation }: DraftMessageProps) => {
65
98
  </ScrollView>
66
99
 
67
100
  <ThemedView className="p-2 border-t border-border bg-background">
101
+ {attachments.length > 0 ? (
102
+ <ComposeAttachments
103
+ attachments={attachments}
104
+ removeAttachment={(index) =>
105
+ setAttachments((current) =>
106
+ current.filter((_, i) => i !== index)
107
+ )
108
+ }
109
+ updateAttachment={(index, attachment) =>
110
+ setAttachments((current) =>
111
+ current.map((item, i) => (i === index ? attachment : item))
112
+ )
113
+ }
114
+ />
115
+ ) : null}
68
116
  <OpenpeepsMarkdownInput
69
117
  rows={5}
70
118
  value={message}
@@ -74,18 +122,22 @@ export const DraftMessage = ({ navigation }: DraftMessageProps) => {
74
122
  defaultValue: 'Write a message…',
75
123
  })}
76
124
  />
77
- <View className="flex-row justify-end">
125
+ <View className="flex-row items-center justify-between">
126
+ <Button
127
+ size="icon"
128
+ variant="ghost"
129
+ accessibilityLabel={t('posts.form.addImage')}
130
+ onPress={() => void openImagePicker()}
131
+ disabled={submitting || attachments.length >= DM_MAX_ATTACHMENTS}
132
+ testID="conversation-add-image"
133
+ >
134
+ <ImageIcon size={20} className="text-foreground" />
135
+ </Button>
78
136
  <TouchableOpacity
79
137
  onPress={() => void send()}
80
- disabled={
81
- !message.trim() ||
82
- submitting ||
83
- message.length > maxContentLength
84
- }
138
+ disabled={!canSend}
85
139
  className={`p-2 rounded-full ${
86
- message.trim() && message.length <= maxContentLength
87
- ? 'bg-primary'
88
- : 'bg-surface'
140
+ canSend ? 'bg-primary' : 'bg-surface'
89
141
  }`}
90
142
  >
91
143
  {submitting ? (
@@ -97,6 +149,11 @@ export const DraftMessage = ({ navigation }: DraftMessageProps) => {
97
149
  </View>
98
150
  </ThemedView>
99
151
  </KeyboardAvoidingView>
152
+ <ImagePickerSheet
153
+ ref={imagePickerRef}
154
+ usage="post-image"
155
+ onSelect={addAttachments}
156
+ />
100
157
  </ThemedView>
101
158
  );
102
159
  };
@@ -1,10 +1,13 @@
1
1
  import React, { useCallback, useEffect, useRef, useState } from 'react';
2
+ import { BottomSheetModal } from '@gorhom/bottom-sheet';
2
3
  import { MainScreenProps } from '../../components/navigation/types/index';
3
4
  import {
4
5
  adjustUnseenCounts,
5
6
  apiErrorMessage,
7
+ canSendDmMessage,
6
8
  useOpenpeeps,
7
9
  usePostViewFlush,
10
+ DM_MAX_ATTACHMENTS,
8
11
  } from '@openpeepshq/react';
9
12
  import { useTranslation } from 'react-i18next';
10
13
  import { useSetPageHeader } from '../../components/layout';
@@ -18,9 +21,16 @@ import {
18
21
  ActivityIndicator,
19
22
  } from 'react-native';
20
23
  import Toast from 'react-native-toast-message';
21
- import { InfoIcon, SearchIcon } from '../../components/icons/index';
22
- import { MediaAttachmentData, type PublicPost } from '@openpeepshq/common';
24
+ import { ImageIcon, InfoIcon, SearchIcon } from '../../components/icons/index';
25
+ import {
26
+ MediaAttachment,
27
+ MediaAttachmentData,
28
+ type PublicPost,
29
+ } from '@openpeepshq/common';
30
+ import { ImagePickerSheet } from '../../components/custom/index';
23
31
  import { ComposeAttachments } from '../../components/post/post-form/ComposeAttachments';
32
+ import { checkMediaPermissions } from '../../lib/media-permissions';
33
+ import { isAttachmentProcessing } from '../../lib/post';
24
34
  import { OpenpeepsMarkdownInput } from '../../components/post/post-form/OpenpeepsMarkdownInput';
25
35
  import { maxContentLength } from '../../lib/utils';
26
36
  import { DropdownMenu } from '../../components/ui/dropdown-menu';
@@ -76,10 +86,15 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
76
86
  const [shownMessages, setShownMessages] = useState<PublicPost[]>([]);
77
87
  const [shownFor, setShownFor] = useState(id);
78
88
  const [searchOpen, setSearchOpen] = useState(false);
89
+ const [isSending, setIsSending] = useState(false);
90
+ const [attachments, setAttachments] = useState<MediaAttachmentData[]>([]);
91
+ const [content, setContent] = useState('');
79
92
  if (shownFor !== id) {
80
93
  setShownFor(id);
81
94
  setShownMessages([]);
82
95
  setSearchOpen(false);
96
+ setContent('');
97
+ setAttachments([]);
83
98
  }
84
99
  useEffect(() => {
85
100
  if (conversationQuery.data) setShownMessages(conversationQuery.data);
@@ -88,10 +103,8 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
88
103
  const isLoading = conversationQuery.isLoading && messages.length === 0;
89
104
  const markPostsSeen = openpeepsApi.markPostsSeenAction();
90
105
  const flushPostViews = usePostViewFlush();
91
- const [isSending, setIsSending] = useState(false);
92
- const [attachments, setAttachments] = useState<MediaAttachmentData[]>([]);
93
- const [content, setContent] = useState('');
94
106
  const scrollViewRef = useRef<ScrollView>(null);
107
+ const imagePickerRef = useRef<BottomSheetModal>(null);
95
108
  const messageOffsets = useRef<Record<string, number>>({});
96
109
  const scrolledToMessageRef = useRef<string | null>(null);
97
110
  const [participantsOpen, setParticipantsOpen] = useState(false);
@@ -115,10 +128,26 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
115
128
  setAttachments([]);
116
129
  }, []);
117
130
 
131
+ const canSend = canSendDmMessage({
132
+ sending: isSending,
133
+ uploadsPending: attachments.some(isAttachmentProcessing),
134
+ text: content,
135
+ attachments,
136
+ });
137
+
138
+ const openImagePicker = useCallback(async () => {
139
+ const hasPermission = await checkMediaPermissions(t, 'photo');
140
+ if (hasPermission) imagePickerRef.current?.present();
141
+ }, [t]);
142
+
143
+ const addAttachments = useCallback((images: MediaAttachment[]) => {
144
+ setAttachments((current) =>
145
+ [...current, ...images].slice(0, DM_MAX_ATTACHMENTS)
146
+ );
147
+ }, []);
148
+
118
149
  const handleSendMessage = async () => {
119
- if (!content.trim() || content.length > maxContentLength) {
120
- return;
121
- }
150
+ if (!canSend) return;
122
151
 
123
152
  try {
124
153
  setIsSending(true);
@@ -128,7 +157,8 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
128
157
  type: 'note',
129
158
  data: {
130
159
  type: 'note',
131
- content: content,
160
+ content: content.trim(),
161
+ attachments: attachments.length ? attachments : undefined,
132
162
  },
133
163
  });
134
164
  resetForm();
@@ -344,15 +374,23 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
344
374
  defaultValue: 'Write a message…',
345
375
  })}
346
376
  />
347
- <View className="flex-row justify-end">
377
+ <View className="flex-row items-center justify-between">
348
378
  <Button
349
- variant="default"
350
- onPress={handleSendMessage}
379
+ size="icon"
380
+ variant="ghost"
381
+ accessibilityLabel={t('posts.form.addImage')}
382
+ onPress={() => void openImagePicker()}
351
383
  disabled={
352
- isSending ||
353
- !content.trim() ||
354
- content.length > maxContentLength
384
+ isSending || attachments.length >= DM_MAX_ATTACHMENTS
355
385
  }
386
+ testID="conversation-add-image"
387
+ >
388
+ <ImageIcon size={20} className="text-foreground" />
389
+ </Button>
390
+ <Button
391
+ variant="default"
392
+ onPress={handleSendMessage}
393
+ disabled={!canSend}
356
394
  >
357
395
  {isSending ? (
358
396
  <View className="flex-row items-center">
@@ -380,6 +418,11 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
380
418
  />
381
419
  ) : null}
382
420
  </View>
421
+ <ImagePickerSheet
422
+ ref={imagePickerRef}
423
+ usage="post-image"
424
+ onSelect={addAttachments}
425
+ />
383
426
  </ThemedView>
384
427
  );
385
428
  };