Skip to content

Commit

Permalink
fix: each car now has its own state
Browse files Browse the repository at this point in the history
  • Loading branch information
longtv2222 committed Dec 20, 2023
1 parent ceca236 commit c4b974d
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions prototype/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Lamborghini from "./components/Models/Lamborghini"
import Scene from "./components/Models/Autobianchi Stellina"
import Maserati from "./components/Models/Maserati_mc20"
import { Leva, levaStore, useControls, button } from 'leva'
import { Suspense } from 'react'
import { Suspense, useEffect, useRef, useState } from 'react'
import { Model } from './components/Models/model'

export default function App() {
Expand All @@ -30,6 +30,9 @@ export default function App() {
},
};

const [carsState, setCarsState] = useState(() => carNameComponentMap);
const carsStateRef = useRef(carsState);

const resetCarColor = () => {
const model: string = levaStore.get("Select");

Check failure on line 37 in prototype/src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an `any` value
set({
Expand All @@ -38,18 +41,50 @@ export default function App() {
});
};

const [{ Interior, Exterior, Rotation, Stats: stats }, set] = useControls(() => ({
Select: {
const setCarInterior = (interior: string) => {
const model: string = levaStore.get("Select");

Check failure on line 45 in prototype/src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an `any` value
setCarsState({
...carsStateRef.current,
[model]: {
...carsStateRef.current[model]!,
interior
}
})
};

const setCarExterior = (exterior: string) => {
const model: string = levaStore.get("Select");

Check failure on line 56 in prototype/src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an `any` value
setCarsState({
...carsStateRef.current,
[model]: {
...carsStateRef.current[model]!,
exterior
}
})
};

useEffect(() => {
carsStateRef.current = carsState;
}, [carsState]);

const [{ Rotation, Stats: stats }, set] = useControls(() => ({
Select: {
options: Object.keys(carNameComponentMap),
onChange:(value)=>{
onChange: (value) => {
set({
Exterior: carNameComponentMap[value]?.exterior,
Interior: carNameComponentMap[value]?.interior,
Exterior: carsStateRef.current[value]?.exterior,

Check failure on line 75 in prototype/src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

Computed name [value] resolves to an any value
Interior: carsStateRef.current[value]?.interior,

Check failure on line 76 in prototype/src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

Computed name [value] resolves to an any value
});
}
},
Interior: '#000000',
Exterior: '#9a9898',
Interior: {
value: '#000000',
onChange: setCarInterior
},
Exterior: {
value: '#9a9898',
onChange: setCarExterior
},
Rotation: false,
Stats: true,
"Reset Color": button(resetCarColor),
Expand All @@ -64,8 +99,8 @@ export default function App() {
{Object.entries(carNameComponentMap)
.map(([name, car]) => (
car.Model({
exterior: Exterior,
interior: Interior,
exterior: carsState[name]?.exterior!,

Check failure on line 102 in prototype/src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong
interior: carsState[name]?.interior!,

Check failure on line 103 in prototype/src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong
visible: levaStore.get("Select") === name,
})
))}
Expand Down

0 comments on commit c4b974d

Please sign in to comment.