Skip to content

Commit

Permalink
feat(fe): preload tutorial media files
Browse files Browse the repository at this point in the history
  • Loading branch information
martinscooper committed Oct 23, 2023
1 parent 8f2a9a6 commit 6f12882
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
16 changes: 16 additions & 0 deletions frontend/src/customHooks/usePrealoadMedia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect } from "react";

/*
This hook is used to preload media files.
When images are fetched again the cached media files
will be used.
*/
export const usePrealoadMedia = (media: string[]) => {
useEffect(() => {
media
.forEach((_, i) => {
new Image().src = media[i];
});
}, [media]);
}
15 changes: 6 additions & 9 deletions frontend/src/modules/Workplace/tutorial/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import media4 from "./assets/v3/stage_4.gif";
import media5 from "./assets/v3/stage_5.gif";
import media6 from "./assets/v3/stage_6.gif";
import media7 from "./assets/v3/stage_7.webp";
import { usePrealoadMedia } from "../../../customHooks/usePrealoadMedia";

const media = [media1, media2, media3, media4, media5, media6, media7];

Expand All @@ -52,7 +53,11 @@ interface TutorialProps {

const Tutorial = ({ tutorialOpen, setTutorialOpen }: TutorialProps) => {
const [stageIndex, setStageIndex] = useState(0);


// preload tutorial media files
// so they are fetched before opening the tutorial
usePrealoadMedia(media);

useEffect(() => {
if (tutorialOpen) {
setStageIndex(0);
Expand Down Expand Up @@ -216,14 +221,6 @@ const Tutorial = ({ tutorialOpen, setTutorialOpen }: TutorialProps) => {
},
];

useEffect(() => {
Array(stages.length)
.fill(0)
.forEach((_, i) => {
new Image().src = `./assets/v3/stage_${i + 1}`;
});
}, [stages.length]);

const currentStage = stages[stageIndex];

const handleKeyPress = useCallback(
Expand Down

0 comments on commit 6f12882

Please sign in to comment.