Skip to content

Commit

Permalink
Fix for clean param (#9762)
Browse files Browse the repository at this point in the history
  • Loading branch information
eanders-ms authored Oct 30, 2023
1 parent 36d2dee commit 3cac786
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 26 deletions.
6 changes: 3 additions & 3 deletions kiosk/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ function App() {
if (cfg) {
dispatch(Actions.setTargetConfig(cfg));
if (cfg.kiosk) {
dispatch(Actions.setGameList(cfg.kiosk.games));
if (!state.clean) {
dispatch(Actions.setGameList(cfg.kiosk.games));
}
// Load user-added games from local storage.
dispatch(Actions.loadUserAddedGames());
// Select the first game in the list.
dispatch(Actions.setSelectedGameId(cfg.kiosk.games[0].id));
}
} else {
// TODO: Handle this better
Expand Down
11 changes: 7 additions & 4 deletions kiosk/src/Components/GameList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const GameList: React.FC<IProps> = ({}) => {
const [userInitiatedTransition, setUserInitiatedTransition] =
React.useState(false);
const [pageInited, setPageInited] = React.useState(false);
const [generation, setGeneration] = React.useState(0);

const handleLocalSwiper = (swiper: SwiperClass) => {
setLocalSwiper(swiper);
Expand Down Expand Up @@ -116,11 +117,12 @@ const GameList: React.FC<IProps> = ({}) => {
};

const slideChangeTransitionEnd = () => {
setGeneration(generation + 1); // This will force GameSlides to re-render
if (userInitiatedTransition) {
syncSelectedGame();
setUserInitiatedTransition(false);
}
}
};

if (!kiosk.allGames?.length) {
return (
Expand All @@ -134,7 +136,6 @@ const GameList: React.FC<IProps> = ({}) => {
<div className="carouselWrap">
<Swiper
focusableElements="div"
slideActiveClass="swiper-slide-active"
tabIndex={0}
loop={true}
slidesPerView={1.8}
Expand All @@ -143,14 +144,16 @@ const GameList: React.FC<IProps> = ({}) => {
onSwiper={handleLocalSwiper}
allowTouchMove={true}
modules={[Pagination]}
onSlideChangeTransitionStart={() => slideChangeTransitionStart()}
onSlideChangeTransitionStart={() =>
slideChangeTransitionStart()
}
onSlideChangeTransitionEnd={() => slideChangeTransitionEnd()}
onTouchStart={() => setUserInitiatedTransition(true)}
>
{kiosk.allGames.map((game, index) => {
return (
<SwiperSlide key={index}>
<GameSlide game={game} />
<GameSlide game={game} generation={generation} />
</SwiperSlide>
);
})}
Expand Down
38 changes: 31 additions & 7 deletions kiosk/src/Components/GameSlide.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useMemo } from "react";
import { useContext, useMemo, useState } from "react";
import { playSoundEffect } from "../Services/SoundEffectService";
import { launchGame } from "../Transforms/launchGame";
import { GameData } from "../Types";
Expand All @@ -10,9 +10,35 @@ import { getEffectiveGameId } from "../Utils";

interface IProps {
game: GameData;
generation: number; // Enables GameList to force a re-render of GameSlide when carousel transitions
}
const GameSlide: React.FC<IProps> = ({ game }) => {
const GameSlide: React.FC<IProps> = ({ game, generation }) => {
const { state: kiosk } = useContext(AppStateContext);
const [myParentRef, setMyParentRef] = useState<
HTMLElement | null | undefined
>(null);

const handleRef = (el: HTMLElement | null) => {
setMyParentRef(el?.parentElement);
};

const isSelected = useMemo(() => {
let selected = kiosk.selectedGameId === game.id;
if (myParentRef) {
selected =
selected &&
myParentRef.classList.contains("swiper-slide-active");
}
return selected;
}, [
kiosk.selectedGameId,
game.id,
myParentRef,
// `generation` is included here to force re-render when carousel
// transitions. This is a workaround for Swiper's direct DOM
// manipulation .. React doesn't always know about it.
generation,
]);

const handleSlideClick = () => {
pxt.tickEvent("kiosk.gameLaunched", { game: game.id });
Expand All @@ -28,7 +54,7 @@ const GameSlide: React.FC<IProps> = ({ game }) => {
}, [game, game?.tempGameId]);

return (
<div className="gameTile" onClick={handleSlideClick}>
<div className="gameTile" onClick={handleSlideClick} ref={handleRef}>
<div className="gameSelectionIndicator" />
<div
className="gameThumbnail"
Expand All @@ -49,13 +75,11 @@ const GameSlide: React.FC<IProps> = ({ game }) => {
)}
</div>

{kiosk.selectedGameId && game.id === kiosk.selectedGameId && (
{isSelected && (
<div className="pressStart">{lf("Press A to Start")}</div>
)}

{kiosk.selectedGameId && game.id === kiosk.selectedGameId && (
<GameMenu />
)}
{isSelected && <GameMenu />}
</div>
);
};
Expand Down
21 changes: 14 additions & 7 deletions kiosk/src/Kiosk.css
Original file line number Diff line number Diff line change
Expand Up @@ -552,19 +552,26 @@ h2 {

.swiper-slide {
position: relative;
width: var(--swiper-width);
transform: scale(0.75);
top: -10%;
}

.swiper-slide-active {
cursor: pointer;
transform: scale(1) !important;
top: 0;
.swiper-slide.swiper-slide-next {
position: relative;
transform: scale(0.75) !important;
top: -10%;
}

.swiper-slide.swiper-slide-prev {
position: relative;
transform: scale(0.75) !important;
top: -10%;
}

.swiper-slide-active .deleted {
animation: 1s shrink;
.swiper-slide.swiper-slide-active {
cursor: pointer;
transform: scale(1);
top: 0;
}

.gameTile {
Expand Down
2 changes: 1 addition & 1 deletion kiosk/src/State/Reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function reducer(state: AppState, action: Action): AppState {
if (state.selectedGameId === action.gameId) {
// Get the index of the now-deleted game in the original list.
const selectedGameIndex = state.allGames.findIndex(g => g.id === action.gameId);
if (selectedGameIndex >= 0) {
if (selectedGameIndex >= 0 && remainingGames.length) {
if (selectedGameIndex > remainingGames.length - 1) {
// The index of the deleted game is beyond the bounds of the updated list. Select the last game in the updated list.
selectedGameId = remainingGames[remainingGames.length - 1].id;
Expand Down
8 changes: 4 additions & 4 deletions kiosk/src/State/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export type AppState = {
kioskCodeExpiration?: number;
notifications: Notifications;
modal?: ModalConfig;
clean: boolean;
locked: boolean;
time?: string;
volume?: number;
clean: boolean; // if true, don't load built-in games
locked: boolean; // if true, hide the add games button
time?: string; // lifetime of kiosk code, in minutes
volume?: number; // volume level of kiosk UI sounds, in [0..1]
targetConfig?: pxt.TargetConfig;
};

Expand Down

0 comments on commit 3cac786

Please sign in to comment.