before ai
This commit is contained in:
1 parent
85d0d0be57
commit
be50463157
11 files changed
+205
-76
No files matched your search
@@ -1,28 +1,32 @@
|
||||
"use client";
|
||||
|
||||
import { CSSProperties, ReactNode, useRef, useState } from "react";
|
||||
import styles from "@/css/ChargedButton.module.css";
|
||||
|
||||
interface ChargedButtonProps {
|
||||
children?: ReactNode;
|
||||
style?: CSSProperties;
|
||||
className?: string;
|
||||
/** Time in milliseconds needed to fully charge by continuous hold. */
|
||||
holdDuration?: number;
|
||||
/** Runs exactly once, when the button is fully charged at 100%. */
|
||||
onAction?: () => void;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
export default function ChargedButton({
|
||||
children,
|
||||
style,
|
||||
className,
|
||||
holdDuration = 1000,
|
||||
onAction,
|
||||
disabled = false,
|
||||
}: ChargedButtonProps) {
|
||||
const [progress, setProgress] = useState(0);
|
||||
const [isCharging, setIsCharging] = useState(false);
|
||||
const [isDischarging, setIsDischarging] = useState(false);
|
||||
const rafRef = useRef(0);
|
||||
const startTimeRef = useRef(0);
|
||||
const stateRef = useRef({ charging: false, discharging: false });
|
||||
const dischargingRef = useRef(false);
|
||||
|
||||
const cancelRaf = () => {
|
||||
@@ -33,7 +37,7 @@ export default function ChargedButton({
|
||||
};
|
||||
|
||||
const onPointerDown = () => {
|
||||
if (dischargingRef.current) return;
|
||||
if (disabled || dischargingRef.current) return;
|
||||
cancelRaf();
|
||||
startTimeRef.current = performance.now();
|
||||
setIsCharging(true);
|
||||
@@ -43,7 +47,7 @@ export default function ChargedButton({
|
||||
};
|
||||
|
||||
const onPointerUp = () => {
|
||||
if (dischargingRef.current) return;
|
||||
if (disabled || dischargingRef.current) return;
|
||||
cancelRaf();
|
||||
setIsCharging(false);
|
||||
setProgress(0);
|
||||
@@ -65,7 +69,7 @@ export default function ChargedButton({
|
||||
onAction?.();
|
||||
}
|
||||
} else {
|
||||
const fraction = Math.min(1, elapsed / (holdDuration * 0.1));
|
||||
const fraction = Math.min(1, elapsed / Math.max(holdDuration * 0.05, 150));
|
||||
setProgress(1.0 - fraction);
|
||||
if (fraction >= 1) {
|
||||
setProgress(0);
|
||||
@@ -82,7 +86,7 @@ export default function ChargedButton({
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
className="charged-button"
|
||||
className={styles["charged-button"] + ((disabled) ? " " + styles["button-disabled"] : "") + (className ? " " + className : "")}
|
||||
style={style}
|
||||
onPointerDown={onPointerDown}
|
||||
onTouchStart={onPointerDown}
|
||||
@@ -91,12 +95,14 @@ export default function ChargedButton({
|
||||
onPointerCancel={onPointerUp}
|
||||
onTouchCancel={onPointerUp}
|
||||
>
|
||||
<span className="charged-button__label">{children}</span>
|
||||
<span className={styles["charged-button-label"]}>{children}</span>
|
||||
<span
|
||||
className=" charged-button-fill"
|
||||
className={styles["charged-button-fill"]}
|
||||
aria-hidden="true"
|
||||
style={{ width: `${progress * 100}%` }}
|
||||
style={{
|
||||
clipPath: `inset(0 ${100 - progress * 100}% 0 0)`,
|
||||
}}
|
||||
/>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
|
||||
export default function PermissionModal() {
|
||||
return (
|
||||
<div className="">
|
||||
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,88 @@
|
||||
"use client";
|
||||
|
||||
import { ChangeEvent, SyntheticEvent, useEffect, useRef, useState } from "react";
|
||||
import styles from "../../css/WelcomeScreen.module.css";
|
||||
import ChargedButton from "../ChargedButton";
|
||||
|
||||
const CODE_LENGTH = 9;
|
||||
const COOLDOWN_MS = 3000;
|
||||
|
||||
export default function WelcomeScreen() {
|
||||
console.log(styles);
|
||||
return(
|
||||
<div className="screen-container">
|
||||
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const [tourCode, setTourCode] = useState("");
|
||||
const [lastSubmitTs, setLastSubmitTs] = useState(0);
|
||||
const [isCooldown, setIsCooldown] = useState(false);
|
||||
const tourCodeRef = useRef("");
|
||||
const cooldownRef = useRef(-1);
|
||||
|
||||
const checkCooldown = () => {
|
||||
if (cooldownRef.current === -1) return false;
|
||||
return Date.now() - cooldownRef.current <= COOLDOWN_MS
|
||||
};
|
||||
|
||||
const isTourCodeValid = () => {
|
||||
return tourCode.length === CODE_LENGTH;
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (checkCooldown()) {
|
||||
setIsCooldown(true);
|
||||
const inputTimeout = setTimeout(() => {
|
||||
console.log("Cooldown over");
|
||||
setIsCooldown(false);
|
||||
}, 3000);
|
||||
return () => clearTimeout(inputTimeout);
|
||||
} else {
|
||||
setIsCooldown(false);
|
||||
}
|
||||
}, [lastSubmitTs]);
|
||||
|
||||
const submit = () => {
|
||||
const value = tourCodeRef.current;
|
||||
|
||||
if (!isTourCodeValid() && checkCooldown()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cooldownRef.current = Date.now();
|
||||
setLastSubmitTs(cooldownRef.current);
|
||||
|
||||
console.log("submitted", value);
|
||||
};
|
||||
|
||||
const handleChange = (event: ChangeEvent<HTMLInputElement>): boolean => {
|
||||
const value = event.target.value;
|
||||
|
||||
if (value.length > CODE_LENGTH) {
|
||||
event.preventDefault();
|
||||
return false;
|
||||
}
|
||||
tourCodeRef.current = value;
|
||||
setTourCode(value.toUpperCase().replaceAll(/\W/g, ""));
|
||||
return true;
|
||||
};
|
||||
|
||||
const buttonDisabled = isCooldown || !isTourCodeValid();
|
||||
|
||||
return (
|
||||
<div className="screen-container">
|
||||
<label className="screen__label">
|
||||
<input
|
||||
type="text"
|
||||
className="screen-input"
|
||||
value={tourCode}
|
||||
onChange={handleChange}
|
||||
aria-label="Tour Code"
|
||||
placeholder={`Enter a tour code (${CODE_LENGTH} characters max)`}
|
||||
/>
|
||||
</label>
|
||||
<ChargedButton
|
||||
holdDuration={1000}
|
||||
onAction={submit}
|
||||
className="screen-submit"
|
||||
disabled={buttonDisabled}
|
||||
>
|
||||
{(!buttonDisabled) ? "Halten zum Spielen!" : "Halten zum Spielen."}
|
||||
</ChargedButton>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user