interstellar_ai/app/components/Models.tsx

113 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-09-26 09:45:31 +02:00
import React, { useState, useEffect } from 'react';
2024-09-18 10:03:36 +02:00
2024-09-26 09:45:31 +02:00
// Define the available model options
2024-09-26 12:31:56 +02:00
const modelDropdown = {
offlineModels: [
'Offline Fast',
'Offline Fast (FOSS)',
'Offline Slow',
'Offline Slow (FOSS)',
],
2024-09-25 12:53:54 +02:00
2024-09-26 12:31:56 +02:00
onlineModels: [
'Online (La Plateforme)',
'Online (FOSS) (La Plateforme)',
'Online Cheap (OpenAI)',
'Online Expensive (OpenAI)',
'Online Cheap (Anthropic)',
'Online Expensive (Anthropic)',
'Online Cheap (Google)',
'Online Expensive (Google)',
],
2024-09-25 13:33:51 +02:00
2024-09-26 12:31:56 +02:00
fossModels: [
'Offline Fast (FOSS)',
'Offline Slow (FOSS)',
'Online (FOSS) (La Plateforme)',
],
};
const Models: React.FC = () => {
// Initialize state with value from localStorage or default to ''
2024-09-26 16:09:45 +02:00
const [radioSelection, setRadioSelection] = useState('');
2024-09-27 09:54:38 +02:00
2024-09-26 12:31:56 +02:00
useEffect(() => {
const handleStorageChange = () => {
setRadioSelection(localStorage.getItem('radioSelection') || '');
};
2024-09-27 09:54:38 +02:00
handleStorageChange();
2024-09-25 12:53:54 +02:00
2024-09-26 12:31:56 +02:00
// Update dropdown immediately when localStorage changes internally or externally
window.addEventListener('storage', handleStorageChange);
// Cleanup listener on component unmount
return () => {
window.removeEventListener('storage', handleStorageChange);
};
}, []);
2024-09-25 13:33:51 +02:00
2024-09-26 09:45:31 +02:00
const handleModelChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const newModel = event.target.value;
2024-09-26 12:31:56 +02:00
setRadioSelection(newModel);
localStorage.setItem('radioSelection', newModel); // Update localStorage directly
2024-09-26 09:45:31 +02:00
};
2024-09-26 09:29:04 +02:00
2024-09-26 12:31:56 +02:00
// Determine the filtered models based on current radioSelection
2024-09-26 09:45:31 +02:00
const filteredModels = (() => {
2024-09-27 09:54:38 +02:00
let models = [];
2024-09-26 12:31:56 +02:00
switch (radioSelection) {
2024-09-26 09:45:31 +02:00
case 'Offline':
2024-09-27 09:54:38 +02:00
models = modelDropdown.offlineModels; // Show only offline models
break;
2024-09-26 09:45:31 +02:00
case 'AI Online':
2024-09-27 09:54:38 +02:00
models = modelDropdown.onlineModels; // Show only online models
break;
2024-09-26 12:31:56 +02:00
case 'FOSS':
2024-09-27 09:54:38 +02:00
models = modelDropdown.fossModels; // Show only FOSS models
break;
2024-09-26 09:45:31 +02:00
default:
2024-09-27 09:54:38 +02:00
models = [...modelDropdown.offlineModels, ...modelDropdown.onlineModels, ...modelDropdown.fossModels]; // Show all models if nothing matches
break;
2024-09-26 09:45:31 +02:00
}
2024-09-27 09:54:38 +02:00
return Array.from(new Set(models)); // Remove duplicates using Set
2024-09-26 09:45:31 +02:00
})();
2024-09-26 09:29:04 +02:00
2024-09-26 12:31:56 +02:00
const isOfflineModel = (model: string) => modelDropdown.offlineModels.includes(model);
2024-09-26 09:45:31 +02:00
return (
<div className="model-background">
2024-09-27 09:54:38 +02:00
<div className="models">
<div className="title">
<h3>Different AI Models</h3>
</div>
2024-09-26 09:29:04 +02:00
2024-09-27 09:54:38 +02:00
{/* Model Selection Dropdown */}
<div className="model-dropdown">
<label htmlFor="model-select">Select AI Model:</label>
<select id="model-select" value={radioSelection} onChange={handleModelChange}>
{filteredModels.map((model) => (
<option key={model} value={model}>
{model}
</option>
))}
</select>
</div>
2024-09-26 09:45:31 +02:00
2024-09-27 09:54:38 +02:00
{/* Model Grid with Cards */}
<div className="grid">
{['Code', 'Math', 'Language', 'Character', 'Finance', 'Weather', 'Time', 'Image', 'Custom1', 'Custom2'].map((category) => (
<button key={category} className={`${category.toLowerCase()}-model model-box`}>
<div className="overlay">
<h3>{category}</h3>
{isOfflineModel(radioSelection) && <img src="/img/nowifi.svg" alt="No Wi-Fi" />}
</div>
</button>
))}
</div>
2024-09-18 10:03:36 +02:00
</div>
2024-09-26 09:45:31 +02:00
</div>
);
};
2024-09-18 10:03:36 +02:00
2024-09-26 09:45:31 +02:00
export default Models;