Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Specify deps for carousel correctly #266

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions extension/source/Popup/components/Carousel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { FunctionComponent, useState, useEffect } from 'react';
import {
FunctionComponent,
useState,
useEffect,
useCallback,
useMemo,
useRef,
} from 'react';
import { Mutex } from 'async-mutex';

import delay from '../../helpers/delay';
Expand Down Expand Up @@ -41,45 +48,43 @@ const Carousel: FunctionComponent<{
const [imageBPosition, setImageBPosition] = useState<Position>('right');

const [resetting, setResetting] = useState(false);
const [transitionMutex] = useState(new Mutex());
const [intervalId, setIntervalId] = useState(-1);
const transitionMutex = useMemo(() => new Mutex(), []);
const intervalId = useRef<number>();

const transitionImage = async (index: number) => {
const releaseMutex = await transitionMutex.acquire();
const transitionImage = useCallback(
async (index: number) => {
const releaseMutex = await transitionMutex.acquire();

setImageAPosition('left');
setImageBIndex(index);
setImageBPosition('middle');
setImageAPosition('left');
setImageBIndex(index);
setImageBPosition('middle');

await delay(550);
await delay(550);

setResetting(true);
setResetting(true);

setImageAIndex(index);
setImageAPosition('middle');
setImageBPosition('right');
setImageAIndex(index);
setImageAPosition('middle');
setImageBPosition('right');

await delay(50);
await delay(50);

setResetting(false);
setResetting(false);

releaseMutex();
};
releaseMutex();
},
[transitionMutex],
);

useEffect(() => {
console.log('Running useEffect');
let index = 0;
setIntervalId(
setInterval(() => {
index = (index + 1) % images.length;
transitionImage(index);
}, 5000) as unknown as number,
);

return () => clearInterval(intervalId);
intervalId.current = setInterval(() => {
index = (index + 1) % images.length;
transitionImage(index);
}, 5000) as unknown as number;

/* eslint react-hooks/exhaustive-deps: "warn" -- TODO (merge-ok) Add hook deps */
}, []);
return () => clearInterval(intervalId.current);
}, [images.length, intervalId, transitionImage]);

return (
<div className="carousel">
Expand Down Expand Up @@ -112,7 +117,7 @@ const Carousel: FunctionComponent<{
className="radio"
key={imgSrc}
{...onAction(() => {
clearInterval(intervalId);
clearInterval(intervalId.current);
transitionImage(Number(i));
})}
>
Expand Down