forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCommandBadge.ts
More file actions
41 lines (35 loc) · 1.1 KB
/
useCommandBadge.ts
File metadata and controls
41 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useState, useCallback } from 'react';
import type { CommandBadge, CliBadge } from '@/types';
export type { CommandBadge, CliBadge } from '@/types';
export interface UseCommandBadgeReturn {
badge: CommandBadge | null;
setBadge: (badge: CommandBadge | null) => void;
cliBadge: CliBadge | null;
setCliBadge: (badge: CliBadge | null) => void;
removeBadge: () => void;
removeCliBadge: () => void;
hasBadge: boolean;
}
export function useCommandBadge(
textareaRef: React.RefObject<HTMLTextAreaElement | null>,
): UseCommandBadgeReturn {
const [badge, setBadge] = useState<CommandBadge | null>(null);
const [cliBadge, setCliBadge] = useState<CliBadge | null>(null);
const removeBadge = useCallback(() => {
setBadge(null);
setTimeout(() => textareaRef.current?.focus(), 0);
}, [textareaRef]);
const removeCliBadge = useCallback(() => {
setCliBadge(null);
setTimeout(() => textareaRef.current?.focus(), 0);
}, [textareaRef]);
return {
badge,
setBadge,
cliBadge,
setCliBadge,
removeBadge,
removeCliBadge,
hasBadge: !!badge || !!cliBadge,
};
}