Add more shortcut options

This commit is contained in:
Nate Kelley 2025-09-29 15:48:53 -06:00
parent d46383103d
commit f2490ecccd
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
4 changed files with 43 additions and 7 deletions

View File

@ -2,8 +2,10 @@ import type { ListShortcutsResponse } from '@buster/server-shared/shortcuts';
import type { GetSuggestedPromptsResponse } from '@buster/server-shared/user'; import type { GetSuggestedPromptsResponse } from '@buster/server-shared/user';
import sampleSize from 'lodash/sampleSize'; import sampleSize from 'lodash/sampleSize';
import React, { useMemo, useRef, useState } from 'react'; import React, { useMemo, useRef, useState } from 'react';
import { Plus } from '@/components/ui/icons';
import type { MentionSuggestionExtension } from '@/components/ui/inputs/MentionInput'; import type { MentionSuggestionExtension } from '@/components/ui/inputs/MentionInput';
import type { import type {
MentionInputSuggestionsDropdownItem,
MentionInputSuggestionsProps, MentionInputSuggestionsProps,
MentionInputSuggestionsRef, MentionInputSuggestionsRef,
} from '@/components/ui/inputs/MentionInputSuggestions'; } from '@/components/ui/inputs/MentionInputSuggestions';
@ -145,12 +147,33 @@ const useShortcuts = (
shortcuts: ListShortcutsResponse['shortcuts'] shortcuts: ListShortcutsResponse['shortcuts']
): MentionInputSuggestionsProps['suggestionItems'] => { ): MentionInputSuggestionsProps['suggestionItems'] => {
return useMemo(() => { return useMemo(() => {
return shortcuts.map((shortcut) => { const shortcutsItems = shortcuts.map<MentionInputSuggestionsDropdownItem>((shortcut) => {
return { return {
type: 'item', type: 'item',
value: shortcut.name, value: shortcut.name,
label: shortcut.name, label: shortcut.name,
icon: '/',
}; };
}); });
shortcutsItems.push({
type: 'item',
value: 'createShortcut',
label: 'Create shortcut',
icon: <Plus />,
onClick: () => {
console.log('createShortcut');
},
});
return [
{
type: 'group',
label: 'Shortcuts',
suggestionItems: shortcutsItems,
addValueToInput: false,
closeOnSelect: true,
},
];
}, [shortcuts]); }, [shortcuts]);
}; };

View File

@ -16,7 +16,6 @@ export type MentionInputSuggestionsDropdownItem<T = string> = {
value: T; value: T;
inputValue?: string; //if this is undefined, the label will be used (string casted), must have addValueToInput set to true inputValue?: string; //if this is undefined, the label will be used (string casted), must have addValueToInput set to true
label: string | React.ReactNode; label: string | React.ReactNode;
shortcut?: string;
icon?: React.ReactNode; icon?: React.ReactNode;
onClick?: () => void; onClick?: () => void;
disabled?: boolean; disabled?: boolean;

View File

@ -1,7 +1,10 @@
import { Command } from 'cmdk'; import { Command } from 'cmdk';
import type React from 'react'; import type React from 'react';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import type { MentionInputSuggestionsDropdownGroup, MentionInputSuggestionsOnSelectParams } from './MentionInputSuggestions.types'; import type {
MentionInputSuggestionsDropdownGroup,
MentionInputSuggestionsOnSelectParams,
} from './MentionInputSuggestions.types';
import { MentionInputSuggestionsItemsSelector } from './MentionInputSuggestionsItemSelector'; import { MentionInputSuggestionsItemsSelector } from './MentionInputSuggestionsItemSelector';
export type MentionInputSuggestionsGroupProps = MentionInputSuggestionsDropdownGroup & { export type MentionInputSuggestionsGroupProps = MentionInputSuggestionsDropdownGroup & {
@ -9,6 +12,8 @@ export type MentionInputSuggestionsGroupProps = MentionInputSuggestionsDropdownG
} & { } & {
className?: string; className?: string;
style?: React.CSSProperties; style?: React.CSSProperties;
hasResults: boolean;
setHasResults: (hasResults: boolean) => void;
}; };
export const MentionInputSuggestionsGroup = ({ export const MentionInputSuggestionsGroup = ({
@ -19,6 +24,7 @@ export const MentionInputSuggestionsGroup = ({
className, className,
closeOnSelect, closeOnSelect,
style, style,
...rest
}: MentionInputSuggestionsGroupProps) => { }: MentionInputSuggestionsGroupProps) => {
return ( return (
<Command.Group <Command.Group
@ -34,6 +40,7 @@ export const MentionInputSuggestionsGroup = ({
onSelect={onSelect} onSelect={onSelect}
addValueToInput={addValueToInput} addValueToInput={addValueToInput}
closeOnSelect={closeOnSelect} closeOnSelect={closeOnSelect}
{...rest}
/> />
</Command.Group> </Command.Group>
); );

View File

@ -20,7 +20,6 @@ export const MentionInputSuggestionsItem = ({
value, value,
inputValue, inputValue,
label, label,
shortcut,
icon, icon,
onClick, onClick,
disabled, disabled,
@ -31,16 +30,19 @@ export const MentionInputSuggestionsItem = ({
onSelect, onSelect,
hasResults, hasResults,
setHasResults, setHasResults,
...props className,
style,
}: MentionInputSuggestionsItemProps) => { }: MentionInputSuggestionsItemProps) => {
return ( return (
<Command.Item <Command.Item
className={cn( className={cn(
'data-[selected=true]:bg-item-hover data-[selected=true]:text-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-base outline-none select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0', 'data-[selected=true]:bg-item-hover data-[selected=true]:text-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-base outline-none select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0',
props.className !disabled ? 'cursor-pointer' : 'cursor-not-allowed',
'text-secondary group',
className
)} )}
value={value} value={value}
{...props} style={style}
onSelect={() => { onSelect={() => {
onSelect({ onSelect({
value, value,
@ -54,6 +56,11 @@ export const MentionInputSuggestionsItem = ({
}); });
}} }}
> >
{icon && (
<span className="text-icon-color min-w-4 w-4 text-center group-hover:text-foreground">
{icon}
</span>
)}
{label} {label}
{!hasResults && <SetHasResults hasResults={hasResults} setHasResults={setHasResults} />} {!hasResults && <SetHasResults hasResults={hasResults} setHasResults={setHasResults} />}
</Command.Item> </Command.Item>