Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share scenario modulation #29

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testMatch: ["**/*.test.ts", "**/*.test.tsx"],
};
17,084 changes: 11,318 additions & 5,766 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"predeploy": "npm run build",
"deploy": "gh-pages -d dist",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
"build-storybook": "storybook build",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage"
},
"dependencies": {
"@ant-design/icons": "^5.3.0",
Expand All @@ -39,6 +42,7 @@
"@storybook/react": "^8.4.4",
"@storybook/react-webpack5": "^8.4.4",
"@storybook/test": "^8.4.4",
"@types/jest": "^29.5.14",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
"@vitejs/plugin-react": "^4.2.0",
Expand All @@ -49,14 +53,18 @@
"eslint-plugin-storybook": "^0.11.0",
"gh-pages": "^6.2.0",
"globals": "^15.11.0",
"jest": "^29.7.0",
"sass": "^1.81.0",
"sass-embedded": "^1.81.0",
"sass-loader": "^16.0.3",
"storybook": "^8.4.4",
"style-loader": "^4.0.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.0.0",
"typescript-eslint": "^8.15.0",
"vite": "^5.0.0"
"vite": "^5.0.0",
"vitest": "^2.1.6"
},
"eslintConfig": {
"extends": [
Expand Down
11 changes: 3 additions & 8 deletions src/components/GravitySimulator/GravitySimulator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useCallback, useEffect } from "react";
import { Point2D, GravityPoint, Force } from "../../utils/types/physics";
import { Point2D, GravityPoint } from "../../utils/types/physics";
import { GravityPointComponent } from "../GravityPoint/GravityPoint";
import { ParticleRenderer } from "../ParticleRenderer/ParticleRenderer";
import { StarPalette } from "../StarPalette/StarPalette";
Expand Down Expand Up @@ -45,12 +45,6 @@
className?: string;
}

interface SimulationScenario {
settings: typeof physicsConfig;
gravityPoints: GravityPoint[];
particles: Array<Omit<Particle, "trails" | "force">>;
}

export const GravitySimulator: React.FC<GravitySimulatorProps> = ({
gravityRef,
pointerPos,
Expand Down Expand Up @@ -103,7 +97,7 @@
}, 0);
});

const handleReportNewPosition = useCallback(

Check warning on line 100 in src/components/GravitySimulator/GravitySimulator.tsx

View workflow job for this annotation

GitHub Actions / build

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead
throttle((point: Point2D, index: number) => {
const offset = getContainerOffset(gravityRef);
if (!offset) return;
Expand Down Expand Up @@ -313,7 +307,6 @@
y,
label: template.label,
mass: template.mass,
color: template.color,
},
]);
}
Expand Down Expand Up @@ -348,6 +341,7 @@
settings: physicsConfig,
gravityPoints,
particles: particles.map(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ trails, force, ...particle }) => particle
),
},
Expand All @@ -369,6 +363,7 @@
settings: physicsConfig,
gravityPoints,
particles: particles.map(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ trails, force, ...particle }) => particle
),
},
Expand Down
2 changes: 0 additions & 2 deletions src/components/SaveScenarioModal/SaveScenarioModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { Scenario } from "../../types/scenario";
import { createShareableLink } from "../../utils/compression";
import { CopyOutlined, CheckOutlined } from "@ant-design/icons";

interface SaveScenarioModalProps {
Expand Down
1 change: 0 additions & 1 deletion src/components/ScenarioPanel/ScenarioPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
export const ScenarioPanel: React.FC<ScenarioPanelProps> = ({
onSelectScenario,
isOpen,
onClose,
}) => {
const [activeTab, setActiveTab] = useState("1");
const { savedScenarios, deleteSavedScenario } = useSettings();
Expand All @@ -35,7 +34,7 @@
onSelectScenario(sharedScenario);
}
}
}, [searchParams]);

Check warning on line 37 in src/components/ScenarioPanel/ScenarioPanel.tsx

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has a missing dependency: 'onSelectScenario'. Either include it or remove the dependency array. If 'onSelectScenario' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<AnimatePresence>
Expand Down
23 changes: 14 additions & 9 deletions src/components/SimulatorSettings/SimulatorSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React, { useState } from "react";
import React from "react";
import { motion, AnimatePresence } from "framer-motion";
import { PHYSICS_CONFIG, SETTINGS_METADATA } from "../../constants/physics";
import {
DEFAULT_PHYSICS_CONFIG,
SETTINGS_METADATA,
} from "../../constants/physics";
import { useSettings } from "../../contexts/SettingsContext";

