Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/api/artistSearch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const searchResultSchema = z.object({
provider: providerSchema,
candidates: z.array(candidateSchema),
error: z.string().optional(),
rateLimitRetryAfter: z.number().optional(),
});
export type SearchResult = z.infer<typeof searchResultSchema>;

Expand Down
30 changes: 26 additions & 4 deletions src/pages/admin/festivals/LinkWizard/useProviderCandidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function useProviderCandidates(

const isLoading = batchQueryResult.isLoading || customResult.isLoading;

const { candidates, error } = resolveProviderResult({
const { candidates, error, rateLimitRetryAfter } = resolveProviderResult({
provider,
artistName,
customSearch,
Expand All @@ -42,7 +42,9 @@ export function useProviderCandidates(
setCustomSearch(query);
}

return { candidates, error, isLoading, search };
const displayError = buildErrorMessage(error, rateLimitRetryAfter);

return { candidates, error: displayError, isLoading, search };
}

interface ResolveProviderResultArgs {
Expand All @@ -62,6 +64,7 @@ function resolveProviderResult({
}: ResolveProviderResultArgs): {
candidates: Candidate[];
error?: string | undefined;
rateLimitRetryAfter?: number | undefined;
} {
const providerLabel = PROVIDER_LABELS[provider];

Expand All @@ -75,7 +78,11 @@ function resolveProviderResult({
const result = customResult.data?.results.find(
(r) => r.provider === provider,
);
return { candidates: result?.candidates ?? [], error: result?.error };
return {
candidates: result?.candidates ?? [],
error: result?.error,
rateLimitRetryAfter: result?.rateLimitRetryAfter,
};
}

if (batchQueryResult.isError) {
Expand All @@ -88,5 +95,20 @@ function resolveProviderResult({
const result = batchQueryResult.data?.results.find(
(r) => r.artistName === artistName && r.provider === provider,
);
return { candidates: result?.candidates ?? [], error: result?.error };
return {
candidates: result?.candidates ?? [],
error: result?.error,
rateLimitRetryAfter: result?.rateLimitRetryAfter,
};
}

function buildErrorMessage(
error?: string,
retryAfterSeconds?: number,
): string | undefined {
if (retryAfterSeconds) {
return `Rate limited. Try again in ${retryAfterSeconds} second${retryAfterSeconds > 1 ? "s" : ""}.`;
}

return error;
}
Loading
Loading