TerminalTinder/app/page.tsx

111 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-09-09 11:51:54 -04:00
'use client';
import React, { useState, useEffect } from 'react';
2024-09-09 15:31:19 -04:00
import Image from 'next/image';
2024-09-09 11:51:54 -04:00
import ColorSchemeCard from "./components/ColorSchemeCard";
2024-09-09 13:31:32 -04:00
import { ColorScheme, knownSchemes, generateRandomScheme, generateSchemeFromGeneticAlgorithm } from './utils/colorSchemes';
import { AnimatePresence } from 'framer-motion';
2024-09-09 09:50:12 -04:00
export default function Home() {
2024-09-09 11:51:54 -04:00
const [schemes, setSchemes] = useState<ColorScheme[]>([]);
const [isDarkMode, setIsDarkMode] = useState(false);
const [likedSchemes, setLikedSchemes] = useState<ColorScheme[]>([]);
const [dislikedSchemes, setDislikedSchemes] = useState<ColorScheme[]>([]);
useEffect(() => {
2024-09-09 13:31:32 -04:00
generateNewSchemes(8);
2024-09-09 11:51:54 -04:00
setIsDarkMode(window.matchMedia('(prefers-color-scheme: dark)').matches);
const storedLikedSchemes = localStorage.getItem('likedSchemes');
const storedDislikedSchemes = localStorage.getItem('dislikedSchemes');
if (storedLikedSchemes) {
setLikedSchemes(JSON.parse(storedLikedSchemes));
}
if (storedDislikedSchemes) {
setDislikedSchemes(JSON.parse(storedDislikedSchemes));
}
}, []);
useEffect(() => {
document.documentElement.classList.toggle('dark', isDarkMode);
}, [isDarkMode]);
useEffect(() => {
localStorage.setItem('likedSchemes', JSON.stringify(likedSchemes));
}, [likedSchemes]);
useEffect(() => {
localStorage.setItem('dislikedSchemes', JSON.stringify(dislikedSchemes));
}, [dislikedSchemes]);
2024-09-09 09:50:12 -04:00
2024-09-09 13:31:32 -04:00
const generateNewSchemes = (count: number) => {
2024-09-09 15:31:19 -04:00
const newSchemes = Array(count).fill(null).map(() => {
const schemeType = Math.random();
if (schemeType < 0.25) {
// 25% chance of known scheme
return knownSchemes[Math.floor(Math.random() * knownSchemes.length)];
} else if (schemeType < 0.5) {
// 25% chance of random scheme
return generateRandomScheme();
} else if (likedSchemes.length > 2 && dislikedSchemes.length > 2) {
// 50% chance of genetic scheme if there are more than 2 liked and disliked schemes
return generateSchemeFromGeneticAlgorithm(likedSchemes, dislikedSchemes);
} else {
// 30% known, 20% random for other cases
if (Math.random() < 0.3) {
return generateRandomScheme();
} else {
return knownSchemes[Math.floor(Math.random() * knownSchemes.length)];
}
}
});
2024-09-09 13:31:32 -04:00
2024-09-09 15:31:19 -04:00
setSchemes(prevSchemes => [...prevSchemes, ...newSchemes]);
2024-09-09 11:51:54 -04:00
};
2024-09-09 13:31:32 -04:00
const handleLike = (scheme: ColorScheme) => {
setLikedSchemes(prev => [...prev, scheme]);
removeTopScheme();
2024-09-09 11:51:54 -04:00
};
2024-09-09 13:31:32 -04:00
const handleDislike = (scheme: ColorScheme) => {
setDislikedSchemes(prev => [...prev, scheme]);
removeTopScheme();
};
const removeTopScheme = () => {
setSchemes(prevSchemes => {
const newSchemes = prevSchemes.slice(1);
if (newSchemes.length < 3) {
generateNewSchemes(3);
}
return newSchemes;
});
2024-09-09 11:51:54 -04:00
};
const toggleDarkMode = () => {
setIsDarkMode(prev => !prev);
};
return (
2024-09-09 15:31:19 -04:00
<div className="h-screen w-screen overflow-hidden p-8 font-[family-name:var(--font-geist-sans)] dark:bg-gray-900 dark:text-white transition-colors duration-300">
<header className="flex items-center mb-8">
<Image src="/app-icon.svg" alt="App Icon" width={40} height={40} />
2024-09-09 11:51:54 -04:00
</header>
2024-09-09 15:31:19 -04:00
<main className="flex flex-col items-center justify-center h-[calc(100vh-100px)]">
2024-09-09 13:31:32 -04:00
<AnimatePresence>
{schemes.slice(0, 3).map((scheme, index) => (
2024-09-09 11:51:54 -04:00
<ColorSchemeCard
2024-09-09 13:31:32 -04:00
key={`${scheme.name}-${index}`}
2024-09-09 11:51:54 -04:00
scheme={scheme}
2024-09-09 13:31:32 -04:00
onLike={() => handleLike(scheme)}
onDislike={() => handleDislike(scheme)}
index={index}
2024-09-09 15:31:19 -04:00
isDarkMode={isDarkMode}
2024-09-09 09:50:12 -04:00
/>
2024-09-09 11:51:54 -04:00
))}
2024-09-09 13:31:32 -04:00
</AnimatePresence>
2024-09-09 09:50:12 -04:00
</main>
</div>
);
}