Improve search result sorting

This commit is contained in:
Geomitron
2025-01-05 17:44:22 -06:00
parent 30eeca490b
commit e20f27dafe
6 changed files with 40 additions and 43 deletions

View File

@@ -30,9 +30,7 @@
<th *ngIf="hasColumn('length')" [ngClass]="sortDirection" class="cursor-pointer" (click)="onColClicked('length')"> <th *ngIf="hasColumn('length')" [ngClass]="sortDirection" class="cursor-pointer" (click)="onColClicked('length')">
Length (min) <i *ngIf="sortColumn === 'length'" class="bi bi-caret-{{ sortDirection === 'asc' ? 'down' : 'up' }}-fill"></i> Length (min) <i *ngIf="sortColumn === 'length'" class="bi bi-caret-{{ sortDirection === 'asc' ? 'down' : 'up' }}-fill"></i>
</th> </th>
<th *ngIf="hasColumn('difficulty')" [ngClass]="sortDirection" class="cursor-pointer" (click)="onColClicked('difficulty')"> <th *ngIf="hasColumn('difficulty')" [ngClass]="sortDirection" class="cursor-pointer">Difficulty</th>
Difficulty <i *ngIf="sortColumn === 'difficulty'" class="bi bi-caret-{{ sortDirection === 'asc' ? 'down' : 'up' }}-fill"></i>
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>

View File

@@ -1,7 +1,6 @@
import { Component, ElementRef, EventEmitter, HostBinding, HostListener, OnInit, Output, QueryList, ViewChild, ViewChildren } from '@angular/core' import { Component, ElementRef, EventEmitter, HostBinding, HostListener, OnInit, Output, QueryList, ViewChild, ViewChildren } from '@angular/core'
import { Router } from '@angular/router' import { Router } from '@angular/router'
import _ from 'lodash'
import { SettingsService } from 'src-angular/app/core/services/settings.service' import { SettingsService } from 'src-angular/app/core/services/settings.service'
import { ChartData } from 'src-shared/interfaces/search.interface' import { ChartData } from 'src-shared/interfaces/search.interface'
@@ -23,7 +22,7 @@ export class ResultTableComponent implements OnInit {
activeSong: ChartData[] | null = null activeSong: ChartData[] | null = null
sortDirection: 'asc' | 'desc' = 'asc' sortDirection: 'asc' | 'desc' = 'asc'
sortColumn: 'name' | 'artist' | 'album' | 'genre' | 'year' | 'charter' | 'length' | 'difficulty' | null = null sortColumn: 'name' | 'artist' | 'album' | 'genre' | 'year' | 'charter' | 'length' | null = null
constructor( constructor(
public searchService: SearchService, public searchService: SearchService,
@@ -36,13 +35,9 @@ export class ResultTableComponent implements OnInit {
this.searchService.newSearch.subscribe(() => { this.searchService.newSearch.subscribe(() => {
this.resultTableDiv.nativeElement.scrollTop = 0 this.resultTableDiv.nativeElement.scrollTop = 0
this.activeSong = null this.activeSong = null
this.sortDirection = 'asc'
this.sortColumn = null
this.updateSort()
setTimeout(() => this.tableScrolled(), 0) setTimeout(() => this.tableScrolled(), 0)
}) })
this.searchService.updateSearch.subscribe(() => { this.searchService.updateSearch.subscribe(() => {
this.updateSort()
setTimeout(() => this.tableScrolled(), 0) setTimeout(() => this.tableScrolled(), 0)
}) })
} }
@@ -62,47 +57,21 @@ export class ResultTableComponent implements OnInit {
} }
} }
onColClicked(column: 'name' | 'artist' | 'album' | 'genre' | 'year' | 'charter' | 'length' | 'difficulty') { onColClicked(column: 'name' | 'artist' | 'album' | 'genre' | 'year' | 'charter' | 'length') {
if (this.songs.length === 0) { return } if (this.songs.length === 0) { return }
if (this.sortColumn !== column) { if (this.sortColumn !== column) {
this.sortColumn = column this.sortColumn = column
this.sortDirection = 'asc' this.sortDirection = 'asc'
} else if (this.sortDirection === 'desc') { } else if (this.sortDirection === 'asc') {
this.sortDirection = 'asc'
} else {
this.sortDirection = 'desc' this.sortDirection = 'desc'
}
this.updateSort()
}
private updateSort() {
const col = this.sortColumn
if (col !== null) {
console.log(this.getSortColumn())
const groupedSongs = _.orderBy(this.searchService.groupedSongs, s => _.get(s[0], this.getSortColumn()), this.sortDirection)
this.searchService.groupedSongs = groupedSongs
}
}
private getSortColumn(): keyof ChartData {
if (this.sortColumn === 'length') {
return 'notesData.effectiveLength' as keyof ChartData
} else if (this.sortColumn === 'difficulty') {
switch (this.searchService.instrument.value) {
case 'guitar': return 'diff_guitar'
case 'guitarcoop': return 'diff_guitar_coop'
case 'rhythm': return 'diff_rhythm'
case 'bass': return 'diff_bass'
case 'drums': return 'diff_drums'
case 'keys': return 'diff_keys'
case 'guitarghl': return 'diff_guitarghl'
case 'guitarcoopghl': return 'diff_guitar_coop_ghl'
case 'rhythmghl': return 'diff_rhythm_ghl'
case 'bassghl': return 'diff_bassghl'
default: throw 'Invalid instrument'
}
} else { } else {
return this.sortColumn! this.sortDirection = 'asc'
this.sortColumn = null
} }
this.searchService.sortColumn = this.sortColumn
this.searchService.sortDirection = this.sortDirection
this.searchService.reloadSearch()
} }
get allSelected() { get allSelected() {

View File

@@ -224,6 +224,7 @@ export class SearchBarComponent implements OnInit, AfterViewInit {
instrument: this.instrument, instrument: this.instrument,
difficulty: this.difficulty, difficulty: this.difficulty,
drumType: this.drumType, drumType: this.drumType,
sort: this.searchService.sortColumn !== null ? { type: this.searchService.sortColumn, direction: this.searchService.sortDirection } : null,
source: 'bridge' as const, source: 'bridge' as const,
...this.advancedSearchForm.getRawValue(), ...this.advancedSearchForm.getRawValue(),
}).subscribe() }).subscribe()

