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 16:02:26 -04:00
|
|
|
import HistoryPopup from "./components/HistoryPopup";
|
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[]>([]);
|
2024-09-09 16:02:26 -04:00
|
|
|
const [isHistoryOpen, setIsHistoryOpen] = useState(false);
|
2024-09-09 11:51:54 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2024-09-09 16:02:26 -04:00
|
|
|
const toggleHistory = () => {
|
|
|
|
setIsHistoryOpen(!isHistoryOpen);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAllSchemes = () => {
|
|
|
|
const allSchemes = [...likedSchemes, ...dislikedSchemes];
|
|
|
|
const uniqueSchemes = allSchemes.filter((scheme, index, self) =>
|
|
|
|
index === self.findIndex((t) => t.name === scheme.name)
|
|
|
|
);
|
|
|
|
return uniqueSchemes.reverse(); // Most recent first
|
|
|
|
};
|
|
|
|
|
2024-09-09 11:51:54 -04:00
|
|
|
return (
|
2024-09-09 16:02:26 -04:00
|
|
|
<div className="h-screen w-screen overflow-hidden font-[family-name:var(--font-geist-sans)] dark:bg-gray-900 dark:text-white transition-colors duration-300">
|
|
|
|
<header className="absolute top-4 left-4 z-10">
|
2024-09-09 15:31:19 -04:00
|
|
|
<Image src="/app-icon.svg" alt="App Icon" width={40} height={40} />
|
2024-09-09 11:51:54 -04:00
|
|
|
</header>
|
2024-09-09 16:02:26 -04:00
|
|
|
<button
|
|
|
|
className="absolute top-4 right-4 z-10"
|
|
|
|
onClick={toggleHistory}
|
|
|
|
>
|
|
|
|
<Image src={isDarkMode ? "/history-icon-dark.svg" : "/history-icon-light.svg"} alt="History" width={32} height={32} />
|
|
|
|
</button>
|
|
|
|
<main className="flex flex-col items-center justify-center h-full">
|
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>
|
2024-09-09 16:02:26 -04:00
|
|
|
{isHistoryOpen && (
|
|
|
|
<HistoryPopup
|
|
|
|
likedSchemes={likedSchemes}
|
|
|
|
dislikedSchemes={dislikedSchemes}
|
|
|
|
onClose={toggleHistory}
|
|
|
|
isDarkMode={isDarkMode}
|
|
|
|
/>
|
|
|
|
)}
|
2024-09-09 09:50:12 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|