Skip to content

Commit 013aa2e

Browse files
authored
feat: 🎸 Refactored sessions page filters to load separately (#3053)
1 parent 953e35b commit 013aa2e

File tree

7 files changed

+346
-215
lines changed

7 files changed

+346
-215
lines changed

‎ui/admin/app/controllers/scopes/scope/session-recordings/index.js‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,7 @@ import { action } from '@ember/object';
99
import { service } from '@ember/service';
1010
import { assert } from '@ember/debug';
1111
import { restartableTask } from 'ember-concurrency';
12-
13-
class FilterOptions {
14-
@tracked search;
15-
@tracked _options = [];
16-
#allOptions = new Map();
17-
18-
get options() {
19-
return this._options;
20-
}
21-
22-
set options(newOptions) {
23-
this._options = newOptions;
24-
newOptions.forEach((option) => {
25-
this.#allOptions.set(option.id, option);
26-
});
27-
}
28-
29-
// Keep track of all filter options that are loaded so they can be
30-
// displayed in the selected filters regardless of search input
31-
get allOptions() {
32-
return Array.from(this.#allOptions.values());
33-
}
34-
}
12+
import FilterOptions from 'admin/utils/filter-options';
3513

3614
export default class ScopesScopeSessionRecordingsIndexController extends Controller {
3715
// =services

‎ui/admin/app/controllers/scopes/scope/sessions/index.js‎

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ import { service } from '@ember/service';
88
import { tracked } from '@glimmer/tracking';
99
import {
1010
STATUS_SESSION_ACTIVE,
11-
STATUS_SESSION_PENDING,
1211
STATUS_SESSION_CANCELING,
12+
STATUS_SESSION_PENDING,
1313
statusTypes,
1414
} from 'api/models/session';
1515
import { action } from '@ember/object';
16-
import { notifySuccess, notifyError } from 'core/decorators/notify';
16+
import { notifyError, notifySuccess } from 'core/decorators/notify';
17+
import { restartableTask } from 'ember-concurrency';
18+
import { assert } from '@ember/debug';
19+
import FilterOptions from 'admin/utils/filter-options';
1720

1821
export default class ScopesScopeSessionsIndexController extends Controller {
1922
// =services
2023

2124
@service store;
2225
@service intl;
26+
@service db;
2327

2428
// =attributes
2529

@@ -34,6 +38,9 @@ export default class ScopesScopeSessionsIndexController extends Controller {
3438
'sortDirection',
3539
];
3640

41+
userFilters = new FilterOptions();
42+
targetFilters = new FilterOptions();
43+
3744
@tracked search;
3845
@tracked users = [];
3946
@tracked targets = [];
@@ -54,8 +61,8 @@ export default class ScopesScopeSessionsIndexController extends Controller {
5461
get filters() {
5562
return {
5663
allFilters: {
57-
users: this.model.associatedUsers,
58-
targets: this.model.associatedTargets,
64+
users: this.userFilters.allOptions,
65+
targets: this.targetFilters.allOptions,
5966
status: this.sessionStatusOptions,
6067
},
6168
selectedFilters: {
@@ -77,6 +84,74 @@ export default class ScopesScopeSessionsIndexController extends Controller {
7784
}));
7885
}
7986

87+
/**
88+
* Configurations for different filter options
89+
* @returns {[object]}
90+
*/
91+
get filterConfigs() {
92+
return {
93+
userFilters: {
94+
resource: 'user',
95+
select: [
96+
{ field: 'id', isDistinct: true },
97+
{ field: 'name', isDistinct: true },
98+
],
99+
searchFields: ['id', 'name'],
100+
filters: {
101+
joins: [
102+
{
103+
resource: 'session',
104+
joinOn: 'user_id',
105+
},
106+
],
107+
},
108+
},
109+
targetFilters: {
110+
resource: 'target',
111+
select: [
112+
{ field: 'id', isDistinct: true },
113+
{ field: 'name', isDistinct: true },
114+
],
115+
searchFields: ['id', 'name'],
116+
filters: {
117+
joins: [
118+
{
119+
resource: 'session',
120+
joinOn: 'target_id',
121+
},
122+
],
123+
},
124+
},
125+
};
126+
}
127+
128+
/**
129+
* Generic retrieve function for session filter options
130+
* @param {string} type - The type of options to retrieve (userFilters, targetFilters)
131+
* @param {string} search - Search term
132+
* @returns {Promise<Array>}
133+
*/
134+
async retrieveFilterOptions(type, search) {
135+
const config = this.filterConfigs[type];
136+
assert(`Unknown filter type: ${type}`, config);
137+
138+
return this.db.query(config.resource, {
139+
select: config.select,
140+
query: {
141+
search: { text: search, fields: config.searchFields },
142+
filters: config.filters,
143+
},
144+
page: 1,
145+
pageSize: 250,
146+
});
147+
}
148+
149+
loadItems = restartableTask(async () => {
150+
this.userFilters.options = await this.retrieveFilterOptions('userFilters');
151+
this.targetFilters.options =
152+
await this.retrieveFilterOptions('targetFilters');
153+
});
154+
80155
// =actions
81156

82157
/**
@@ -106,7 +181,7 @@ export default class ScopesScopeSessionsIndexController extends Controller {
106181
*/
107182
@action
108183
refresh() {
109-
this.send('refreshAll');
184+
this.router.refresh('scopes.scope.sessions');
110185
}
111186

112187
/**
@@ -131,4 +206,9 @@ export default class ScopesScopeSessionsIndexController extends Controller {
131206
this.sortDirection = sortOrder;
132207
this.page = 1;
133208
}
209+
210+
onFilterSearch = restartableTask(async (filter, value) => {
211+
this[filter].search = value;
212+
this[filter].options = await this.retrieveFilterOptions(filter, value);
213+
});
134214
}

0 commit comments

Comments
 (0)