import {ChapterContext, ChapterContextProps} from "@/context/ChapterContext"; import {EditorContext, EditorContextProps} from "@/context/EditorContext"; import React, {useContext, useEffect, useState} from "react"; import {BookOpen, ChevronRight, FileText, Pilcrow, Type} from "lucide-react"; import IconLabel from "@/components/ui/IconLabel"; import {useTranslations} from '@/lib/i18n'; import {AlertContext, AlertContextProps} from "@/context/AlertContext"; import {BookContext, BookContextProps} from "@/context/BookContext"; import {chapterVersions} from "@/lib/constants/chapter"; export default function ScribeFooterBar() { const t = useTranslations(); const {chapter}: ChapterContextProps = useContext(ChapterContext); const {editor}: EditorContextProps = useContext(EditorContext); const {errorMessage}: AlertContextProps = useContext(AlertContext); const {book}: BookContextProps = useContext(BookContext); const [wordsCount, setWordsCount] = useState(0); const [paragraphCount, setParagraphCount] = useState(0); useEffect((): void => { getWordCount(); }, [editor?.state.doc.textContent]); function getWordCount(): void { if (editor) { try { const content: string = editor.state.doc.textContent; const texteNormalise: string = content .replace(/'/g, ' ') .replace(/-/g, ' ') .replace(/\s+/g, ' ') .trim(); const mots: string[] = texteNormalise.split(' '); const wordCount: number = mots.filter( (mot: string): boolean => mot.length > 0, ).length; setWordsCount(wordCount); let paragraphs: number = 0; editor.state.doc.descendants(function (node): void { if (node.type.name === 'paragraph' && node.textContent.trim().length > 0) { paragraphs++; } }); setParagraphCount(paragraphs); } catch (e: unknown) { if (e instanceof Error) { errorMessage(t('errors.wordCountError') + ` (${e.message})`); } else { errorMessage(t('errors.wordCountError')); } } } } function getVersionLabel(): string { if (!chapter) return ''; const version = chapterVersions.find(function (v) { return v.value === chapter.chapterContent.version.toString(); }); return version ? t(version.label) : ''; } return (
{/* Gauche : Breadcrumb */}
{book ? ( <> {book.title} {chapter && ( <> {chapter.title} {getVersionLabel()} )} ) : ( {t('scribeFooterBar.madeWith')} ERitors )}
{/* Droite : Stats */} {(chapter || book) && (
{wordsCount} {t('scribeFooterBar.words')} {Math.ceil(wordsCount / 300)} {t('scribeFooterBar.pages')} {paragraphCount} {t('scribeFooterBar.paragraphs')}
)}
); }