Skip to content

Commit

Permalink
Don't repeat a splash until they've seen all of them
Browse files Browse the repository at this point in the history
  • Loading branch information
owenmoogk committed Jan 18, 2025
1 parent ac12556 commit b0ccf27
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/components/homepage/Homepage.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Anchor, Box, Container, Text, Title } from '@mantine/core';
import { useState } from 'react';
import { useRef, useState } from 'react';
import { Helmet } from 'react-helmet-async';
import { Link } from 'react-router';

import splashes from './splashes.json';
import FeaturedIcon from '../projects/FeaturedIcon';
import { assetUrl } from '@global/global';

export function getRandomInt(min: number, max: number) {
function getRandomIntInclusive(min: number, max: number) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
Expand All @@ -16,11 +16,19 @@ export function getRandomInt(min: number, max: number) {
export default function Homepage() {
const defaultSplash = 'Mechatronics Engineering Student';
const [splash, setSplash] = useState(defaultSplash);
const alreadyShownSet = useRef(new Set<number>());

function changeSplash() {
setSplash(
splashes ? splashes[getRandomInt(0, splashes.length)] : defaultSplash
);
if (alreadyShownSet.current.size == splashes.length) {
alreadyShownSet.current.clear();
}

let randomInt: number | null = null;
while (randomInt == null || alreadyShownSet.current.has(randomInt)) {
randomInt = getRandomIntInclusive(0, splashes.length - 1);
}
alreadyShownSet.current.add(randomInt);
setSplash(splashes ? splashes[randomInt] : defaultSplash);
}

return (
Expand Down

0 comments on commit b0ccf27

Please sign in to comment.