Skip to content

Commit 95f572d

Browse files
committed
added search filters
1 parent 15165ea commit 95f572d

File tree

1 file changed

+104
-9
lines changed

1 file changed

+104
-9
lines changed

src/Komga/komga.ts

Lines changed: 104 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ import {
3838
getBookPages,
3939
getBooks as getBooksList,
4040
getBooksOnDeck,
41+
getCollections,
42+
getGenres,
43+
getLibraries,
4144
getSeriesById as getOneSeries,
4245
getSeries as getSeriesList,
4346
getSeriesNew,
47+
getSeriesTags,
4448
getSeriesUpdated,
4549
} from './sdk/index.js'
4650
import { client } from './sdk/client.gen.js'
@@ -120,12 +124,12 @@ export class KomgaExtension implements IKomgaExtension {
120124
]
121125
// For each tag, we append a type identifier to its id and capitalize its label
122126
tagSections[0].tags = metadata.genres.map((elem: string) => ({
123-
id: 'genre-' + encodeURIComponent(elem),
127+
id: 'genre-' + btoa(elem),
124128
title: capitalize(elem),
125129
}))
126130

127131
tagSections[1].tags = metadata.tags.map((elem: string) => ({
128-
id: 'tag-' + encodeURIComponent(elem),
132+
id: 'tag-' + btoa(elem),
129133
title: capitalize(elem),
130134
}))
131135

@@ -160,7 +164,98 @@ export class KomgaExtension implements IKomgaExtension {
160164
}
161165

162166
async getSearchFilters(): Promise<SearchFilter[]> {
163-
return []
167+
// This function is called on the homepage and should not throw if the server is unavailable
168+
// We define four types of tags:
169+
// - `genre`
170+
// - `tag`
171+
// - `collection`
172+
// - `library`
173+
// To be able to make the difference between theses types, we append `genre-` or `tag-` at the beginning of the tag id
174+
175+
const { data: genresResult, error: genresError } = await getGenres()
176+
if (!genresResult) {
177+
throw new Error(JSON.stringify(genresError, undefined, 2))
178+
}
179+
180+
const { data: tagsResult, error: tagsError } = await getSeriesTags()
181+
if (!tagsResult) {
182+
throw new Error(JSON.stringify(tagsError, undefined, 2))
183+
}
184+
185+
const { data: collectionResult, error: collectionError } =
186+
await getCollections()
187+
if (!collectionResult) {
188+
throw new Error(JSON.stringify(collectionError, undefined, 2))
189+
}
190+
191+
const { data: libraryResult, error: libraryError } = await getLibraries()
192+
if (!libraryResult) {
193+
throw new Error(JSON.stringify(libraryError, undefined, 2))
194+
}
195+
196+
const genreSearchFilter: SearchFilter = {
197+
type: 'multiselect',
198+
allowEmptySelection: true,
199+
allowExclusion: true,
200+
id: 'genre',
201+
title: 'Genres',
202+
maximum: undefined,
203+
options: genresResult.map((elem) => ({
204+
id: 'genre-' + btoa(elem),
205+
value: capitalize(elem),
206+
})),
207+
value: {},
208+
}
209+
210+
const tagsSearchFilter: SearchFilter = {
211+
type: 'multiselect',
212+
allowEmptySelection: true,
213+
allowExclusion: true,
214+
id: 'tags',
215+
title: 'Tags',
216+
maximum: undefined,
217+
options: tagsResult.map((elem) => ({
218+
id: 'tag-' + btoa(elem),
219+
value: capitalize(elem),
220+
})),
221+
value: {},
222+
}
223+
224+
const collectionsSearchFilter: SearchFilter = {
225+
type: 'multiselect',
226+
allowEmptySelection: true,
227+
allowExclusion: true,
228+
id: 'collections',
229+
title: 'Collections',
230+
maximum: undefined,
231+
options:
232+
collectionResult.content?.map((elem) => ({
233+
id: 'collection-' + btoa(elem.id),
234+
value: capitalize(elem.name),
235+
})) ?? [],
236+
value: {},
237+
}
238+
239+
const librarySearchFilter: SearchFilter = {
240+
type: 'multiselect',
241+
allowEmptySelection: true,
242+
allowExclusion: true,
243+
id: 'library',
244+
title: 'Libraries',
245+
maximum: undefined,
246+
options: libraryResult.map((elem) => ({
247+
id: 'library-' + btoa(elem.id),
248+
value: capitalize(elem.name),
249+
})),
250+
value: {},
251+
}
252+
253+
return [
254+
genreSearchFilter,
255+
tagsSearchFilter,
256+
librarySearchFilter,
257+
collectionsSearchFilter,
258+
]
164259
}
165260

166261
async getSearchResults(
@@ -184,25 +279,25 @@ export class KomgaExtension implements IKomgaExtension {
184279
const keys = Object.keys(value)
185280
for (const key of keys) {
186281
const operator = value[key]! == 'included' ? 'is' : 'isNot'
187-
282+
console.log(key)
188283
// There are two types of tags: `tag` and `genre`
189284
if (key.substring(0, 4) == 'tag-') {
190-
const tag = encodeURIComponent(key.substring(4))
285+
const tag = encodeURIComponent(atob(key.substring(4)))
191286
filters.push({ tag: { operator, value: tag } })
192287
}
193288

194289
if (key.substring(0, 6) == 'genre-') {
195-
const genre = encodeURIComponent(key.substring(6))
290+
const genre = encodeURIComponent(atob(key.substring(6)))
196291
filters.push({ tag: { operator, value: genre } })
197292
}
198293

199294
if (key.substring(0, 11) == 'collection-') {
200-
const collectionId = encodeURIComponent(key.substring(11))
295+
const collectionId = encodeURIComponent(atob(key.substring(11)))
201296
filters.push({ tag: { operator, value: collectionId } })
202297
}
203298

204299
if (key.substring(0, 8) == 'library-') {
205-
const libraryId = encodeURIComponent(key.substring(8))
300+
const libraryId = encodeURIComponent(atob(key.substring(8)))
206301
filters.push({ tag: { operator, value: libraryId } })
207302
}
208303
}
@@ -287,7 +382,7 @@ export class KomgaExtension implements IKomgaExtension {
287382
creationDate: new Date(book.fileLastModified),
288383
sortingIndex: book.metadata.numberSort,
289384
sourceManga: sourceManga,
290-
version: languageCode
385+
version: languageCode,
291386
})
292387
}
293388

0 commit comments

Comments
 (0)