forked from mongodb/docs-sample-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchMovieModal.tsx
More file actions
414 lines (379 loc) · 14.2 KB
/
SearchMovieModal.tsx
File metadata and controls
414 lines (379 loc) · 14.2 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
'use client';
/**
* Search Movie Modal Component
*
* Modal for searching movies across multiple fields using MongoDB Search.
* Supports plot, fullplot, directors, writers, cast fields with search operator options.
*/
import { useState } from 'react';
import styles from './SearchMovieModal.module.css';
interface SearchMovieModalProps {
onSearch: (searchParams: SearchParams) => void;
onCancel: () => void;
isLoading?: boolean;
}
export type SearchType = 'mongodb-search' | 'vector-search';
export interface SearchParams {
searchType: SearchType;
// MongoDB Search fields
plot?: string;
fullplot?: string;
directors?: string;
writers?: string;
cast?: string;
limit?: number;
skip?: number;
search_operator?: 'must' | 'should' | 'mustNot' | 'filter';
// Vector Search fields
q?: string;
}
interface SearchFormData {
searchType: SearchType;
// MongoDB Search fields
plot: string;
fullplot: string;
directors: string;
writers: string;
cast: string;
limit: string;
search_operator: 'must' | 'should' | 'mustNot' | 'filter';
// Vector Search fields
q: string;
}
const getInitialFormData = (): SearchFormData => ({
searchType: 'mongodb-search',
// MongoDB Search fields
plot: '',
fullplot: '',
directors: '',
writers: '',
cast: '',
limit: '20',
search_operator: 'must',
// Vector Search fields
q: '',
});
export default function SearchMovieModal({
onSearch,
onCancel,
isLoading = false
}: SearchMovieModalProps) {
const [formData, setFormData] = useState<SearchFormData>(getInitialFormData());
const [errors, setErrors] = useState<Record<string, string>>({});
const validateForm = () => {
const newErrors: Record<string, string> = {};
if (formData.searchType === 'mongodb-search') {
// Check if at least one search field has a value for MongoDB Search
const hasSearchInput = formData.plot.trim() ||
formData.fullplot.trim() ||
formData.directors.trim() ||
formData.writers.trim() ||
formData.cast.trim();
if (!hasSearchInput) {
newErrors.general = 'Please enter search terms in at least one field';
}
} else if (formData.searchType === 'vector-search') {
// Check if query field has a value for Vector Search
if (!formData.q.trim()) {
newErrors.q = 'Please enter a search query.';
}
}
// Validate limit
const limitNum = parseInt(formData.limit);
if (!limitNum || limitNum < 1 || limitNum > 100) {
newErrors.limit = 'Limit must be between 1 and 100';
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (!validateForm()) {
return;
}
// Build search parameters based on search type
const searchParams: SearchParams = {
searchType: formData.searchType,
limit: parseInt(formData.limit),
};
if (formData.searchType === 'mongodb-search') {
// Add MongoDB Search specific parameters
searchParams.search_operator = formData.search_operator;
searchParams.skip = 0; // Always start from beginning for new search
if (formData.plot.trim()) {
searchParams.plot = formData.plot.trim();
}
if (formData.fullplot.trim()) {
searchParams.fullplot = formData.fullplot.trim();
}
if (formData.directors.trim()) {
searchParams.directors = formData.directors.trim();
}
if (formData.writers.trim()) {
searchParams.writers = formData.writers.trim();
}
if (formData.cast.trim()) {
searchParams.cast = formData.cast.trim();
}
} else if (formData.searchType === 'vector-search') {
// Add Vector Search specific parameters
searchParams.q = formData.q.trim();
}
onSearch(searchParams);
};
const handleInputChange = (field: string, value: string) => {
setFormData(prev => ({ ...prev, [field]: value }));
// Clear errors when user starts typing
if (errors[field]) {
setErrors(prev => ({ ...prev, [field]: '' }));
}
if (errors.general) {
setErrors(prev => ({ ...prev, general: '' }));
}
};
const handleClear = () => {
setFormData(getInitialFormData());
setErrors({});
};
const searchOperatorOptions = [
{ value: 'must', label: 'Must match all fields (AND)', description: 'All specified fields must match' },
{ value: 'should', label: 'Should match any field (OR)', description: 'At least one field should match' },
{ value: 'mustNot', label: 'Must not match', description: 'Results must NOT contain these terms' },
{ value: 'filter', label: 'Filter results', description: 'Filter results by these criteria' },
];
return (
<div className={styles.formContainer}>
<h2 className={styles.formTitle}>Search Movies</h2>
<p className={styles.batchDescription}>
{formData.searchType === 'mongodb-search'
? 'Search across movie plots, directors, writers, and cast.'
: 'Find movies with similar plots using semantic search.'
}
</p>
{errors.general && (
<div className={styles.generalError}>
{errors.general}
</div>
)}
<form onSubmit={handleSubmit} className={styles.form}>
{/* Search Type Selector */}
<div className={styles.formGroup}>
<label htmlFor="searchType" className={styles.label}>
Search Type
</label>
<select
id="searchType"
value={formData.searchType}
onChange={(e) => handleInputChange('searchType', e.target.value)}
className={styles.input}
disabled={isLoading}
>
<option value="mongodb-search">MongoDB Search</option>
<option value="vector-search">MongoDB Vector Search</option>
</select>
<small className={styles.searchOperatorDescription}>
{formData.searchType === 'mongodb-search'
? 'Search across multiple fields by using text matching and compound operators'
: 'Find movies with similar plots using AI-powered semantic search'
}
</small>
</div>
{/* Conditional Form Fields */}
{formData.searchType === 'mongodb-search' ? (
<>
{/* MongoDB Search Fields */}
<div className={styles.formGrid}>
{/* Plot Search */}
<div className={styles.formGroup}>
<label htmlFor="plot" className={styles.label}>
Plot Keywords
</label>
<input
type="text"
id="plot"
value={formData.plot}
onChange={(e) => handleInputChange('plot', e.target.value)}
className={`${styles.input} ${errors.plot ? styles.inputError : ''}`}
disabled={isLoading}
placeholder="Exact phrase search in plot summaries"
/>
{errors.plot && <span className={styles.error}>{errors.plot}</span>}
</div>
{/* Full Plot Search */}
<div className={styles.formGroup}>
<label htmlFor="fullplot" className={styles.label}>
Full Plot Keywords
</label>
<input
type="text"
id="fullplot"
value={formData.fullplot}
onChange={(e) => handleInputChange('fullplot', e.target.value)}
className={`${styles.input} ${errors.fullplot ? styles.inputError : ''}`}
disabled={isLoading}
placeholder="Search in full plot descriptions"
/>
{errors.fullplot && <span className={styles.error}>{errors.fullplot}</span>}
</div>
{/* Directors Search */}
<div className={styles.formGroup}>
<label htmlFor="directors" className={styles.label}>
Directors
</label>
<input
type="text"
id="directors"
value={formData.directors}
onChange={(e) => handleInputChange('directors', e.target.value)}
className={`${styles.input} ${errors.directors ? styles.inputError : ''}`}
disabled={isLoading}
placeholder="Director names"
/>
{errors.directors && <span className={styles.error}>{errors.directors}</span>}
</div>
{/* Writers Search */}
<div className={styles.formGroup}>
<label htmlFor="writers" className={styles.label}>
Writers
</label>
<input
type="text"
id="writers"
value={formData.writers}
onChange={(e) => handleInputChange('writers', e.target.value)}
className={`${styles.input} ${errors.writers ? styles.inputError : ''}`}
disabled={isLoading}
placeholder="Writer names"
/>
{errors.writers && <span className={styles.error}>{errors.writers}</span>}
</div>
{/* Cast Search */}
<div className={styles.formGroup}>
<label htmlFor="cast" className={styles.label}>
Cast
</label>
<input
type="text"
id="cast"
value={formData.cast}
onChange={(e) => handleInputChange('cast', e.target.value)}
className={`${styles.input} ${errors.cast ? styles.inputError : ''}`}
disabled={isLoading}
placeholder="Actor names"
/>
{errors.cast && <span className={styles.error}>{errors.cast}</span>}
</div>
{/* Limit */}
<div className={styles.formGroup}>
<label htmlFor="limit" className={styles.label}>
Max Results
</label>
<input
type="number"
id="limit"
value={formData.limit}
onChange={(e) => handleInputChange('limit', e.target.value)}
className={`${styles.input} ${errors.limit ? styles.inputError : ''}`}
disabled={isLoading}
min="1"
max="100"
/>
{errors.limit && <span className={styles.error}>{errors.limit}</span>}
</div>
</div>
{/* Search Operator */}
<div className={styles.formGroup}>
<label htmlFor="search_operator" className={styles.label}>
Search Logic
</label>
<select
id="search_operator"
value={formData.search_operator}
onChange={(e) => handleInputChange('search_operator', e.target.value)}
className={styles.input}
disabled={isLoading}
>
{searchOperatorOptions.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
<small className={styles.searchOperatorDescription}>
{searchOperatorOptions.find(opt => opt.value === formData.search_operator)?.description}
</small>
</div>
</>
) : (
<>
{/* Vector Search Fields */}
<div className={styles.formGroup}>
<label htmlFor="q" className={styles.label}>
Search Query
</label>
<textarea
id="q"
value={formData.q}
onChange={(e) => handleInputChange('q', e.target.value)}
className={`${styles.input} ${errors.q ? styles.inputError : ''}`}
disabled={isLoading}
placeholder="Describe the plot or theme you're looking for. e.g., 'A story about friendship and adventure in space'"
rows={3}
/>
{errors.q && <span className={styles.error}>{errors.q}</span>}
<small className={styles.searchOperatorDescription}>
Describe the plot, theme, or mood you're looking for. MongoDB will find movies with similar content.
</small>
</div>
{/* Limit for Vector Search */}
<div className={styles.formGroup}>
<label htmlFor="limit_vector" className={styles.label}>
Max Results
</label>
<input
type="number"
id="limit_vector"
value={formData.limit}
onChange={(e) => handleInputChange('limit', e.target.value)}
className={`${styles.input} ${errors.limit ? styles.inputError : ''}`}
disabled={isLoading}
min="1"
max="50"
/>
{errors.limit && <span className={styles.error}>{errors.limit}</span>}
<small className={styles.searchOperatorDescription}>
Vector search supports up to 50 results
</small>
</div>
</>
)}
{/* Form Actions */}
<div className={styles.formActions}>
<button
type="button"
onClick={handleClear}
className={`${styles.button} ${styles.clearButton}`}
disabled={isLoading}
>
Clear
</button>
<button
type="button"
onClick={onCancel}
className={`${styles.button} ${styles.cancelButton}`}
disabled={isLoading}
>
Close
</button>
<button
type="submit"
className={`${styles.button} ${styles.saveButton}`}
disabled={isLoading}
>
{isLoading ? 'Searching...' : `Search Movies`}
</button>
</div>
</form>
</div>
);
}