Skip to content

Commit 5ad05dd

Browse files
authored
Logging & bugfixes for facet bar (#410)
1 parent 194d4e9 commit 5ad05dd

File tree

4 files changed

+102
-62
lines changed

4 files changed

+102
-62
lines changed

src/collection-facets/smart-facets/smart-facet-bar.ts

Lines changed: 83 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import type { CollectionTitles } from '../../data-source/models';
1616
import type { FacetOption, SelectedFacets } from '../../models';
1717
import { updateSelectedFacetBucket } from '../../utils/facet-utils';
1818
import { SmartQueryHeuristicGroup } from './smart-facet-heuristics';
19+
import type { SmartFacetDropdown } from './smart-facet-dropdown';
1920
import type { SmartFacet, SmartFacetEvent } from './models';
21+
import { smartFacetEquals } from './smart-facet-equals';
22+
import { dedupe } from './dedupe';
2023
import filterIcon from '../../assets/img/icons/filter';
2124

2225
import './smart-facet-button';
2326
import './smart-facet-dropdown';
24-
import { smartFacetEquals } from './smart-facet-equals';
25-
import { dedupe } from './dedupe';
27+
import { log } from '../../utils/log';
2628

2729
const fieldPrefixes: Partial<Record<FacetOption, string>> = {
2830
collection: 'Collection: ',
@@ -73,9 +75,12 @@ export class SmartFacetBar extends LitElement {
7375
}
7476

7577
protected willUpdate(changed: PropertyValues): void {
78+
let shouldUpdateSmartFacets = false;
79+
7680
if (changed.has('query')) {
77-
this.updateSmartFacets();
81+
log('query change', changed.get('query'), this.query);
7882
this.lastAggregations = undefined;
83+
shouldUpdateSmartFacets = true;
7984
}
8085

8186
if (
@@ -84,16 +89,25 @@ export class SmartFacetBar extends LitElement {
8489
this.aggregations &&
8590
Object.keys(this.aggregations).length > 0
8691
) {
92+
log('aggs change', changed.get('aggregations'), this.aggregations);
8793
this.lastAggregations = this.aggregations;
94+
shouldUpdateSmartFacets = true;
95+
}
96+
97+
if (shouldUpdateSmartFacets) {
98+
log('should update smart facets, doing so...');
99+
this.updateSmartFacets();
88100
}
89101
}
90102

91103
private async updateSmartFacets(): Promise<void> {
92-
console.log('updating smart facets');
104+
log('updating smart facets');
93105
if (this.query) {
94106
this.heuristicRecs =
95107
await new SmartQueryHeuristicGroup().getRecommendedFacets(this.query);
108+
log('heuristic recs are', this.heuristicRecs);
96109
this.smartFacets = dedupe(this.facetsToDisplay);
110+
log('smart facets are', this.smartFacets);
97111
}
98112
}
99113

@@ -129,6 +143,7 @@ export class SmartFacetBar extends LitElement {
129143
.labelPrefix=${fieldPrefixes[facets[0].facets[0].facetType]}
130144
.activeFacetRef=${facets[0].facets[0]}
131145
@facetClick=${this.facetDropdownClicked}
146+
@dropdownClick=${this.onDropdownClick}
132147
></smart-facet-dropdown>
133148
`;
134149
}
@@ -147,8 +162,6 @@ export class SmartFacetBar extends LitElement {
147162
}
148163

149164
private get facetsToDisplay(): SmartFacet[][] {
150-
if (!this.lastAggregations) return [];
151-
152165
const facets: SmartFacet[][] = [];
153166

154167
if (this.heuristicRecs.length > 0) {
@@ -157,52 +170,54 @@ export class SmartFacetBar extends LitElement {
157170
}
158171
}
159172

160-
const keys = [
161-
'mediatype',
162-
'year',
163-
'language',
164-
'creator',
165-
'subject',
166-
'collection',
167-
];
168-
for (const key of keys) {
169-
const agg = this.lastAggregations[key];
170-
if (!agg) continue;
171-
if (agg.buckets.length === 0) continue;
172-
if (['lending', 'year_histogram'].includes(key)) continue;
173-
if (typeof agg.buckets[0] === 'number') continue;
174-
175-
if (
176-
key === 'mediatype' &&
177-
this.selectedFacets &&
178-
Object.values(this.selectedFacets.mediatype).some(
179-
bucket => bucket.state !== 'none'
180-
)
181-
) {
182-
continue;
183-
}
184-
185-
const facetType = key as FacetOption;
186-
const buckets = agg.buckets as Bucket[];
187-
188-
const unusedBuckets = buckets.filter(b => {
189-
const selectedFacetBucket = this.selectedFacets?.[facetType][b.key];
190-
if (selectedFacetBucket && selectedFacetBucket.state !== 'none') {
191-
return false;
173+
if (this.lastAggregations) {
174+
const keys = [
175+
'mediatype',
176+
'year',
177+
'language',
178+
'creator',
179+
'subject',
180+
'collection',
181+
];
182+
for (const key of keys) {
183+
const agg = this.lastAggregations[key];
184+
if (!agg) continue;
185+
if (agg.buckets.length === 0) continue;
186+
if (['lending', 'year_histogram'].includes(key)) continue;
187+
if (typeof agg.buckets[0] === 'number') continue;
188+
189+
if (
190+
key === 'mediatype' &&
191+
this.selectedFacets &&
192+
Object.values(this.selectedFacets.mediatype).some(
193+
bucket => bucket.state !== 'none'
194+
)
195+
) {
196+
continue;
192197
}
193-
return true;
194-
});
195198

196-
if (facetType === 'mediatype') {
197-
facets.push(
198-
[this.toSmartFacet(facetType, [unusedBuckets[0]])],
199-
[this.toSmartFacet(facetType, [unusedBuckets[1]])]
200-
);
201-
} else if (facetType === 'collection' || facetType === 'subject') {
202-
const topBuckets = unusedBuckets.slice(0, 5);
203-
facets.push(topBuckets.map(b => this.toSmartFacet(facetType, [b])));
204-
} else {
205-
facets.push([this.toSmartFacet(facetType, [unusedBuckets[0]])]);
199+
const facetType = key as FacetOption;
200+
const buckets = agg.buckets as Bucket[];
201+
202+
const unusedBuckets = buckets.filter(b => {
203+
const selectedFacetBucket = this.selectedFacets?.[facetType][b.key];
204+
if (selectedFacetBucket && selectedFacetBucket.state !== 'none') {
205+
return false;
206+
}
207+
return true;
208+
});
209+
210+
if (facetType === 'mediatype') {
211+
facets.push(
212+
[this.toSmartFacet(facetType, [unusedBuckets[0]])],
213+
[this.toSmartFacet(facetType, [unusedBuckets[1]])]
214+
);
215+
} else if (facetType === 'collection' || facetType === 'subject') {
216+
const topBuckets = unusedBuckets.slice(0, 5);
217+
facets.push(topBuckets.map(b => this.toSmartFacet(facetType, [b])));
218+
} else {
219+
facets.push([this.toSmartFacet(facetType, [unusedBuckets[0]])]);
220+
}
206221
}
207222
}
208223

@@ -222,10 +237,6 @@ export class SmartFacetBar extends LitElement {
222237
if (title) displayText = title;
223238
}
224239

225-
// if (prefix && fieldPrefixes[facetType]) {
226-
// displayText = fieldPrefixes[facetType] + displayText;
227-
// }
228-
229240
return {
230241
facetType,
231242
bucketKey: bucket.key.toString(),
@@ -236,10 +247,12 @@ export class SmartFacetBar extends LitElement {
236247
}
237248

238249
private facetClicked(e: CustomEvent<SmartFacetEvent>): void {
239-
this.smartFacets = [
240-
[{ ...e.detail.smartFacet, selected: !e.detail.smartFacet.selected }],
241-
...this.smartFacets.filter(f => f[0] !== e.detail.smartFacet),
242-
];
250+
if (!e.detail.smartFacet.selected) {
251+
this.smartFacets = [
252+
[{ ...e.detail.smartFacet, selected: true }],
253+
...this.smartFacets.filter(f => f[0] !== e.detail.smartFacet),
254+
];
255+
}
243256

244257
for (const facet of e.detail.details) {
245258
this.selectedFacets = updateSelectedFacetBucket(
@@ -283,6 +296,18 @@ export class SmartFacetBar extends LitElement {
283296
this.dispatchEvent(event);
284297
}
285298

299+
private onDropdownClick(e: CustomEvent<SmartFacetDropdown>): void {
300+
log('smart bar: onDropdownClick', e.detail);
301+
this.shadowRoot
302+
?.querySelectorAll('smart-facet-dropdown')
303+
.forEach(dropdown => {
304+
if (dropdown !== e.detail) {
305+
log('closing', dropdown);
306+
(dropdown as SmartFacetDropdown).close();
307+
}
308+
});
309+
}
310+
286311
private filterToggleClicked(): void {
287312
this.dispatchEvent(new CustomEvent('filtersToggled'));
288313
}

src/collection-facets/smart-facets/smart-facet-button.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { css, html, LitElement, CSSResultGroup, nothing } from 'lit';
22
import { customElement, property } from 'lit/decorators.js';
33
import { mediatypeConfig } from '../../mediatype/mediatype-config';
4-
import type { SmartFacet } from './models';
4+
import type { SmartFacet, SmartFacetEvent } from './models';
55

66
function capitalize(str?: string): string | undefined {
77
if (!str) return str;
@@ -75,7 +75,7 @@ export class SmartFacetButton extends LitElement {
7575
this.selected = !this.selected;
7676

7777
this.dispatchEvent(
78-
new CustomEvent('facetClick', {
78+
new CustomEvent<SmartFacetEvent>('facetClick', {
7979
detail: {
8080
smartFacet: this.facetInfo,
8181
details: this.facetInfo.facets.map(f => ({

src/collection-facets/smart-facets/smart-facet-dropdown.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { css, html, LitElement, CSSResultGroup, nothing } from 'lit';
22
import { customElement, property, query } from 'lit/decorators.js';
33
import type { IaDropdown, optionInterface } from '@internetarchive/ia-dropdown';
44
import type { FacetRef, SmartFacet, SmartFacetEvent } from './models';
5+
import { log } from '../../utils/log';
56

67
@customElement('smart-facet-dropdown')
78
export class SmartFacetDropdown extends LitElement {
@@ -36,6 +37,7 @@ export class SmartFacetDropdown extends LitElement {
3637
.options=${this.dropdownOptions}
3738
.selectedOption=${this.activeDropdownOption}
3839
@optionSelected=${this.optionSelected}
40+
@click=${this.onDropdownClick}
3941
>
4042
<span class="dropdown-label" slot="dropdown-label"
4143
>${this.labelPrefix ?? nothing} ${displayText}</span
@@ -105,6 +107,19 @@ export class SmartFacetDropdown extends LitElement {
105107
);
106108
}
107109

110+
private onDropdownClick(): void {
111+
log('smart dropdown: onDropdownClick', this);
112+
this.dispatchEvent(
113+
new CustomEvent<SmartFacetDropdown>('dropdownClick', { detail: this })
114+
);
115+
}
116+
117+
close(): void {
118+
if (this.dropdown) {
119+
this.dropdown.open = false;
120+
}
121+
}
122+
108123
//
109124
// STYLES
110125
//

src/data-source/collection-browser-data-source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,12 +1048,12 @@ export class CollectionBrowserDataSource
10481048
};
10491049
params.uid = await this.requestUID(params, 'hits');
10501050

1051-
log('=== FIRING PAGE REQUEST ===', params);
1051+
// log('=== FIRING PAGE REQUEST ===', params);
10521052
const searchResponse = await this.host.searchService?.search(
10531053
params,
10541054
this.host.searchType
10551055
);
1056-
log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
1056+
// log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
10571057
const success = searchResponse?.success;
10581058

10591059
// This is checking to see if the fetch has been invalidated since it was fired off.

0 commit comments

Comments
 (0)