'use client'; import { Button } from '@heroui/button'; import { Modal, ModalBody, ModalContent, ModalFooter, ModalHeader } from '@heroui/modal'; import { useDisclosure } from '@heroui/use-disclosure'; import { forwardRef, useImperativeHandle, useState } from 'react'; import { Index2Color } from './consts'; import { rotationController } from './rotation-controller'; export type StateModalRef = { open: (state: Array>) => void; }; export const StateModal = forwardRef((_, ref) => { const [state, setState] = useState>>([]); const { isOpen, onOpen, onOpenChange } = useDisclosure(); useImperativeHandle(ref, () => ({ open: (state: Array>) => { setState(state); onOpen(); }, })); const copy = () => { navigator.clipboard.writeText(JSON.stringify(state)); }; const solve = async () => { try { const response = await fetch('/api/solve', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ state }), }); 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); } }; return ( {(onClose) => ( <> State
Front
{JSON.stringify(state[0])}
[{state[0].map((index) => Index2Color[index]).join(', ')}]
Back
{JSON.stringify(state[1])}
[{state[1].map((index) => Index2Color[index]).join(', ')}]
Right
{JSON.stringify(state[2])}
[{state[2].map((index) => Index2Color[index]).join(', ')}]
Left
{JSON.stringify(state[3])}
[{state[3].map((index) => Index2Color[index]).join(', ')}]
Up
{JSON.stringify(state[4])}
[{state[4].map((index) => Index2Color[index]).join(', ')}]
Down
{JSON.stringify(state[5])}
[{state[5].map((index) => Index2Color[index]).join(', ')}]
)}
); }); StateModal.displayName = 'StateModal';