better life handling

This commit is contained in:
Viktor Rådberg
2023-09-22 13:13:29 +02:00
parent d7d9d26540
commit 4157ef53c1
15 changed files with 197 additions and 268 deletions

View File

@@ -1,10 +1,9 @@
import { useState, useEffect } from 'react';
import { useWakeLock } from 'react-screen-wake-lock';
import { useEffect, useState } from 'react';
import styled from 'styled-components';
import { InitialSettings } from '../Data/getInitialPlayers';
import { useAnalytics } from '../Hooks/useAnalytics';
import Play from './Views/Play';
import StartMenu from './Views/StartMenu/StartMenu';
import { useGlobalSettings } from '../Hooks/useGlobalSettings';
const StartWrapper = styled.div`
max-width: fit-content;
@@ -32,28 +31,14 @@ const EmergencyResetButton = styled.button`
`;
export const LifeTrinket = () => {
const analytics = useAnalytics();
const savedGameSettings = localStorage.getItem('initialGameSettings');
const savedShowPlay = localStorage.getItem('showPlay');
const { isSupported, release, released, request, type } = useWakeLock();
const { showPlay, goToStart } = useGlobalSettings();
const [initialGameSettings, setInitialGameSettings] =
useState<InitialSettings | null>(
savedGameSettings ? JSON.parse(savedGameSettings) : null
);
const [showPlay, setShowPlay] = useState<boolean>(
savedShowPlay ? savedShowPlay === 'true' : false
);
const isActive = released === undefined ? false : !released;
const wakeLock = {
isSupported,
release,
active: isActive,
request,
type,
};
useEffect(() => {
localStorage.setItem(
@@ -62,36 +47,11 @@ export const LifeTrinket = () => {
);
}, [initialGameSettings]);
const removeLocalStorage = async () => {
localStorage.removeItem('initialGameSettings');
localStorage.removeItem('players');
localStorage.removeItem('playing');
localStorage.removeItem('showPlay');
setShowPlay(localStorage.getItem('showPlay') === 'true' ?? false);
};
const goToStart = async () => {
// this function is broken for the moment, need to set players object
const currentPlayers = localStorage.getItem('players');
if (currentPlayers) {
analytics.trackEvent('go_to_start', {
playersBeforeReset: currentPlayers,
});
}
await removeLocalStorage();
// setPlayers([]);
};
return (
<>
{showPlay && initialGameSettings ? (
<PlayWrapper>
<Play
gridAreas={initialGameSettings?.gridAreas}
goToStart={goToStart}
wakeLock={wakeLock}
/>
<Play gridAreas={initialGameSettings?.gridAreas} />
<EmergencyResetButton onClick={goToStart}>
<p>If you can see this, something is wrong.</p>
<p>Press screen to go to start.</p>
@@ -104,8 +64,6 @@ export const LifeTrinket = () => {
<StartMenu
initialGameSettings={initialGameSettings}
setInitialGameSettings={setInitialGameSettings}
wakeLock={wakeLock}
setShowPlay={setShowPlay}
/>
</StartWrapper>
)}