File size: 2,102 Bytes
4e1096a | 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 | import type React from 'react';
import { memo, useMemo } from 'react';
import type { Book } from '@/types/book';
import { useTranslation } from '@/hooks/useTranslation';
import { SHOW_UNREAD_STATUS_BADGE } from '@/services/constants';
import StatusBadge from './StatusBadge';
interface ReadingProgressProps {
book: Book;
}
const getProgressPercentage = (book: Book) => {
if (!book.progress || !book.progress[1]) {
return null;
}
if (book.progress && book.progress[1] === 1) {
return 100;
}
const percentage = Math.round((book.progress[0] / book.progress[1]) * 100);
return Math.max(0, Math.min(100, percentage));
};
const ReadingProgress: React.FC<ReadingProgressProps> = memo(
({ book }) => {
const _ = useTranslation();
const progressPercentage = useMemo(() => getProgressPercentage(book), [book]);
if (book.readingStatus === 'finished') {
return (
<div className='flex justify-start'>
<StatusBadge status={book.readingStatus}>{_('Finished')}</StatusBadge>
</div>
);
}
if (book.readingStatus === 'unread') {
if (SHOW_UNREAD_STATUS_BADGE) {
return (
<div className='flex justify-start'>
<StatusBadge status={book.readingStatus}>{_('Unread')}</StatusBadge>
</div>
);
} else {
return <div className='flex justify-start'></div>;
}
}
if (progressPercentage === null || Number.isNaN(progressPercentage)) {
return <div className='flex justify-start'></div>;
}
return (
<div
className='text-neutral-content/70 flex justify-between text-xs'
role='status'
aria-label={`${progressPercentage}%`}
>
<span>{progressPercentage}%</span>
</div>
);
},
(prevProps, nextProps) => {
return (
prevProps.book.hash === nextProps.book.hash &&
prevProps.book.updatedAt === nextProps.book.updatedAt &&
prevProps.book.readingStatus === nextProps.book.readingStatus
);
},
);
ReadingProgress.displayName = 'ReadingProgress';
export default ReadingProgress;
|