-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathCommentThread.tsx
More file actions
94 lines (85 loc) · 3.79 KB
/
CommentThread.tsx
File metadata and controls
94 lines (85 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { useState, useCallback } from 'react';
import { Alert } from 'react-native';
import { YStack, XStack, Text, Button, Spinner, TextArea, useTheme } from 'tamagui';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { faPaperPlane, faRotate } from '@fortawesome/free-solid-svg-icons';
import useAppTheme from '../hooks/use-app-theme';
import Comment from './Comment';
const CommentThread = ({ comments: initialComments = [], subject, onReloadComments, isReloading }) => {
const { isDarkMode } = useAppTheme();
const theme = useTheme();
const [comments, setComments] = useState(initialComments);
const [input, setInput] = useState('');
const reloadComments = useCallback(async () => {
if (typeof onReloadComments === 'function') {
const newComments = await onReloadComments();
setComments(newComments);
}
}, [onReloadComments]);
const isCommentInvalid = (comment) => {
if (!comment || comment.trim().length < 2) {
Alert.alert('Invalid Comment', 'Comment must be at least 2 characters.');
return true;
}
return false;
};
const publishComment = async () => {
if (isCommentInvalid(input)) return;
// Create a new comment object. Replace this with your API call.
const newComment = {
id: Date.now().toString(),
content: input,
author: {
name: 'Current User', // Replace with your current user data
avatar_url: 'https://example.com/default-avatar.png',
},
created_at: new Date(),
replies: [],
editable: true,
};
setComments([newComment, ...comments]);
setInput('');
};
return (
<YStack space='$4'>
<YStack>
<TextArea
value={input}
placeholder='Write a comment...'
onChangeText={setInput}
width='100%'
bg={isDarkMode ? '$secondary' : '$white'}
borderWidth={1}
borderColor={isDarkMode ? '$gray-600' : '$borderColorWithShadow'}
minHeight={100}
placeholderTextColor={isDarkMode ? '$gray-500' : '$gray-400'}
/>
<XStack justifyContent='flex-end' alignItems='center' marginTop='$2' space='$2'>
{isReloading ? (
<YStack alignItems='center' justifyContent='center' pr='$2'>
<Spinner color='$textPrimary' />
</YStack>
) : (
<Button onPress={reloadComments} size='$3' bg='$default' borderWidth={1} borderColor='$defaultBorder'>
<Button.Icon>
<FontAwesomeIcon icon={faRotate} color={theme['$defaultText'].val} />
</Button.Icon>
</Button>
)}
<Button onPress={publishComment} size='$3' bg='$info' borderWidth={1} borderColor='$infoBorder' disabled={!input.trim()}>
<Button.Icon>
<FontAwesomeIcon icon={faPaperPlane} color={theme['$infoText'].val} />
</Button.Icon>
<Button.Text color='$infoText'>Publish Comment</Button.Text>
</Button>
</XStack>
</YStack>
<YStack space='$4'>
{comments.map((comment) => (
<Comment key={comment.id} comment={comment} reloadComments={reloadComments} isCommentInvalid={isCommentInvalid} />
))}
</YStack>
</YStack>
);
};
export default CommentThread;