I would like to create my own dna sequence viewer in a react ts app (to learn and practice coding).
I get the sequence from my flask server as a long string, very well.
but in the case of the sequence is very large (more than 4 millions), my app crash.
Because it is not simply to display text, but also a rule under the letter and give for each letter (A,C,G, or T) a different color – the rule is mandatory, the different colors displaying not, if it’s make the app to slow – (see the picture)
,
My render text code:
const renderColoredText = (text: string) => {
return text.split('').map((char, index) => {
let color = 'black';
switch (char.toLowerCase()) {
case 'a':
color = 'primary';
break;
case 'b':
color = 'secondary';
break;
case 'c':
color = 'warning';
break;
default:
break;
}
const borderBottom = index % 10 === 9 ? '3px solid lightblue' : '1px solid lightblue'
const padding = '5px'
return (
<Box
key={index}
sx={{
display: 'inline-block',
borderBottom,
// color: getNucleotideColor(nucleotide),
paddingTop: '2px',
position: 'relative',
marginBottom: '20px',
}}
>
{char}
{
index % 10 === 9 && (
<Box
sx={{
position: 'absolute',
top: '110%', // Below the nucleotide
left: '50%',
transform: 'translateX(-50%)',
fontSize: '12px',
color: 'black',
}}
>
{index + 1}
</Box>
)}
</Box>
I added an interval, so each 1 sec it will add to sequence chunk of 10000 or 20000 letters (more than that it crash), And in the case of a long sequence of 4-5 millions, it will a very long time to updating, and at some point, crash.
My interval code:
const chunkSize = 10000;
const updateInterval = 1000;
useEffect(() => {
const currentUser = getUserToken()
if (!currentUser || !currentUser._id)
return;
getWorkspaceInput(currentUser._id)
.then(data => {
let currentIndex = 0;
const intervalId = setInterval(() => {
const nextChunk = data.input.slice(currentIndex, currentIndex + chunkSize);
setSequence(prevSequence => prevSequence + nextChunk);
currentIndex += chunkSize;
if (currentIndex >= data.input.length) {
clearInterval(intervalId);
}
}, updateInterval);
return () => {
clearInterval(intervalId);
};
})
},[])
Do you have any idea how to do this ?
Thank you