**What** - Renames /admin-next -> /admin - Renames @medusajs/admin-sdk -> @medusajs/admin-bundler - Creates a new package called @medusajs/admin-sdk that will hold all tooling relevant to creating admin extensions. This is currently `defineRouteConfig` and `defineWidgetConfig`, but will eventually also export methods for adding custom fields, register translation, etc. - cc: @shahednasser we should update the examples in the docs so these functions are imported from `@medusajs/admin-sdk`. People will also need to install the package in their project, as it's no longer a transient dependency. - cc: @olivermrbl we might want to publish a changelog when this is merged, as it is a breaking change, and will require people to import the `defineXConfig` from the new package instead of `@medusajs/admin-shared`. - Updates CODEOWNERS so /admin packages does not require a review from the UI team.
68 lines
1.4 KiB
TypeScript
68 lines
1.4 KiB
TypeScript
import { useCallback, useState } from "react"
|
|
|
|
/**
|
|
* Base interface for a command that can be managed
|
|
* by the `useCommandHistory` hook.
|
|
*/
|
|
export interface Command {
|
|
execute: () => void
|
|
undo: () => void
|
|
redo: () => void
|
|
}
|
|
|
|
/**
|
|
* Hook to manage a history of commands that can be undone, redone, and executed.
|
|
*/
|
|
export const useCommandHistory = (maxHistory = 20) => {
|
|
const [past, setPast] = useState<Command[]>([])
|
|
const [future, setFuture] = useState<Command[]>([])
|
|
|
|
const canUndo = past.length > 0
|
|
const canRedo = future.length > 0
|
|
|
|
const undo = useCallback(() => {
|
|
if (!canUndo) {
|
|
return
|
|
}
|
|
|
|
const previous = past[past.length - 1]
|
|
const newPast = past.slice(0, past.length - 1)
|
|
|
|
previous.undo()
|
|
|
|
setPast(newPast)
|
|
setFuture([previous, ...future.slice(0, maxHistory - 1)])
|
|
}, [canUndo, future, past, maxHistory])
|
|
|
|
const redo = useCallback(() => {
|
|
if (!canRedo) {
|
|
return
|
|
}
|
|
|
|
const next = future[0]
|
|
const newFuture = future.slice(1)
|
|
|
|
next.redo()
|
|
|
|
setPast([...past, next].slice(0, maxHistory - 1))
|
|
setFuture(newFuture)
|
|
}, [canRedo, future, past, maxHistory])
|
|
|
|
const execute = useCallback(
|
|
(command: Command) => {
|
|
command.execute()
|
|
setPast((past) => [...past, command].slice(0, maxHistory - 1))
|
|
setFuture([])
|
|
},
|
|
[maxHistory]
|
|
)
|
|
|
|
return {
|
|
undo,
|
|
redo,
|
|
execute,
|
|
canUndo,
|
|
canRedo,
|
|
}
|
|
}
|