Skip to content

Commit

Permalink
refactor: adapt to updated api endpoint, improves perf by 100x
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst committed May 15, 2024
1 parent 712b579 commit 0116b98
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 221 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"npm": "10.x"
},
"dependencies": {
"@acdh-oeaw/lib": "^0.1.11",
"@next/bundle-analyzer": "^13.0.5",
"@popperjs/core": "^2.11.6",
"@react-aria/button": "^3.6.3",
Expand Down
93 changes: 46 additions & 47 deletions src/api/sola/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,7 @@ interface GetByIdRequestConfig<T extends Record<string, unknown> = Record<string
}

export interface Results<
T extends
| SolaEntity
| SolaPassageSearchResult
| SolaRelation
| SolaText
| SolaUser
| SolaVocabulary,
T extends SolaEntity | SolaListEntity | SolaRelation | SolaText | SolaUser | SolaVocabulary,
> {
limit: number
offset: number
Expand All @@ -198,41 +192,6 @@ export interface Results<
results: Array<T>
}

export type SolaPassageSearchResult = {
id: number
type: SolaEntityType
entity_type: SolaEntityType
name: string
primary_date: string | null
end_date: string | null
end_date_written: string | null
start_date: string | null
start_date_written: string | null
publication: {
id: number
name: string
}
}
export type SolaPassagesSearchResults = Results<SolaPassageSearchResult>
export async function searchSolaPassages({
query,
locale,
options,
}: GetAllRequestConfig): Promise<SolaPassagesSearchResults> {
const data = await request<SolaPassagesSearchResults>({
path: '/apis/api2/search/',
baseUrl,
query,
options,
})
return {
...data,
results: data.results.map((result) => {
return addEntityType(result as unknown as SolaEntityBase, result.entity_type, locale)
}) as unknown as Array<SolaPassageSearchResult>,
}
}

export async function getSolaEvents({
query,
locale,
Expand Down Expand Up @@ -359,6 +318,49 @@ export async function getSolaPublications({
}
}

export type SolaListEntity = Pick<
SolaEntity,
| 'end_date_written'
| 'end_date'
| 'id'
| 'name'
| 'primary_date'
| 'start_date_written'
| 'start_date'
| 'type'
>

type GetSolaEntitiesQuery = {
search?: string
name__icontains?: string
kind__id__in?: Array<number>
topic__id__in?: Array<number>
publication_set__id__in?: Array<number>
publication_set__person_set__id__in?: Array<number>
publication_relationtype_set__id?: number
}

type SolaListEntitiesResponse = Results<SolaListEntity>

export async function getSolaEntities({
query,
locale,
options,
}: GetAllRequestConfig<GetSolaEntitiesQuery>): Promise<SolaListEntitiesResponse> {
const data = await request<SolaListEntitiesResponse>({
path: '/apis/api2/entity_list/',
baseUrl,
query,
options,
})
return {
...data,
results: data.results.map((result) => {
return localise(result, locale)
}),
}
}

export async function getSolaEventById({
id,
query,
Expand Down Expand Up @@ -622,10 +624,7 @@ export async function getSolaUsers({
/**
* Until the backend implements proper i18n, manually map the `name` field.
*/
function localise<T extends SolaEntityBase | SolaPassageSearchResult | SolaVocabulary>(
entity: T,
locale: SiteLocale,
) {
function localise<T extends { name: string }>(entity: T, locale: SiteLocale) {
if (locale === 'de') return entity
const nameEnglish = (entity as { name_english?: string | null }).name_english
if (nameEnglish != null && nameEnglish.length > 0) {
Expand All @@ -637,7 +636,7 @@ function localise<T extends SolaEntityBase | SolaPassageSearchResult | SolaVocab
/**
* Adds entity type and localises entity label.
*/
function addEntityType<T extends SolaEntityBase | SolaPassageSearchResult>(
function addEntityType<T extends SolaEntityBase>(
entity: T,
type: SolaEntityType,
locale: SiteLocale,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/data/useSortedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMemo } from 'react'
/**
* Returns object values, sorted by specified key.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useSortedData<T extends Record<string, any>, K extends keyof T>(
map: Record<string, T> | undefined,
key: K,
Expand All @@ -12,6 +13,7 @@ export function useSortedData<T extends Record<string, any>, K extends keyof T>(
}, [map, key])
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function sort<T extends Record<string, any>, K extends keyof T>(
map: Record<string, T> | undefined,
key: K,
Expand Down
Loading

0 comments on commit 0116b98

Please sign in to comment.