'use client'; import { Button, ButtonGroup } from '@heroui/button'; import { Card, CardBody } from '@heroui/card'; import { Checkbox } from '@heroui/checkbox'; import { Slider } from '@heroui/slider'; import { useRef, useState } from 'react'; import { useControlContext } from '@/contexts/control-context'; import { Actions } from './consts'; import { rotationController } from './rotation-controller'; import { StateModal, StateModalRef } from './state-modal'; export const UIControls = () => { const stateModalRef = useRef(null); const [isSolving, setIsSolving] = useState(false); const [isControlsOpen, setIsControlsOpen] = useState(true); const { rubiksCubeRef, showRotationIndicators, setShowRotationIndicators, setBackground, cubeRoughness, setCubeRoughness, cubeSpeed, setCubeSpeed, scrambleLength, setScrambleLength, } = useControlContext(); const scramble = () => { const scrambleSteps = Array.from( { length: scrambleLength }, () => Actions[Math.floor(Math.random() * Actions.length)], ); rubiksCubeRef?.current?.rotate(scrambleSteps); }; const reset = () => { rubiksCubeRef?.current?.reset(); setIsSolving(false); }; const showState = () => { const state = rotationController.getState(); stateModalRef.current?.open(state); }; const train = () => { window.open('https://github.com/crossentropy-ai/rlcube', '_blank'); }; const solve = async () => { try { setIsSolving(true); const response = await fetch('/api/solve', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ state: rotationController.getState() }), }); if (response.status === 422) { alert('Unable to solve the cube.'); return; } if (!response.ok) { throw new Error('Server error', { cause: response }); } const { steps } = await response.json(); rotationController.addRotationStepCode(...steps); } catch (err) { alert('An error occurred. Check the console for details.'); console.error(err); } finally { setIsSolving(false); } }; return (
Controls
Rotation Indicators
Background
setCubeRoughness(value as number)} minValue={0.2} maxValue={1} step={0.01} /> setCubeSpeed(value as number)} minValue={1} maxValue={10} step={1} /> setScrambleLength(value as number)} minValue={1} maxValue={6} step={1} />
Train your own model!
); };