-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
137 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<!doctype html> | ||
<!DOCTYPE html> | ||
<html lang="en" class="dark h-full"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@import "src/assets/styles/styles"; | ||
|
||
.autoplay { | ||
position: relative; | ||
} | ||
|
||
.progress { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
background-color: $primary; | ||
height: 5px; | ||
z-index: 0; | ||
transition: width 0.5s linear; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useMemo, useState } from "react"; | ||
import { Technique } from "src/model/Technique"; | ||
import { Alert, Col, Form, Row } from "react-bootstrap"; | ||
import css from "./AutoPlay.module.scss"; | ||
import { useCountDown } from "./useCounter"; | ||
|
||
export interface AutoPlayProps { | ||
className?: string; | ||
playing: boolean; | ||
lastTechnique?: Technique; | ||
onWaitDone: () => void; | ||
} | ||
|
||
const LENGTH_OF_VIDEO_INTRO_SECONDS = 6; | ||
|
||
const speeds: Record<string, number> = { | ||
slow: 1000, | ||
medium: 750, | ||
fast: 500, | ||
}; | ||
|
||
export const AutoPlay: React.FC<AutoPlayProps> = ({ className, playing, lastTechnique, onWaitDone }) => { | ||
const classes = [className, css.autoplay]; | ||
|
||
const [mode, setMode] = useState("no"); | ||
|
||
const duration = useMemo(() => { | ||
if (lastTechnique == null) return null; | ||
return videoDurationSeconds(lastTechnique); | ||
}, [lastTechnique]); | ||
|
||
const { progressPercent } = useCountDown(mode !== "no" && !playing, duration, speeds[mode], onWaitDone); | ||
|
||
return ( | ||
<Alert variant={"secondary"} className={classes.join(" ")}> | ||
<div className={css.progress} style={{ width: `${progressPercent}%`, transitionDuration: speeds[mode] + "ms" }}> | ||
{" "} | ||
</div> | ||
<Form.Group as={Row}> | ||
<Form.Label column xs={4}> | ||
Autoplay | ||
</Form.Label> | ||
<Col xs={8}> | ||
<Form.Select | ||
aria-label="Default select example" | ||
value={mode} | ||
onChange={(event) => setMode(event.target.value)} | ||
> | ||
<option value="no">Aus</option> | ||
<option value="slow">Langsam</option> | ||
<option value="medium">Mittel</option> | ||
<option value="fast">Schnell</option> | ||
</Form.Select> | ||
</Col> | ||
</Form.Group> | ||
</Alert> | ||
); | ||
}; | ||
|
||
const FALLBACK_DURATION_SECONDS = 20; | ||
|
||
function videoDurationSeconds(technique: Technique): number { | ||
const youtube = technique.metadata.youtube; | ||
if (youtube == null) { | ||
return FALLBACK_DURATION_SECONDS; | ||
} | ||
if (Array.isArray(youtube)) { | ||
return youtube[0].durationSeconds - LENGTH_OF_VIDEO_INTRO_SECONDS; | ||
} | ||
return youtube.durationSeconds - LENGTH_OF_VIDEO_INTRO_SECONDS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export function useCountDown(run: boolean, startFrom: null | number, waitMillis: number, onDone: () => void) { | ||
const [counter, setCounter] = useState<number>(0); | ||
const [progressPercent, setProgressPercent] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
if (run && startFrom != null && startFrom > 0) { | ||
let myCounter = startFrom; | ||
setCounter(startFrom); | ||
setProgressPercent(0); | ||
const interval = setInterval(() => { | ||
myCounter--; | ||
setProgressPercent(Math.ceil(((startFrom - myCounter) / startFrom) * 100)); | ||
setCounter(myCounter); | ||
if (myCounter <= 0) { | ||
clearInterval(interval); | ||
onDone(); | ||
} | ||
}, waitMillis); | ||
return () => { | ||
clearInterval(interval); | ||
}; | ||
} | ||
}, [run, startFrom, onDone, waitMillis]); | ||
|
||
return { | ||
counter, | ||
progressPercent, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters