forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryQuickAdd.tsx
More file actions
168 lines (155 loc) · 4.8 KB
/
Copy pathMemoryQuickAdd.tsx
File metadata and controls
168 lines (155 loc) · 4.8 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use client';
import { useState, useRef, useEffect } from 'react';
import { motion } from 'framer-motion';
import { Plus, Eye, EyeOff } from 'lucide-react';
import { cn } from '@/lib/utils';
import type { Memory, MemoryVisibility } from '@/types/conversation';
interface MemoryQuickAddProps {
onAdd: (content: string, visibility?: MemoryVisibility) => Promise<Memory | null>;
disabled?: boolean;
}
export function MemoryQuickAdd({ onAdd, disabled = false }: MemoryQuickAddProps) {
const [isExpanded, setIsExpanded] = useState(false);
const [content, setContent] = useState('');
const [visibility, setVisibility] = useState<MemoryVisibility>('public');
const [isSubmitting, setIsSubmitting] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (isExpanded && inputRef.current) {
inputRef.current.focus();
}
}, [isExpanded]);
const handleSubmit = async (e?: React.FormEvent) => {
e?.preventDefault();
if (!content.trim() || isSubmitting) return;
setIsSubmitting(true);
try {
const result = await onAdd(content.trim(), visibility);
if (result) {
setContent('');
setVisibility('public');
setIsExpanded(false);
}
} finally {
setIsSubmitting(false);
}
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit();
} else if (e.key === 'Escape') {
handleCancel();
}
};
const handleCancel = () => {
setContent('');
setVisibility('public');
setIsExpanded(false);
};
if (!isExpanded) {
return (
<button
onClick={() => setIsExpanded(true)}
disabled={disabled}
className={cn(
'flex items-center gap-2 w-full px-3 py-2.5',
'rounded-lg border border-dashed border-bg-quaternary',
'text-text-tertiary hover:text-text-secondary',
'hover:border-white/50 hover:bg-bg-tertiary',
'transition-all duration-150',
disabled && 'opacity-50 cursor-not-allowed',
)}
>
<Plus className="w-4 h-4" />
<span className="text-sm">Add a memory...</span>
</button>
);
}
return (
<motion.form
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.15 }}
onSubmit={handleSubmit}
className={cn('rounded-lg border border-white/50', 'bg-bg-secondary p-3 space-y-3')}
>
{/* Input */}
<input
ref={inputRef}
type="text"
value={content}
onChange={(e) => setContent(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="What would you like to remember?"
disabled={isSubmitting}
className={cn(
'w-full bg-transparent',
'text-sm text-text-primary placeholder:text-text-quaternary',
'outline-none',
)}
/>
{/* Actions row */}
<div className="flex items-center justify-between gap-2">
{/* Visibility toggle */}
<button
type="button"
onClick={() => setVisibility(visibility === 'public' ? 'private' : 'public')}
disabled={isSubmitting}
className={cn(
'flex items-center gap-1.5 px-2 py-1 rounded text-xs',
'transition-colors',
visibility === 'public'
? 'text-success hover:bg-success/10'
: 'text-warning hover:bg-warning/10',
)}
>
{visibility === 'public' ? (
<>
<Eye className="w-3 h-3" />
Public
</>
) : (
<>
<EyeOff className="w-3 h-3" />
Private
</>
)}
</button>
{/* Buttons */}
<div className="flex items-center gap-2">
<button
type="button"
onClick={handleCancel}
disabled={isSubmitting}
className={cn(
'px-3 py-1 text-xs rounded',
'text-text-tertiary hover:text-text-secondary',
'transition-colors',
)}
>
Cancel
</button>
<button
type="submit"
disabled={!content.trim() || isSubmitting}
className={cn(
'px-3 py-1 text-xs rounded',
'bg-white hover:bg-white/90',
'text-black font-medium',
'transition-colors',
'disabled:opacity-50 disabled:cursor-not-allowed',
)}
>
{isSubmitting ? 'Adding...' : 'Add Memory'}
</button>
</div>
</div>
{/* Hint */}
<p className="text-[10px] text-text-quaternary">
Press Enter to add, Escape to cancel
</p>
</motion.form>
);
}