View File

@@ -33,6 +33,8 @@ export class SearchService {
public instrument: FormControl<Instrument | null> public instrument: FormControl<Instrument | null>
public difficulty: FormControl<Difficulty | null> public difficulty: FormControl<Difficulty | null>
public drumType: FormControl<DrumTypeName | null> public drumType: FormControl<DrumTypeName | null>
public sortDirection: 'asc' | 'desc' = 'asc'
public sortColumn: 'name' | 'artist' | 'album' | 'genre' | 'year' | 'charter' | 'length' | null = null
constructor( constructor(
private http: HttpClient, private http: HttpClient,
@@ -102,6 +104,7 @@ export class SearchService {
instrument: this.instrument.value, instrument: this.instrument.value,
difficulty: this.difficulty.value, difficulty: this.difficulty.value,
drumType: this.drumType.value, drumType: this.drumType.value,
sort: this.sortColumn !== null ? { type: this.sortColumn, direction: this.sortDirection } : null,
source: 'bridge', source: 'bridge',
}).pipe( }).pipe(
catchError((err, caught) => { catchError((err, caught) => {
@@ -212,4 +215,12 @@ export class SearchService {
} }
} }
} }
public reloadSearch() {
if (this.isAdvancedSearch) {
this.advancedSearch(this.lastAdvancedSearch, false).subscribe()
} else {
this.search(this.searchControl.value || '*', false).subscribe()
}
}
} }

View File

@@ -3,6 +3,7 @@ import { z } from 'zod'
import { difficulties, drumTypeNames, instruments } from '../UtilFunctions.js' import { difficulties, drumTypeNames, instruments } from '../UtilFunctions.js'
export const searchSortProperties = ['name', 'artist', 'album', 'genre', 'year', 'charter', 'length', 'modifiedTime'] as const
export const sources = ['website', 'bridge'] as const export const sources = ['website', 'bridge'] as const
export const GeneralSearchSchema = z.object({ export const GeneralSearchSchema = z.object({
@@ -11,6 +12,10 @@ export const GeneralSearchSchema = z.object({
instrument: z.enum(instruments).nullable(), instrument: z.enum(instruments).nullable(),
difficulty: z.enum(difficulties).nullable(), difficulty: z.enum(difficulties).nullable(),
drumType: z.enum(drumTypeNames).nullable(), drumType: z.enum(drumTypeNames).nullable(),
sort: z
.object({ type: z.enum(searchSortProperties), direction: z.enum(['asc', 'desc']) })
.nullable()
.default(null),
source: z.enum(sources).optional(), source: z.enum(sources).optional(),
}) })
export type GeneralSearch = z.infer<typeof GeneralSearchSchema> export type GeneralSearch = z.infer<typeof GeneralSearchSchema>
@@ -27,6 +32,10 @@ export const AdvancedSearchSchema = z.object({
}, { message: 'Invalid instrument list' }).nullable(), }, { message: 'Invalid instrument list' }).nullable(),
difficulty: z.enum(difficulties).nullable(), difficulty: z.enum(difficulties).nullable(),
drumType: z.enum(drumTypeNames).nullable(), drumType: z.enum(drumTypeNames).nullable(),
sort: z
.object({ type: z.enum(searchSortProperties), direction: z.enum(['asc', 'desc']) })
.nullable()
.default(null),
source: z.enum(sources).optional(), source: z.enum(sources).optional(),
name: z.object({ value: z.string(), exact: z.boolean(), exclude: z.boolean() }), name: z.object({ value: z.string(), exact: z.boolean(), exclude: z.boolean() }),
artist: z.object({ value: z.string(), exact: z.boolean(), exclude: z.boolean() }), artist: z.object({ value: z.string(), exact: z.boolean(), exclude: z.boolean() }),

View File

@@ -2,6 +2,7 @@ import { z } from 'zod'
import { difficulties, drumTypeNames, instruments } from './UtilFunctions.js' import { difficulties, drumTypeNames, instruments } from './UtilFunctions.js'
export const searchSortProperties = ['name', 'artist', 'album', 'genre', 'year', 'charter', 'length', 'modifiedTime'] as const
export const sources = ['api'] as const export const sources = ['api'] as const
export const GeneralSearchSchema = z.object({ export const GeneralSearchSchema = z.object({
@@ -11,6 +12,10 @@ export const GeneralSearchSchema = z.object({
instrument: z.enum(instruments).nullable().default(null), instrument: z.enum(instruments).nullable().default(null),
difficulty: z.enum(difficulties).nullable().default(null), difficulty: z.enum(difficulties).nullable().default(null),
drumType: z.enum(drumTypeNames).nullable().default(null), drumType: z.enum(drumTypeNames).nullable().default(null),
sort: z
.object({ type: z.enum(searchSortProperties), direction: z.enum(['asc', 'desc']) })
.nullable()
.default(null),
source: z.enum(sources).optional(), source: z.enum(sources).optional(),
}) })
export type GeneralSearch = z.infer<typeof GeneralSearchSchema> export type GeneralSearch = z.infer<typeof GeneralSearchSchema>
@@ -22,6 +27,10 @@ export const AdvancedSearchSchema = z.object({
instrument: z.enum(instruments).nullable().default(null), instrument: z.enum(instruments).nullable().default(null),
difficulty: z.enum(difficulties).nullable().default(null), difficulty: z.enum(difficulties).nullable().default(null),
drumType: z.enum(drumTypeNames).nullable().default(null), drumType: z.enum(drumTypeNames).nullable().default(null),
sort: z
.object({ type: z.enum(searchSortProperties), direction: z.enum(['asc', 'desc']) })
.nullable()
.default(null),
source: z.enum(sources).optional(), source: z.enum(sources).optional(),
per_page: z.number().positive().lte(250, 'Getting more than 250 results at a time is not supported').optional(), per_page: z.number().positive().lte(250, 'Getting more than 250 results at a time is not supported').optional(),
page: z.number().positive().optional(), page: z.number().positive().optional(),