import React from 'react'
import {
KBarPortal,
KBarPositioner,
KBarAnimator,
KBarResults,
KBarSearch,
useMatches,
useKBar,
ActionImpl,
ActionId,
} from 'kbar'
import classNames from 'classnames'
import MultilineKBarSearch from './MultilineKBarSearch'
import useCommandBarActions, {
COMMAND_BAR_INPUT_ID,
VIEW_ONLY_MESSAGE,
} from './useCommandBarActions'
import IconExternalLink from '~/icons/compiled/ExternalLink'
export function DynamicCommandBarActions() {
useCommandBarActions()
return null
}
function usePlatformControlIcon() {
const isMac = navigator.userAgent.toUpperCase().indexOf('MAC') >= 0
return isMac ? '⌘' : 'Ctrl'
}
function Shortcut({
children,
withCmdCtrl,
}: {
children: React.ReactNode
withCmdCtrl?: boolean
}) {
const prefix = usePlatformControlIcon()
return (
{withCmdCtrl && prefix} {children}
)
}
export default function CommandBar() {
const { currentRootActionId } = useKBar(state => ({
currentRootActionId: state.currentRootActionId,
}))
return (
{currentRootActionId === null && (
K
)}
{currentRootActionId === 'search-actions' && (
/
)}
)
}
function RenderResults() {
let { results, rootActionId } = useMatches()
if (!results.length) {
return (
)
}
return (
// kbar gives the fixed container a static height that's 2px too short, unsure why
typeof item === 'string' ? (
{item}
) : (
)
}
/>
)
}
const ResultItem = React.forwardRef(
(
{
action,
active,
currentRootActionId,
}: {
action: ActionImpl
active: boolean
currentRootActionId: ActionId
},
ref: React.Ref
) => {
const ancestors = React.useMemo(() => {
if (!currentRootActionId) return action.ancestors
const index = action.ancestors.findIndex(
ancestor => ancestor.id === currentRootActionId
)
// +1 removes the currentRootAction; e.g.
// if we are on the "Set theme" parent action,
// the UI should not display "Set theme… > Dark"
// but rather just "Dark"
return action.ancestors.slice(index + 1)
}, [action.ancestors, currentRootActionId])
return (
{action.icon && action.icon}
{ancestors.length > 0 &&
ancestors.map(ancestor => (
{ancestor.name}
›
))}
{action.name}
{action.id === 'docs' && (
)}
{action.subtitle && (
{action.subtitle}
)}
{action.shortcut?.length ? (
{action.shortcut.map(sc => (
{sc}
))}
) : null}
)
}
)
ResultItem.displayName = 'ResultItem'