interface SimulatorSettingsProps {
onSettingsChange: (settings: typeof PHYSICS_CONFIG) => void;
onSettingsChange: (settings: typeof DEFAULT_PHYSICS_CONFIG) => void;
isOpen: boolean;
onClose: () => void;
}

export const SimulatorSettings: React.FC<SimulatorSettingsProps> = ({
onSettingsChange,
isOpen,
onClose,
}) => {
const {
settings,
Expand Down Expand Up @@ -40,7 +42,7 @@ export const SimulatorSettings: React.FC<SimulatorSettingsProps> = ({
onSettingsChange({ ...settings, ...newSettings });
};

const shouldShowSetting = (key: keyof typeof PHYSICS_CONFIG) => {
const shouldShowSetting = (key: keyof typeof DEFAULT_PHYSICS_CONFIG) => {
const isDevSetting = SETTINGS_METADATA[key].isDev;
return !isDevSetting || (isDevelopment && showDevSettings);
};
Expand Down Expand Up @@ -130,10 +132,13 @@ export const SimulatorSettings: React.FC<SimulatorSettingsProps> = ({
)}

{Object.entries(settings).map(([key, value]) =>
shouldShowSetting(key as keyof typeof PHYSICS_CONFIG) ? (
shouldShowSetting(
key as keyof typeof DEFAULT_PHYSICS_CONFIG
) ? (
<div key={key} style={{ marginBottom: "16px" }}>
{SETTINGS_METADATA[key as keyof typeof PHYSICS_CONFIG]
.type === "boolean" ? (
{SETTINGS_METADATA[
key as keyof typeof DEFAULT_PHYSICS_CONFIG
].type === "boolean" ? (
<label
style={{
display: "flex",
Expand Down Expand Up @@ -228,7 +233,7 @@ export const SimulatorSettings: React.FC<SimulatorSettingsProps> = ({
value={value as number}
onChange={(e) =>
handleSettingChange(
key as keyof typeof PHYSICS_CONFIG,
key as keyof typeof DEFAULT_PHYSICS_CONFIG,
Number(e.target.value)
)
}
Expand Down
18 changes: 15 additions & 3 deletions src/constants/physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ interface BooleanSettingMetadata {
}

type SettingMetadata = SliderSettingMetadata | BooleanSettingMetadata;

export const PHYSICS_CONFIG = {
export interface PhysicsSettings {
NEW_PARTICLE_MASS: number;
NEW_PARTICLE_ELASTICITY: number;
FRICTION: number;
DELTA_TIME: number;
POINTER_MASS: number;
SHOW_VELOCITY_ARROWS: boolean;
SHOW_FORCE_ARROWS: boolean;
CONSTANT_FORCE_X: number;
CONSTANT_FORCE_Y: number;
SOLID_BOUNDARIES: boolean;
PARTICLES_EXERT_GRAVITY: boolean;
}
export const DEFAULT_PHYSICS_CONFIG: PhysicsSettings = {
NEW_PARTICLE_MASS: 0.1,
NEW_PARTICLE_ELASTICITY: 0.8,
FRICTION: 1,
Expand All @@ -30,7 +42,7 @@ export const PHYSICS_CONFIG = {
} as const;

export const SETTINGS_METADATA: Record<
keyof typeof PHYSICS_CONFIG,
keyof typeof DEFAULT_PHYSICS_CONFIG,
SettingMetadata
> = {
NEW_PARTICLE_MASS: {
Expand Down
31 changes: 17 additions & 14 deletions src/contexts/SettingsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { createContext, useContext, useState, useCallback } from "react";
import { PHYSICS_CONFIG } from "../constants/physics";
import { DEFAULT_PHYSICS_CONFIG, PhysicsSettings } from "../constants/physics";
import { Scenario } from "../types/scenario";

const STORAGE_KEYS = {
Expand All @@ -9,10 +9,10 @@
} as const;

interface SettingsContextType {
settings: typeof PHYSICS_CONFIG;
settings: PhysicsSettings;
showDevSettings: boolean;
savedScenarios: Scenario[];
updateSettings: (newSettings: Partial<typeof PHYSICS_CONFIG>) => void;
updateSettings: (newSettings: Partial<PhysicsSettings>) => void;
updateShowDevSettings: (show: boolean) => void;
resetSettings: () => void;
saveScenario: (scenario: Scenario) => void;
Expand All @@ -25,20 +25,20 @@
export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [settings, setSettings] = useState(() => {
const [settings, setSettings] = useState<PhysicsSettings>(() => {
const savedSettings = localStorage.getItem(STORAGE_KEYS.SETTINGS);
if (savedSettings) {
const parsedSettings = JSON.parse(savedSettings);
const validSettings = Object.keys(parsedSettings).reduce((acc, key) => {
if (key in PHYSICS_CONFIG) {
if (key in DEFAULT_PHYSICS_CONFIG) {
acc[key] = parsedSettings[key];
}
return acc;
}, {} as Partial<typeof PHYSICS_CONFIG>);
}, {} as Partial<PhysicsSettings>);

return { ...PHYSICS_CONFIG, ...validSettings };
return { ...DEFAULT_PHYSICS_CONFIG, ...validSettings };
}
return PHYSICS_CONFIG;
return DEFAULT_PHYSICS_CONFIG;
});

const [showDevSettings, setShowDevSettings] = useState(() => {
Expand All @@ -52,13 +52,13 @@
});

const updateSettings = useCallback(
(newSettings: Partial<typeof PHYSICS_CONFIG>) => {
(newSettings: Partial<PhysicsSettings>) => {
const validNewSettings = Object.keys(newSettings).reduce((acc, key) => {
if (key in PHYSICS_CONFIG) {
acc[key] = newSettings[key as keyof typeof PHYSICS_CONFIG];
if (key in DEFAULT_PHYSICS_CONFIG) {
acc[key] = newSettings[key as keyof typeof DEFAULT_PHYSICS_CONFIG];
}
return acc;
}, {} as Partial<typeof PHYSICS_CONFIG>);
}, {} as Partial<PhysicsSettings>);

setSettings((prevSettings) => {
const updatedSettings = { ...prevSettings, ...validNewSettings };
Expand All @@ -78,8 +78,11 @@
}, []);

const resetSettings = useCallback(() => {
setSettings(PHYSICS_CONFIG);
localStorage.setItem(STORAGE_KEYS.SETTINGS, JSON.stringify(PHYSICS_CONFIG));
setSettings(DEFAULT_PHYSICS_CONFIG);
localStorage.setItem(
STORAGE_KEYS.SETTINGS,
JSON.stringify(DEFAULT_PHYSICS_CONFIG)
);
}, []);

const saveScenario = useCallback((scenario: Scenario) => {
Expand Down Expand Up @@ -123,7 +126,7 @@
);
};

export const useSettings = () => {

Check warning on line 129 in src/contexts/SettingsContext.tsx

View workflow job for this annotation

GitHub Actions / build

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components
const context = useContext(SettingsContext);
if (!context) {
throw new Error("useSettings must be used within a SettingsProvider");
Expand Down
1 change: 1 addition & 0 deletions src/types/particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface ParticleMechanics {
force: Force;
mass: number;
elasticity: number;
trails: TrailPoint[];
}

export interface TrailPoint extends Point2D {
Expand Down
37 changes: 7 additions & 30 deletions src/types/scenario.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
import { GravityPoint } from "../utils/types/physics";
import { Particle } from "./particle";
import { PhysicsSettings } from "../constants/physics";

export interface ScenarioData {
settings: {
NEW_PARTICLE_MASS: number;
NEW_PARTICLE_ELASTICITY: number;
FRICTION: number;
DELTA_TIME: number;
POINTER_MASS: number;
SHOW_VELOCITY_ARROWS: boolean;
SHOW_FORCE_ARROWS: boolean;
CONSTANT_FORCE_X: number;
CONSTANT_FORCE_Y: number;
SOLID_BOUNDARIES: boolean;
PARTICLES_EXERT_GRAVITY: boolean;
};
gravityPoints: Array<{
x: number;
y: number;
label: string;
mass: number;
color: string;
}>;
particles: Array<{
id: string;
position: { x: number; y: number };
velocity: { x: number; y: number };
mass: number;
elasticity: number;
color: string;
size: number;
showVectors: boolean;
}>;
settings: PhysicsSettings;
gravityPoints: Array<GravityPoint>;
particles: Array<Omit<Particle, "trails" | "force">>;
}

export interface Scenario {
Expand Down
8 changes: 0 additions & 8 deletions src/types/star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ export interface StarTemplate {
size: number;
}

export interface GravityPoint {
x: number;
y: number;
label: string;
mass: number;
color: string;
}

export enum StarClass {
BROWN_DWARF = "BROWN_DWARF", // <0.08 solar masses
RED_DWARF = "RED_DWARF", // 0.08-0.45 solar masses
Expand Down
Loading