:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, beforeUnmount, kea, listeners, path, reducers } from 'kea'
import posthog from 'lib/posthog-typed'
/** Where the command menu open was initiated from, reported on the `command menu opened` event. */
export type CommandOpenSource = 'keyboard-shortcut' | 'nav-search-button' | 'nav-search-bar'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface commandLogicValues {
hasOpenedCommand: boolean
isCommandInputReady: boolean
isCommandOpen: boolean
openSource: CommandOpenSource | null
pendingText: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface commandLogicActions {
bufferKey: (key: string) => {
key: string
}
bufferPaste: (text: string) => {
text: string
}
closeCommand: () => {
value: true
}
commandInputReady: () => {
value: true
}
openCommand: (source: CommandOpenSource) => {
source: CommandOpenSource
}
toggleCommand: (source: CommandOpenSource) => {
source: CommandOpenSource
}
}
export type commandLogicType = MakeLogicType<commandLogicValues, commandLogicActions>
function isTypedCharacter(event: KeyboardEvent): boolean {
return event.key.length === 1 && !event.metaKey && !event.ctrlKey && !event.altKey && !event.isComposing
}
export const commandLogic = kea<commandLogicType>([
path(['lib', 'components', 'Command', 'commandLogic']),
actions({
openCommand: (source: CommandOpenSource) => ({ source }),
closeCommand: true,
toggleCommand: (source: CommandOpenSource) => ({ source }),
commandInputReady: true,
bufferKey: (key: string) => ({ key }),
bufferPaste: (text: string) => ({ text }),
}),
reducers({
isCommandOpen: [
false,
{
openCommand: () => true,
closeCommand: () => false,
},
],
// Stays true after the first open, so the shell loads the palette once and keeps it mounted.
hasOpenedCommand: [
false,
{
openCommand: () => true,
},
],
isCommandInputReady: [
false,
{
commandInputReady: () => true,
},
],
// Keys typed while the palette chunk loads, so the search input starts with them.
pendingText: [
'',
{
bufferKey: (state, { key }) => (key === 'Backspace' ? state.slice(0, -1) : state + key),
bufferPaste: (state, { text }) => state + text,
closeCommand: () => '',
},
],
openSource: [
null as CommandOpenSource | null,
{
openCommand: (_, { source }) => source,
},
],
}),
listeners(({ actions, values, cache }) => {
const stopBuffering = (): void => {
if (cache.bufferKeys) {
window.removeEventListener('keydown', cache.bufferKeys, true)
window.removeEventListener('paste', cache.bufferPaste, true)
cache.bufferKeys = null
}
}
return {
toggleCommand: ({ source }) => {
if (!values.isCommandOpen) {
actions.openCommand(source)
} else if (values.isCommandInputReady) {
actions.closeCommand()
}
// A second press while the palette loads keeps it opening, because the user has not seen it yet.
},
openCommand: ({ source }) => {
posthog.capture('command menu opened', { source })
if (!values.isCommandInputReady && !cache.bufferKeys) {
cache.bufferKeys = (event: KeyboardEvent): void => {
if (event.key === 'Escape') {
actions.closeCommand()
} else if (event.key === 'Backspace' || isTypedCharacter(event)) {
event.preventDefault()
event.stopPropagation()
actions.bufferKey(event.key)
}
}
cache.bufferPaste = (event: ClipboardEvent): void => {
event.preventDefault()
event.stopPropagation()
actions.bufferPaste(event.clipboardData?.getData('text/plain') ?? '')
}
window.addEventListener('keydown', cache.bufferKeys, true)
window.addEventListener('paste', cache.bufferPaste, true)
}
},
commandInputReady: stopBuffering,
closeCommand: stopBuffering,
}
}),
beforeUnmount(({ cache }) => {
if (cache.bufferKeys) {
window.removeEventListener('keydown', cache.bufferKeys, true)
window.removeEventListener('paste', cache.bufferPaste, true)
}
}),
])