Skip to content

Commit

Permalink
Merge pull request #423 from poap-xyz/release/v1.9.0
Browse files Browse the repository at this point in the history
Release v1.9.0
  • Loading branch information
jm42 authored Dec 9, 2024
2 parents 56b72d5 + 5aeac95 commit 5ef3184
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 304 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poap-family",
"version": "1.18.0",
"version": "1.19.0",
"author": {
"name": "POAP",
"url": "https://poap.xyz"
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useState } from 'react'
import { fetchEvent as loadEvent } from 'loaders/event'
import { AbortedError } from 'models/error'
import { Drop } from 'models/drop'
import { fetchDrop } from 'loaders/drop'

function useEvent(eventId?: number): {
loadingEvent: boolean
Expand All @@ -24,7 +24,7 @@ function useEvent(eventId?: number): {
setLoading(true)
setError(null)
setEvent(null)
loadEvent(
fetchDrop(
newEventId ?? eventId,
/*includeDescription*/false,
controller.signal
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useEventSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState } from 'react'
import { SEARCH_LIMIT } from 'models/event'
import { Drop } from 'models/drop'
import { AbortedError } from 'models/error'
import { searchEvents as loadSearchEvents } from 'loaders/event'
import { searchDrops } from 'loaders/drop'

function useEventSearch(query?: string, page: number = 1): {
loadingEventSearch: boolean
Expand Down Expand Up @@ -30,7 +30,7 @@ function useEventSearch(query?: string, page: number = 1): {
if (total == null || offset <= total) {
controller = new AbortController()
setLoading(true)
loadSearchEvents(
searchDrops(
newQuery ?? query,
controller.signal,
offset,
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useEventsInCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function useEventsInCommon(
() => {
for (const eventId of eventIds) {
if (eventsOwners[eventId] == null) {
console.error('Missing event owners', { eventId })
continue
}
if (
Expand Down
7 changes: 2 additions & 5 deletions src/loaders/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,10 @@ export async function getInCommonEventsWithEvents(
totalEvents
)
} else {
resolved = true
inCommonStream.close()
reject(new Error(
console.info(
`Received empty list of events ` +
`when streaming event '${eventId}' in common`
))
return
)
}
}
if (
Expand Down
29 changes: 20 additions & 9 deletions src/loaders/compass.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { AbortedError, HttpError } from 'models/error'
import { COMPASS_KEY, COMPASS_URL } from 'models/compass'

export async function requestCompass(query: string, variables: Record<string, unknown>, abortSignal: AbortSignal): Promise<object> {
export async function requestCompass(
query: string,
variables: Record<string, unknown>,
abortSignal?: AbortSignal,
): Promise<object> {
let response: Response
try {
response = await fetch(COMPASS_URL, {
Expand Down Expand Up @@ -103,7 +107,7 @@ export async function queryCompass<T>(
Model: (data: unknown) => T,
query: string,
variables: Record<string, unknown>,
abortSignal: AbortSignal
abortSignal?: AbortSignal
): Promise<T> {
const results = await requestCompass(query, variables, abortSignal)

Expand All @@ -124,8 +128,8 @@ export async function queryFirstCompass<T>(
Model: (data: unknown) => T,
query: string,
variables: Record<string, unknown>,
defaultValue: T,
abortSignal: AbortSignal
defaultValue?: T,
abortSignal?: AbortSignal
): Promise<T> {
const FirstModel = (data: unknown): T =>{
if (data == null || !Array.isArray(data)) {
Expand All @@ -135,7 +139,11 @@ export async function queryFirstCompass<T>(
}

if (data.length === 0) {
return defaultValue
if (defaultValue) {
return defaultValue
}

throw new HttpError(`Model ${name} empty results`, { status: 404 })
}

return Model(data[0])
Expand All @@ -155,7 +163,7 @@ export async function queryManyCompass<T>(
Model: (data: unknown) => T,
query: string,
variables: Record<string, unknown>,
abortSignal: AbortSignal
abortSignal?: AbortSignal
): Promise<T[]> {
const ManyModel = (data: unknown): T[] => {
if (data == null || !Array.isArray(data)) {
Expand All @@ -178,7 +186,7 @@ export async function queryAllCompass<T>(
offsetKey: string,
limit: number,
total: number,
abortSignal: AbortSignal
abortSignal?: AbortSignal
): Promise<T[]> {
let results: T[] = []
let pageCount = 0
Expand Down Expand Up @@ -219,7 +227,7 @@ export async function queryAggregateCountCompass(
name: string,
query: string,
variables: Record<string, unknown>,
abortSignal: AbortSignal
abortSignal?: AbortSignal
): Promise<number> {
const AggregateCountModel = (data: unknown): number => {
if (
Expand Down Expand Up @@ -248,7 +256,10 @@ export async function queryAggregateCountCompass(
)
}

export async function countCompass(target: string, abortSignal: AbortSignal): Promise<number> {
export async function countCompass(
target: string,
abortSignal?: AbortSignal,
): Promise<number> {
return await queryAggregateCountCompass(
`${target}_aggregate`,
`
Expand Down
198 changes: 198 additions & 0 deletions src/loaders/drop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import { DEFAULT_COMPASS_LIMIT } from 'models/compass'
import { DEFAULT_DROP_LIMIT, DEFAULT_SEARCH_LIMIT } from 'models/event'
import { Drop, parseDrop } from 'models/drop'
import { queryAggregateCountCompass, queryFirstCompass, queryManyCompass } from 'loaders/compass'
import { HttpError } from 'models/error'

export async function searchDrops(
query: string,
abortSignal: AbortSignal,
offset: number = 0,
limit: number = Math.min(DEFAULT_SEARCH_LIMIT, DEFAULT_COMPASS_LIMIT),
): Promise<{
items: Drop[]
total: number
offset: number
limit: number
}> {
const [items, total] = await Promise.all([
queryManyCompass(
'search_drops',
(data: unknown) => parseDrop(data, /*includeDescription*/true),
`
query SearchDrops(
$limit: Int!
$offset: Int!
$query: String!
) {
search_drops(
limit: $limit
offset: $offset
args: {
search: $query
}
) {
id
name
description
image_url
city
country
start_date
end_date
expiry_date
drop_image {
gateways {
type
url
}
}
}
}
`,
{
query,
offset,
limit,
},
abortSignal
),
queryAggregateCountCompass(
'search_drops_aggregate',
`
query SearchDropsCount($query: String!) {
search_drops_aggregate(args: { search: $query }) {
aggregate {
count
}
}
}
`,
{
query,
},
abortSignal
),
])
return {
items,
total,
offset,
limit,
}
}

export async function fetchDropsOrErrors(
dropIds: number[],
limit: number = Math.min(DEFAULT_DROP_LIMIT, DEFAULT_COMPASS_LIMIT),
): Promise<[
Record<number, Drop>,
Record<number, Error>
]> {
const eventsMap: Record<number, Drop> = {}
const errorsMap: Record<number, Error> = {}

for (let i = 0; i < dropIds.length; i += limit) {
const ids = dropIds.slice(i, i + limit)

if (ids.length === 0) {
break
}

try {
const drops = await queryManyCompass(
'drops',
(data: unknown): Drop => parseDrop(data, /*includeDescription*/false),
`
query FetchDrops($dropIds: [Int!]) {
drops(where: { id: { _in: $dropIds } }) {
id
name
description
image_url
city
country
start_date
end_date
expiry_date
drop_image {
gateways {
type
url
}
}
}
}
`,
{
dropIds: ids,
},
)

for (const id of ids) {
const drop = drops.find((drop) => drop.id === id)

if (!drop) {
errorsMap[id] = new HttpError(`Drop '${id}' not found`, {
status: 404,
})
} else {
eventsMap[id] = drop
}
}
} catch (err: unknown) {
for (const id of ids) {
errorsMap[id] = err instanceof Error
? err
: new Error(`Failed to fetch drop '${id}'`, { cause: err })
}
}
}

return [eventsMap, errorsMap]
}

export async function fetchDrop(
dropId: number,
includeDescription: boolean,
abortSignal?: AbortSignal,
): Promise<Drop | null> {
try {
return await queryFirstCompass(
`drops`,
(data: unknown): Drop => parseDrop(data, includeDescription),
`
query FetchDrop($dropId: Int!) {
drops(limit: 1, where: { id: { _eq: $dropId } }) {
id
name
description
image_url
city
country
start_date
end_date
expiry_date
drop_image {
gateways {
type
url
}
}
}
}
`,
{
dropId,
},
undefined,
abortSignal,
)
} catch (err: unknown) {
if (err instanceof HttpError && err.status === 404) {
return null
}

throw err
}
}
Loading

0 comments on commit 5ef3184

Please sign in to comment.