forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSelector.tsx
More file actions
212 lines (198 loc) · 7.58 KB
/
Copy pathAppSelector.tsx
File metadata and controls
212 lines (198 loc) · 7.58 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use client';
import { useState, useEffect, useRef } from 'react';
import { ChevronDown, Check, Sparkles, Loader2 } from 'lucide-react';
import Image from '@tschk/moonshine-next/image';
import { motion, AnimatePresence } from 'framer-motion';
import { cn } from '@/lib/utils';
import { getChatApps, type App } from '@/lib/api';
interface AppSelectorProps {
selectedAppId: string | null;
onSelectApp: (appId: string | null) => void;
disabled?: boolean;
}
export function AppSelector({ selectedAppId, onSelectApp, disabled }: AppSelectorProps) {
const [isOpen, setIsOpen] = useState(false);
const [apps, setApps] = useState<App[]>([]);
const [isLoading, setIsLoading] = useState(true);
const dropdownRef = useRef<HTMLDivElement>(null);
// Load chat apps
useEffect(() => {
async function loadApps() {
try {
const chatApps = await getChatApps();
setApps(chatApps);
} catch (err) {
console.error('Failed to load chat apps:', err);
} finally {
setIsLoading(false);
}
}
loadApps();
}, []);
// Close dropdown when clicking outside
useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
}
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);
const selectedApp = apps.find(app => app.id === selectedAppId);
return (
<div className="relative" ref={dropdownRef}>
{/* Trigger button */}
<button
onClick={() => !disabled && setIsOpen(!isOpen)}
disabled={disabled}
className={cn(
'flex items-center gap-2 px-3 py-2 rounded-lg',
'bg-bg-tertiary hover:bg-bg-quaternary',
'border border-bg-quaternary',
'transition-colors',
'disabled:opacity-50 disabled:cursor-not-allowed',
isOpen && 'bg-bg-quaternary'
)}
>
{/* Selected app avatar */}
{selectedApp ? (
<div className="w-6 h-6 rounded-full overflow-hidden bg-bg-quaternary flex-shrink-0">
{selectedApp.image ? (
<Image
src={selectedApp.image}
alt={selectedApp.name}
width={24}
height={24}
className="object-cover"
/>
) : (
<div className="w-full h-full flex items-center justify-center text-text-tertiary text-xs">
{selectedApp.name.charAt(0)}
</div>
)}
</div>
) : (
<div className="w-6 h-6 rounded-full bg-white/[0.14] flex items-center justify-center flex-shrink-0">
<Sparkles className="w-3.5 h-3.5 text-text-primary" />
</div>
)}
<span className="text-sm text-text-primary max-w-[120px] truncate">
{selectedApp ? selectedApp.name : 'Omi'}
</span>
<ChevronDown className={cn(
'w-4 h-4 text-text-tertiary transition-transform',
isOpen && 'rotate-180'
)} />
</button>
{/* Dropdown menu */}
<AnimatePresence>
{isOpen && (
<motion.div
initial={{ opacity: 0, y: -8 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -8 }}
transition={{ duration: 0.15 }}
className={cn(
'absolute top-full left-0 mt-2 z-50',
'min-w-[200px] max-w-[280px]',
'bg-bg-secondary border border-bg-tertiary rounded-xl',
'shadow-lg overflow-hidden'
)}
>
{isLoading ? (
<div className="flex items-center justify-center py-4">
<Loader2 className="w-5 h-5 text-text-tertiary animate-spin" />
</div>
) : (
<div className="py-1 max-h-[300px] overflow-y-auto">
{/* Default Omi option */}
<button
onClick={() => {
onSelectApp(null);
setIsOpen(false);
}}
className={cn(
'w-full flex items-center gap-3 px-4 py-2.5',
'hover:bg-bg-tertiary transition-colors',
!selectedAppId && 'bg-bg-tertiary/50'
)}
>
<div className="w-8 h-8 rounded-full bg-white/[0.14] flex items-center justify-center flex-shrink-0">
<Sparkles className="w-4 h-4 text-text-primary" />
</div>
<div className="flex-1 text-left">
<p className="text-sm font-medium text-text-primary">Omi</p>
<p className="text-xs text-text-tertiary">Default assistant</p>
</div>
{!selectedAppId && (
<Check className="w-4 h-4 text-text-primary flex-shrink-0" />
)}
</button>
{/* Separator if there are apps */}
{apps.length > 0 && (
<div className="border-t border-bg-tertiary my-1" />
)}
{/* Chat apps list */}
{apps.map(app => (
<button
key={app.id}
onClick={() => {
onSelectApp(app.id);
setIsOpen(false);
}}
className={cn(
'w-full flex items-center gap-3 px-4 py-2.5',
'hover:bg-bg-tertiary transition-colors',
selectedAppId === app.id && 'bg-bg-tertiary/50'
)}
>
<div className="w-8 h-8 rounded-full overflow-hidden bg-bg-quaternary flex-shrink-0">
{app.image ? (
<Image
src={app.image}
alt={app.name}
width={32}
height={32}
className="object-cover"
/>
) : (
<div className="w-full h-full flex items-center justify-center text-text-tertiary text-sm">
{app.name.charAt(0)}
</div>
)}
</div>
<div className="flex-1 text-left min-w-0">
<p className="text-sm font-medium text-text-primary truncate">
{app.name}
</p>
{app.description && (
<p className="text-xs text-text-tertiary truncate">
{app.description}
</p>
)}
</div>
{selectedAppId === app.id && (
<Check className="w-4 h-4 text-text-primary flex-shrink-0" />
)}
</button>
))}
{/* Empty state */}
{apps.length === 0 && (
<div className="px-4 py-3 text-center">
<p className="text-sm text-text-tertiary">
No chat apps enabled
</p>
<p className="text-xs text-text-quaternary mt-1">
Enable apps in the Apps section
</p>
</div>
)}
</div>
)}
</motion.div>
)}
</AnimatePresence>
</div>
);
}