-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathfeedback.tsx
More file actions
144 lines (136 loc) · 8.17 KB
/
Copy pathfeedback.tsx
File metadata and controls
144 lines (136 loc) · 8.17 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {Avatar, AvatarFallback, Button, Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, Separator, SimplePagination, SimplePaginationNavigation, SimplePaginationNextButton, SimplePaginationPreviousButton, SkeletonTable, Tabs, TabsList, TabsTrigger} from '@tryghost/shade/components';
import {HTable} from '@tryghost/shade/primitives';
import {LucideIcon, formatPercentage, formatTimestamp, stringToHslColor, useSimplePagination} from '@tryghost/shade/utils';
import {formatMemberName, getMemberInitials} from '@tryghost/shade/app';
import {useNavigate, useParams} from '@tryghost/admin-x-framework';
import {usePostFeedback} from '@hooks/use-post-feedback';
import {useState} from 'react';
interface FeedbackProps {
feedbackStats: {
positiveFeedback: number;
negativeFeedback: number;
totalFeedback: number;
};
}
const Feedback: React.FC<FeedbackProps> = ({feedbackStats}) => {
const {postId} = useParams();
const navigate = useNavigate();
const [activeFeedbackTab, setActiveFeedbackTab] = useState<'positive' | 'negative'>('positive');
const ITEMS_PER_PAGE = 9;
// Get detailed feedback data for the active tab (all data, not limited)
const score = activeFeedbackTab === 'positive' ? 1 : 0;
const {feedback, isLoading: isFeedbackLoading} = usePostFeedback(postId || '', score);
// Pagination for feedback
const {
totalPages,
paginatedData: paginatedFeedback,
nextPage,
previousPage,
hasNextPage,
hasPreviousPage
} = useSimplePagination({
data: feedback,
itemsPerPage: ITEMS_PER_PAGE
});
const isLoading = isFeedbackLoading;
return (
<Card>
<CardHeader className='pb-5'>
<CardTitle>Feedback</CardTitle>
<CardDescription>What did your readers think?</CardDescription>
</CardHeader>
{feedbackStats.totalFeedback > 0 ?
<CardContent className='pb-3'>
<div className='flex items-center justify-between gap-3'>
<Tabs className='pb-3' defaultValue="positive" value={activeFeedbackTab} variant='button' onValueChange={value => setActiveFeedbackTab(value as 'positive' | 'negative')}>
<TabsList className='gap-1'>
<TabsTrigger className='h-7' value="positive">
<div className='flex items-center gap-1 text-xs'>
<LucideIcon.ThumbsUp size={14} strokeWidth={1.25} />
<span className='hidden font-medium sm:visible! sm:inline!'>More like this</span>
<span className='font-semibold tracking-tight'>{formatPercentage(feedbackStats.positiveFeedback / feedbackStats.totalFeedback)}</span>
</div>
</TabsTrigger>
<TabsTrigger className='h-7' value="negative">
<div className='flex items-center gap-1 text-xs'>
<LucideIcon.ThumbsDown size={14} strokeWidth={1.25} />
<span className='hidden font-medium sm:visible! sm:inline!'>Less like this</span>
<span className='font-semibold tracking-tight'>{formatPercentage(feedbackStats.negativeFeedback / feedbackStats.totalFeedback)}</span>
</div>
</TabsTrigger>
</TabsList>
</Tabs>
<HTable className='mr-2 mb-3 lg:hidden xl:visible! xl:block!'>Date</HTable>
</div>
<Separator />
{isLoading ?
<SkeletonTable className='mt-3' lines={3} />
:
paginatedFeedback && paginatedFeedback.length > 0 ? (
<div className='flex w-full flex-col py-3'>
{paginatedFeedback.map(item => (
<div key={item.id} className='flex h-10 w-full items-center justify-between gap-3 rounded-sm border-none px-2 text-sm hover:cursor-pointer hover:bg-accent' onClick={() => {
navigate(`/members/${item.member.id}`, {crossApp: true});
}}>
<div className='flex items-center gap-2 font-medium'>
<Avatar className='size-7'>
{item.member?.avatar_image && <img className='absolute aspect-square size-full' src={item.member?.avatar_image} />}
<AvatarFallback className='text-white' style={{
backgroundColor: `${stringToHslColor(formatMemberName(item.member), '45', '55')}`
}}>{getMemberInitials(item.member)}</AvatarFallback>
</Avatar>
{formatMemberName(item.member)}
</div>
<div className='whitespace-nowrap text-muted-foreground'>
{formatTimestamp(item.created_at)}
</div>
</div>
))}
</div>
) : (
<div className='flex h-full items-center justify-center py-8 text-center text-sm text-muted-foreground'>
<div>No {activeFeedbackTab === 'positive' ? 'positive' : 'negative'} feedback yet</div>
</div>
)
}
</CardContent>
:
<CardContent className='flex grow flex-col items-center justify-center text-center text-sm text-muted-foreground'>
<div>No members have given feedback yet</div>
<div>When someone does, you'll see their response here.</div>
</CardContent>
}
{feedbackStats.totalFeedback > 0 &&
<CardFooter className='grow-0'>
<div className='flex w-full items-center justify-between gap-3'>
<Button variant='outline' onClick={() => {
const positiveFilter = `(feedback.post_id:'${postId}'+feedback.score:1)`;
const negativeFilter = `(feedback.post_id:'${postId}'+feedback.score:0)`;
const positiveFilterParam = `${encodeURIComponent(positiveFilter)}&post=${postId}`;
const negativeFilterParam = `${encodeURIComponent(negativeFilter)}&post=${postId}`;
navigate(`/members?filter=${activeFeedbackTab === 'positive' ? positiveFilterParam : negativeFilterParam}`, {crossApp: true});
}}>
View all
<LucideIcon.TableOfContents />
</Button>
{totalPages > 1 && (
<SimplePagination className='pb-0'>
<SimplePaginationNavigation>
<SimplePaginationPreviousButton
disabled={!hasPreviousPage}
onClick={previousPage}
/>
<SimplePaginationNextButton
disabled={!hasNextPage}
onClick={nextPage}
/>
</SimplePaginationNavigation>
</SimplePagination>
)}
</div>
</CardFooter>
}
</Card>
);
};
export default Feedback;