|
| 1 | +import React, { useState, useEffect, useCallback, useRef } from 'react'; |
| 2 | +import { useLocation } from '@docusaurus/router'; |
| 3 | +import confetti from 'canvas-confetti'; |
| 4 | +import styles from './styles.module.css'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Caminhos onde a barra de progresso NÃO deve ser exibida |
| 8 | + * |
| 9 | + * Adicione aqui os paths que são muito curtos ou onde a barra |
| 10 | + * de progresso não faz sentido (ex: landing pages, índices, etc) |
| 11 | + */ |
| 12 | +const EXCLUDED_PATHS = [ |
| 13 | + '/pt_BR/', |
| 14 | + '/pt_BR', |
| 15 | + // Adicione mais paths aqui conforme necessário |
| 16 | + // Exemplo: '/sobre', '/contato', etc |
| 17 | +]; |
| 18 | + |
| 19 | +/** |
| 20 | + * Barra de Progresso de Leitura |
| 21 | + * |
| 22 | + * Mostra o progresso de leitura da página atual e dispara confetti |
| 23 | + * quando o usuário completa a leitura (chega ao final da página). |
| 24 | + * |
| 25 | + * A barra não é exibida em páginas definidas em EXCLUDED_PATHS. |
| 26 | + */ |
| 27 | +export default function ReadingProgressBar() { |
| 28 | + const location = useLocation(); |
| 29 | + const [progress, setProgress] = useState(0); |
| 30 | + const hasCompletedRef = useRef(false); |
| 31 | + const confettiTimeoutRef = useRef(null); |
| 32 | + |
| 33 | + // Verificar se a página atual está na lista de exclusão |
| 34 | + const isExcludedPath = EXCLUDED_PATHS.includes(location.pathname); |
| 35 | + |
| 36 | + const fireConfetti = useCallback(() => { |
| 37 | + const duration = 3000; |
| 38 | + const animationEnd = Date.now() + duration; |
| 39 | + const defaults = { |
| 40 | + startVelocity: 30, |
| 41 | + spread: 360, |
| 42 | + ticks: 60, |
| 43 | + zIndex: 10000, |
| 44 | + colors: ['#f7df1e', '#FFD700', '#FFA500', '#FF6347'] |
| 45 | + }; |
| 46 | + |
| 47 | + function randomInRange(min, max) { |
| 48 | + return Math.random() * (max - min) + min; |
| 49 | + } |
| 50 | + |
| 51 | + const interval = setInterval(function() { |
| 52 | + const timeLeft = animationEnd - Date.now(); |
| 53 | + |
| 54 | + if (timeLeft <= 0) { |
| 55 | + return clearInterval(interval); |
| 56 | + } |
| 57 | + |
| 58 | + const particleCount = 50 * (timeLeft / duration); |
| 59 | + |
| 60 | + confetti({ |
| 61 | + ...defaults, |
| 62 | + particleCount, |
| 63 | + origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 } |
| 64 | + }); |
| 65 | + confetti({ |
| 66 | + ...defaults, |
| 67 | + particleCount, |
| 68 | + origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 } |
| 69 | + }); |
| 70 | + }, 250); |
| 71 | + }, []); |
| 72 | + |
| 73 | + const handleScroll = useCallback(() => { |
| 74 | + // Não processar scroll em páginas excluídas |
| 75 | + if (isExcludedPath) return; |
| 76 | + |
| 77 | + const windowHeight = window.innerHeight; |
| 78 | + const documentHeight = document.documentElement.scrollHeight; |
| 79 | + const scrollTop = window.scrollY || window.pageYOffset || document.documentElement.scrollTop; |
| 80 | + |
| 81 | + // Calcular progresso |
| 82 | + const scrollableHeight = documentHeight - windowHeight; |
| 83 | + const scrollPercentage = scrollableHeight > 0 |
| 84 | + ? Math.min((scrollTop / scrollableHeight) * 100, 100) |
| 85 | + : 0; |
| 86 | + |
| 87 | + setProgress(scrollPercentage); |
| 88 | + |
| 89 | + // Disparar confetti quando completar a leitura (chegou a 95%+) |
| 90 | + if (scrollPercentage >= 95 && !hasCompletedRef.current) { |
| 91 | + hasCompletedRef.current = true; |
| 92 | + |
| 93 | + // Adicionar pequeno delay para garantir que o usuário realmente chegou ao final |
| 94 | + if (confettiTimeoutRef.current) { |
| 95 | + clearTimeout(confettiTimeoutRef.current); |
| 96 | + } |
| 97 | + |
| 98 | + confettiTimeoutRef.current = setTimeout(() => { |
| 99 | + fireConfetti(); |
| 100 | + }, 300); |
| 101 | + } |
| 102 | + |
| 103 | + // Reset se o usuário voltar para o topo |
| 104 | + if (scrollPercentage < 10) { |
| 105 | + hasCompletedRef.current = false; |
| 106 | + } |
| 107 | + }, [fireConfetti, isExcludedPath]); |
| 108 | + |
| 109 | + useEffect(() => { |
| 110 | + // Adicionar listener de scroll |
| 111 | + window.addEventListener('scroll', handleScroll, { passive: true }); |
| 112 | + |
| 113 | + // Calcular progresso inicial |
| 114 | + handleScroll(); |
| 115 | + |
| 116 | + // Cleanup |
| 117 | + return () => { |
| 118 | + window.removeEventListener('scroll', handleScroll); |
| 119 | + if (confettiTimeoutRef.current) { |
| 120 | + clearTimeout(confettiTimeoutRef.current); |
| 121 | + } |
| 122 | + }; |
| 123 | + }, [handleScroll]); |
| 124 | + |
| 125 | + // Reset quando mudar de página |
| 126 | + useEffect(() => { |
| 127 | + hasCompletedRef.current = false; |
| 128 | + setProgress(0); |
| 129 | + }, [location.pathname]); |
| 130 | + |
| 131 | + // Não renderizar a barra em páginas excluídas |
| 132 | + if (isExcludedPath) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + return ( |
| 137 | + <div className={styles.progressBarContainer}> |
| 138 | + <div |
| 139 | + className={styles.progressBar} |
| 140 | + style={{ width: `${progress}%` }} |
| 141 | + role="progressbar" |
| 142 | + aria-valuenow={Math.round(progress)} |
| 143 | + aria-valuemin="0" |
| 144 | + aria-valuemax="100" |
| 145 | + aria-label={`Progresso de leitura: ${Math.round(progress)}%`} |
| 146 | + /> |
| 147 | + </div> |
| 148 | + ); |
| 149 | +} |
0 commit comments