-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodal.gts
550 lines (497 loc) · 15.4 KB
/
modal.gts
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import { registerDestructor } from '@ember/destroyable';
import { fn, hash } from '@ember/helper';
import { on } from '@ember/modifier';
import { action } from '@ember/object';
import type Owner from '@ember/owner';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { restartableTask, task } from 'ember-concurrency';
import focusTrap from 'ember-focus-trap/modifiers/focus-trap';
import debounce from 'lodash/debounce';
import { TrackedArray, TrackedObject } from 'tracked-built-ins';
import { Button, BoxelInput } from '@cardstack/boxel-ui/components';
import { and, eq, gt, not } from '@cardstack/boxel-ui/helpers';
import { IconPlus } from '@cardstack/boxel-ui/icons';
import {
createNewCard,
baseRealm,
type CodeRef,
type CreateNewCard,
Deferred,
type RealmInfo,
Loader,
} from '@cardstack/runtime-common';
import type { Query, Filter } from '@cardstack/runtime-common/query';
import type OperatorModeStateService from '@cardstack/host/services/operator-mode-state-service';
import type { CardDef, CardContext } from 'https://cardstack.com/base/card-api';
import { getCard } from '../../resources/card-resource';
import { getSearchResults, Search } from '../../resources/search';
import {
suggestCardChooserTitle,
getSuggestionWithLowestDepth,
} from '../../utils/text-suggestion';
import ModalContainer from '../modal-container';
import { Submodes } from '../submode-switcher';
import CardCatalogFilters from './filters';
import CardCatalog from './index';
import type CardService from '../../services/card-service';
import type LoaderService from '../../services/loader-service';
interface Signature {
Args: {
context?: CardContext;
};
}
export interface RealmCards {
url: string | null;
realmInfo: RealmInfo;
cards: CardDef[];
}
type Request = {
search: Search;
deferred: Deferred<CardDef | undefined>;
opts?: {
offerToCreate?: { ref: CodeRef; relativeTo: URL | undefined };
createNewCard?: CreateNewCard;
};
};
type State = {
id: number;
request: Request;
selectedCard?: CardDef;
selectedRealms: RealmCards[];
searchKey: string;
searchResults: RealmCards[];
cardURL: string;
chooseCardTitle: string;
dismissModal: boolean;
errorMessage?: string;
};
const DEFAULT_CHOOOSE_CARD_TITLE = 'Choose a Card';
export default class CardCatalogModal extends Component<Signature> {
<template>
{{#if (and (gt this.stateStack.length 0) (not this.state.dismissModal))}}
<ModalContainer
@title={{this.state.chooseCardTitle}}
@onClose={{fn this.pick undefined}}
@zIndex={{this.zIndex}}
{{focusTrap
isActive=(not this.state.dismissModal)
focusTrapOptions=(hash
initialFocus='[data-test-search-field]' allowOutsideClick=true
)
}}
{{on 'keydown' this.handleKeydown}}
data-test-card-catalog-modal
>
<:header>
<BoxelInput
@type='search'
@variant='large'
@value={{this.state.searchKey}}
@onInput={{this.setSearchKey}}
@onKeyPress={{this.onSearchFieldKeypress}}
@placeholder='Search for a card or enter card URL'
@state={{if this.isInvalid 'invalid'}}
@errorMessage={{this.state.errorMessage}}
data-test-search-field
/>
<CardCatalogFilters
@availableRealms={{this.availableRealms}}
@selectedRealms={{this.state.selectedRealms}}
@onSelectRealm={{this.onSelectRealm}}
@onDeselectRealm={{this.onDeselectRealm}}
@disableRealmFilter={{this.searchKeyIsURL}}
/>
</:header>
<:content>
{{#if this.state.request.search.isLoading}}
Loading...
{{else}}
{{! The getter for availableRealms is necessary because
it's a resource that needs to load the search results }}
<CardCatalog
@results={{if
this.availableRealms.length
this.state.searchResults
this.availableRealms
}}
@select={{this.selectCard}}
@selectedCard={{this.state.selectedCard}}
@context={{@context}}
/>
{{/if}}
</:content>
<:footer>
<div class='footer'>
<div class='footer__actions-left'>
{{#if this.state.request.opts.offerToCreate}}
{{#unless
(eq this.operatorModeStateService.state.submode Submodes.Code)
}}
<Button
@kind='secondary-light'
@size='tall'
class='create-new-button'
{{on
'click'
(fn
this.createNew
this.state.request.opts.offerToCreate.ref
this.state.request.opts.offerToCreate.relativeTo
)
}}
data-test-card-catalog-create-new-button
>
<IconPlus width='20' height='20' role='presentation' />
Create New
{{this.cardRefName}}
</Button>
{{/unless}}
{{/if}}
</div>
<div>
<Button
@kind='secondary-light'
@size='tall'
class='footer-button'
{{on 'click' (fn this.pick undefined undefined)}}
data-test-card-catalog-cancel-button
>
Cancel
</Button>
<Button
@kind='primary'
@size='tall'
@disabled={{eq this.state.selectedCard undefined}}
class='footer-button'
{{on 'click' (fn this.pick this.state.selectedCard undefined)}}
data-test-card-catalog-go-button
>
Go
</Button>
</div>
</div>
</:footer>
</ModalContainer>
{{/if}}
<style>
.footer {
display: flex;
justify-content: space-between;
gap: var(--boxel-sp);
margin-left: auto;
}
.footer__actions-left {
display: flex;
gap: var(--boxel-sp);
flex-grow: 1;
}
.footer-button + .footer-button {
margin-left: var(--boxel-sp-xs);
}
.create-new-button {
--icon-color: var(--boxel-highlight);
display: flex;
justify-content: center;
align-items: center;
gap: var(--boxel-sp-xxs);
}
</style>
</template>
stateStack: State[] = new TrackedArray<State>();
stateId = 0;
@tracked zIndex = 20;
@service declare cardService: CardService;
@service declare loaderService: LoaderService;
@service declare operatorModeStateService: OperatorModeStateService;
constructor(owner: Owner, args: {}) {
super(owner, args);
(globalThis as any)._CARDSTACK_CARD_CHOOSER = this;
registerDestructor(this, () => {
delete (globalThis as any)._CARDSTACK_CARD_CHOOSER;
});
}
get cardRefName() {
return (
(
this.state.request.opts?.offerToCreate?.ref as {
module: string;
name: string;
}
).name ?? 'Card'
);
}
get availableRealms(): RealmCards[] {
// returns all available realms and their cards that match a certain type criteria
// realm filters and search key filter these groups of cards
// filters dropdown menu will always display all available realms
if (this.state.request.search.instancesByRealm.length) {
// eslint-disable-next-line ember/no-side-effects
this.state.searchResults = this.state.request.search.instancesByRealm;
}
return this.state.request.search.instancesByRealm ?? [];
}
get displayedRealms(): RealmCards[] {
// filters the available realm cards by selected realms
return this.state.selectedRealms.length
? this.state.selectedRealms
: this.availableRealms;
}
get state(): State {
return this.stateStack[this.stateStack.length - 1];
}
@action onSelectRealm(realm: RealmCards) {
this.state.selectedRealms.push(realm);
this.onSearchFieldUpdated();
}
@action onDeselectRealm(realm: RealmCards) {
let selectedRealmIndex = this.state.selectedRealms.findIndex(
(r) => r.url === realm.url,
);
this.state.selectedRealms.splice(selectedRealmIndex, 1);
this.onSearchFieldUpdated();
}
private resetState() {
this.state.searchKey = '';
this.state.searchResults = this.availableRealms;
this.state.cardURL = '';
this.state.selectedCard = undefined;
this.state.errorMessage = '';
this.state.dismissModal = false;
}
// This is part of our public API for runtime-common to invoke the card chooser
async chooseCard<T extends CardDef>(
query: Query,
opts?: {
offerToCreate?: { ref: CodeRef; relativeTo: URL | undefined };
multiSelect?: boolean;
createNewCard?: CreateNewCard;
},
): Promise<undefined | T> {
this.zIndex++;
return (await this._chooseCard.perform(
{
// default to title sort so that we can maintain stability in
// the ordering of the search results (server sorts results
// by order indexed by default)
sort: [
{
on: {
module: `${baseRealm.url}card-api`,
name: 'CardDef',
},
by: 'title',
},
],
...query,
},
opts,
)) as T | undefined;
}
private _chooseCard = task(
async <T extends CardDef>(
query: Query,
opts: {
offerToCreate?: { ref: CodeRef; relativeTo: URL | undefined };
multiSelect?: boolean;
} = {},
) => {
this.stateId++;
let title = await chooseCardTitle(
query.filter,
this.loaderService.loader,
opts?.multiSelect,
);
let request = new TrackedObject<Request>({
search: getSearchResults(this, () => query),
deferred: new Deferred(),
opts,
});
let cardCatalogState = new TrackedObject<State>({
id: this.stateId,
request,
chooseCardTitle: title,
searchKey: '',
cardURL: '',
searchResults: new TrackedArray<RealmCards>([]),
selectedRealms: new TrackedArray<RealmCards>([]),
dismissModal: false,
});
this.stateStack.push(cardCatalogState);
let card = await request.deferred.promise;
if (card) {
return card as T;
} else {
return undefined;
}
},
);
@action
setSearchKey(searchKey: string) {
this.state.searchKey = searchKey;
if (!this.state.searchKey) {
this.resetState();
} else {
this.debouncedSearchFieldUpdate();
}
}
get searchKeyIsURL() {
try {
new URL(this.state.searchKey);
return true;
} catch (_e) {
return false;
}
}
debouncedSearchFieldUpdate = debounce(() => this.onSearchFieldUpdated(), 500);
@action setCardURL(cardURL: string) {
this.state.selectedCard = undefined;
this.state.cardURL = cardURL;
}
@action
onSearchFieldKeypress(e: KeyboardEvent) {
if (e.key === 'Enter') {
this.onSearchFieldUpdated();
}
}
get isInvalid() {
if (!this.state.searchKey) {
return false;
}
return this.state.errorMessage || this.state.searchResults?.length === 0;
}
setErrorState(message: string) {
this.state.errorMessage = message;
this.state.searchResults = [];
}
private getCard = restartableTask(async (cardURL: string) => {
let maybeIndexCardURL = this.cardService.realmURLs.find(
(u) => u === cardURL + '/',
);
const cardResource = getCard(this, () => maybeIndexCardURL ?? cardURL, {
isLive: () => false,
});
await cardResource.loaded;
let { card } = cardResource;
if (!card) {
this.setErrorState(`Could not find card at ${this.state.searchKey}`);
return;
}
let realmInfo = await this.cardService.getRealmInfo(card);
if (!realmInfo) {
this.setErrorState(`Encountered error getting realm info for ${cardURL}`);
return;
}
this.state.searchResults = [
{
url: card.id,
realmInfo,
cards: [card],
},
];
});
@action
onSearchFieldUpdated() {
this.state.errorMessage = '';
if (!this.state.searchKey && !this.state.selectedRealms.length) {
return this.resetState();
}
if (this.searchKeyIsURL) {
this.getCard.perform(this.state.searchKey);
return;
}
let results: RealmCards[] = [];
let cardFilter = (c: CardDef) => {
return c.title
?.trim()
.toLowerCase()
.includes(this.state.searchKey.trim().toLowerCase());
};
for (let { url, realmInfo, cards } of this.displayedRealms) {
let filteredCards = cards.filter(cardFilter);
if (filteredCards.length) {
results.push({
url,
realmInfo,
cards: filteredCards,
});
}
}
this.state.searchResults = results;
}
@action selectCard(card?: CardDef, event?: MouseEvent | KeyboardEvent): void {
this.state.cardURL = '';
this.state.selectedCard = card;
if (
(event instanceof KeyboardEvent && event?.key === 'Enter') ||
(event instanceof MouseEvent && event?.type === 'dblclick')
) {
this.pick(card);
}
}
@action handleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') {
this.pick(undefined);
}
}
@action pick(card?: CardDef, state?: State) {
let request = state ? state.request : this.state.request;
if (request) {
request.deferred.fulfill(card);
}
// In the 'createNewCard' case, auto-save doesn't follow any specific order,
// so we cannot guarantee that the outer 'createNewCard' process (the top item in the stack) will be saved before the inner one.
// That's why we use state ID to remove state from the stack.
if (state) {
let stateIndex = this.stateStack.findIndex((s) => s.id === state.id);
this.stateStack.splice(stateIndex, 1);
} else {
this.stateStack.pop();
}
}
@action createNew(ref: CodeRef, relativeTo: URL | undefined) {
this.createNewTask.perform(ref, relativeTo);
}
createNewTask = task(
async (
ref: CodeRef,
relativeTo: URL | undefined /* this should be the catalog entry ID */,
) => {
let newCard;
this.state.dismissModal = true;
// We need to store the current state in a variable
// because there is a possibility that in createNewCard,
// users will open the card catalog modal and insert a new state into the stack.
let currentState = this.state;
if (this.state.request.opts?.createNewCard) {
newCard = await this.state.request.opts?.createNewCard(
ref,
relativeTo,
{
isLinkedCard: true,
},
);
} else {
newCard = await createNewCard(ref, relativeTo);
}
this.pick(newCard, currentState);
},
);
}
async function chooseCardTitle(
filter: Filter | undefined,
loader: Loader,
multiSelect?: boolean,
): Promise<string> {
if (!filter) {
return DEFAULT_CHOOOSE_CARD_TITLE;
}
let suggestions = await suggestCardChooserTitle(filter, 0, {
loader,
multiSelect,
});
return (
getSuggestionWithLowestDepth(suggestions) ?? DEFAULT_CHOOOSE_CARD_TITLE
);
}