Spaces:
Running
Running
File size: 4,631 Bytes
e6a9f90 | 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 145 146 147 148 149 150 151 152 153 154 155 156 | import React, { forwardRef, useImperativeHandle, useState, useEffect, useRef } from 'react';
import { Section } from '../../types/Conversation';
import SectionItem from './SectionItem';
export interface ScrollingSectionsProps {
sections: Section[];
onSectionChange?: (index: number) => void;
displayProp: string;
showProgress?: boolean;
displayBeforeAndAfter?: boolean;
startAtTop?: boolean;
onComplete?: () => void;
}
export interface ScrollingSectionsRef {
play: () => void;
pause: () => void;
goToSection: (index: number) => void;
}
const ScrollingSections = forwardRef<ScrollingSectionsRef, ScrollingSectionsProps>(
({ sections, onSectionChange, displayProp, showProgress = true, displayBeforeAndAfter = true, startAtTop = false, onComplete }, ref) => {
const [activeIndex, setActiveIndex] = useState(0);
const [isPlaying, setIsPlaying] = useState(false);
const [progress, setProgress] = useState(0);
const progressIntervalRef = useRef<number | null>(null);
const sectionTimeoutRef = useRef<number | null>(null);
const [elapsedTime, setElapsedTime] = useState(0); // in milliseconds
const startProgress = () => {
if (!isPlaying) return;
const currentDuration = sections[activeIndex]?.duration || 10;
const totalDurationMs = currentDuration * 1000;
const startTime = Date.now() - elapsedTime;
if (progressIntervalRef.current) {
window.clearInterval(progressIntervalRef.current);
}
progressIntervalRef.current = window.setInterval(() => {
const now = Date.now();
const newElapsed = now - startTime;
const newProgress = (newElapsed / totalDurationMs) * 100;
if (newProgress >= 100) {
window.clearInterval(progressIntervalRef.current!);
setProgress(100);
setElapsedTime(0);
goToNextSection();
} else {
setProgress(newProgress);
setElapsedTime(newElapsed);
}
}, 100);
};
const goToNextSection = () => {
setActiveIndex((prev) => (prev + 1) % sections.length);
onSectionChange?.((activeIndex + 1) % sections.length);
if ((activeIndex + 1) % sections.length === 0) {
pause();
onComplete?.();
}
};
const goToSection = (index: number) => {
if (index >= 0 && index < sections.length) {
setActiveIndex(index);
setProgress(0);
setElapsedTime(0); // reset elapsed time
onSectionChange?.(index);
}
};
const play = () => {
setIsPlaying(true);
};
const pause = () => {
setIsPlaying(false);
if (progressIntervalRef.current) {
window.clearInterval(progressIntervalRef.current);
}
if (sectionTimeoutRef.current) {
window.clearTimeout(sectionTimeoutRef.current);
}
};
useImperativeHandle(ref, () => ({
play,
pause,
goToSection
}));
useEffect(() => {
if (isPlaying) {
startProgress();
}
return () => {
if (progressIntervalRef.current) {
window.clearInterval(progressIntervalRef.current);
}
if (sectionTimeoutRef.current) {
window.clearTimeout(sectionTimeoutRef.current);
}
};
}, [activeIndex, isPlaying]);
const getSectionPosition = (index: number) => {
if (index === activeIndex) return 'active';
if (index === activeIndex - 1 && displayBeforeAndAfter) return 'before';
if (index === (activeIndex + 1) % sections.length && displayBeforeAndAfter) return 'after';
return null;
};
if (!sections || sections.length === 0) {
return <div style={{ textAlign: 'center', padding: '1.5rem' }}>No sections available</div>;
}
return (
<div className="sections-container">
<div className={startAtTop ? 'sections-viewport' : 'sections-viewport topmargin'}>
{sections.map((section, index) => {
const position = getSectionPosition(index);
if (position) {
return (
<SectionItem
key={index}
section={section}
position={position}
displayProp={displayProp}
/>
);
}
return null;
})}
</div>
{showProgress && (
<div className="progress-container">
<div
className="progress-bar"
style={{ width: `${progress}%` }}
/>
</div>
)}
</div>
);
}
);
export default ScrollingSections; |