Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created December 4, 2024 17:26
Show Gist options
  • Save johnlindquist/99cf70b0cf1e4a7ba5b95ffd9041f962 to your computer and use it in GitHub Desktop.
Save johnlindquist/99cf70b0cf1e4a7ba5b95ffd9041f962 to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit"
import type { Action } from "@johnlindquist/kit"
export const metadata: Metadata = {
name: "Testing Change Actions on Focus",
}
// Define possible states
type ActionState = "open" | "closed"
// Create a state manager class
class ActionStateManager {
private currentState: ActionState = "open"
private readonly stateActions: Record<ActionState, Action[]> = {
open: [{
name: "Open",
shortcut: `${cmd}+p`,
onAction: (input, state) => {
console.log(`open ${state.focused.value}`);
},
}],
closed: [{
name: "Close",
shortcut: `${cmd}+p`,
onAction: (input, state) => {
console.log(`close ${state.focused.value}`);
},
}],
}
transition(newState: ActionState): void {
if (this.currentState !== newState) {
this.currentState = newState
setActions(this.stateActions[newState])
}
}
getCurrentActions(): Action[] {
return this.stateActions[this.currentState]
}
}
const stateManager = new ActionStateManager()
await arg(
{
placeholder: "Pick One",
onChoiceFocus: (input, state) => {
stateManager.transition(state.focused.value === "two" ? "closed" : "open")
},
},
[
"one",
"two",
"three",
],
stateManager.getCurrentActions()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment