Skip to content

Commit 45537d9

Browse files
committed
fix(web): prevent keyword URL duplication in entity mapper
Strip entity type prefix from OpenAlex IDs that include path segments (e.g., keywords/machine-learning) to prevent duplication when EntityCard constructs the final path as /${entityType}/${id}. Affects keywords, domains, fields, and subfields entity types.
1 parent b66d4ce commit 45537d9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

apps/web/src/utils/entity-mappers.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,22 @@ const singularToPluralEntityType: Record<string, EntityType> = {
148148
export const transformAutocompleteResultToGridItem = (result: AutocompleteResult): EntityGridItem => {
149149
const entityType = singularToPluralEntityType[result.entity_type] || (result.entity_type as EntityType)
150150

151+
// Extract ID from OpenAlex URL, removing the base URL
152+
let id = result.id.replace("https://openalex.org/", "")
153+
154+
// For entity types where the ID includes the type prefix (e.g., "keywords/machine-learning"),
155+
// strip it since EntityCard will add it back via entityType
156+
if (id.includes("/")) {
157+
const parts = id.split("/")
158+
// Check if first part matches a known entity type plural
159+
const knownPrefixes = ["keywords", "domains", "fields", "subfields"]
160+
if (knownPrefixes.includes(parts[0])) {
161+
id = parts.slice(1).join("/")
162+
}
163+
}
164+
151165
return {
152-
id: result.id.replace("https://openalex.org/", ""),
166+
id,
153167
displayName: result.display_name,
154168
entityType,
155169
worksCount: result.works_count,

0 commit comments

Comments
 (0)