2023-04-01 16:47:00 +11:00
|
|
|
import { Dialog, Transition } from '@headlessui/react'
|
2023-08-18 17:14:35 -05:00
|
|
|
import { Fragment } from 'react'
|
2023-09-09 01:38:36 -04:00
|
|
|
import { toast } from 'react-hot-toast'
|
2023-10-16 08:54:38 +11:00
|
|
|
import { type InstanceProps, create } from 'react-modal-promise'
|
2025-04-01 23:54:26 -07:00
|
|
|
|
|
|
|
import { ActionButton } from '@src/components/ActionButton'
|
|
|
|
import { CreateNewVariable } from '@src/components/AvailableVarsHelpers'
|
|
|
|
import { useCalculateKclExpression } from '@src/lib/useCalculateKclExpression'
|
2025-07-03 09:10:48 -04:00
|
|
|
import type { Selections } from '@src/lib/selections'
|
2023-10-16 08:54:38 +11:00
|
|
|
|
|
|
|
type ModalResolve = { variableName: string }
|
|
|
|
type ModalReject = boolean
|
|
|
|
type SetVarNameModalProps = InstanceProps<ModalResolve, ModalReject> & {
|
|
|
|
valueName: string
|
2025-07-03 09:10:48 -04:00
|
|
|
selectionRanges: Selections
|
2023-10-16 08:54:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
export const createSetVarNameModal = create<
|
|
|
|
SetVarNameModalProps,
|
|
|
|
ModalResolve,
|
|
|
|
ModalReject
|
|
|
|
>
|
2023-04-01 16:47:00 +11:00
|
|
|
|
|
|
|
export const SetVarNameModal = ({
|
|
|
|
isOpen,
|
|
|
|
onResolve,
|
|
|
|
onReject,
|
|
|
|
valueName,
|
2025-07-03 09:10:48 -04:00
|
|
|
selectionRanges,
|
2023-10-16 08:54:38 +11:00
|
|
|
}: SetVarNameModalProps) => {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { isNewVariableNameUnique, newVariableName, setNewVariableName } =
|
2025-07-03 09:10:48 -04:00
|
|
|
useCalculateKclExpression({
|
|
|
|
value: '',
|
|
|
|
initialVariableName: valueName,
|
|
|
|
selectionRanges,
|
|
|
|
})
|
2023-04-01 16:47:00 +11:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Transition appear show={isOpen} as={Fragment}>
|
2023-09-09 01:38:36 -04:00
|
|
|
<Dialog
|
|
|
|
as="div"
|
|
|
|
className="fixed inset-0 z-40 overflow-y-auto p-4 pt-[25vh]"
|
|
|
|
onClose={onReject}
|
|
|
|
>
|
2023-04-01 16:47:00 +11:00
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="ease-out duration-300"
|
2023-09-09 01:38:36 -04:00
|
|
|
enterFrom="opacity-0 translate-y-4"
|
|
|
|
enterTo="opacity-100 translate-y-0"
|
|
|
|
leave="ease-in duration-75"
|
2023-04-01 16:47:00 +11:00
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
2023-09-09 01:38:36 -04:00
|
|
|
<Dialog.Overlay className="fixed inset-0 bg-chalkboard-10/70 dark:bg-chalkboard-110/50" />
|
2023-04-01 16:47:00 +11:00
|
|
|
</Transition.Child>
|
|
|
|
|
2023-09-09 01:38:36 -04:00
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="ease-out duration-300"
|
|
|
|
enterFrom="opacity-0 scale-95"
|
|
|
|
enterTo="opacity-100 scale-100"
|
|
|
|
leave="ease-in duration-200"
|
|
|
|
leaveFrom="opacity-100 scale-100"
|
|
|
|
leaveTo="opacity-0 scale-95"
|
|
|
|
>
|
|
|
|
<Dialog.Panel className="rounded relative mx-auto px-4 py-8 bg-chalkboard-10 dark:bg-chalkboard-100 border dark:border-chalkboard-70 max-w-xl w-full shadow-lg">
|
|
|
|
<form
|
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
onResolve({
|
|
|
|
variableName: newVariableName,
|
|
|
|
})
|
|
|
|
toast.success(`Added variable ${newVariableName}`)
|
|
|
|
}}
|
2023-04-01 16:47:00 +11:00
|
|
|
>
|
2023-09-09 01:38:36 -04:00
|
|
|
<CreateNewVariable
|
|
|
|
setNewVariableName={setNewVariableName}
|
|
|
|
newVariableName={newVariableName}
|
|
|
|
isNewVariableNameUnique={isNewVariableNameUnique}
|
|
|
|
shouldCreateVariable={true}
|
|
|
|
showCheckbox={false}
|
|
|
|
/>
|
|
|
|
<div className="mt-8 flex justify-between">
|
2024-05-24 20:54:42 +10:00
|
|
|
<ActionButton Element="button" onClick={() => onReject(false)}>
|
|
|
|
Cancel
|
|
|
|
</ActionButton>
|
2023-09-09 01:38:36 -04:00
|
|
|
<ActionButton
|
|
|
|
Element="button"
|
|
|
|
type="submit"
|
|
|
|
disabled={!isNewVariableNameUnique}
|
2024-05-24 20:54:42 +10:00
|
|
|
iconStart={{ icon: 'plus' }}
|
2023-04-01 16:47:00 +11:00
|
|
|
>
|
2023-09-09 01:38:36 -04:00
|
|
|
Add variable
|
|
|
|
</ActionButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
2023-04-01 16:47:00 +11:00
|
|
|
</Dialog>
|
|
|
|
</Transition>
|
|
|
|
)
|
|
|
|
}
|