Skip to content

Commit

Permalink
Move getFaviconClass out of render and simplify
Browse files Browse the repository at this point in the history
While I'm not sure if this is faster than three nested .some calls,
it shouldn't be a lot slower and I'd say it makes the code at least somewhat
more clear.

This does no longer prioritize blog over review, but that probably doesn't
matter since I'm not sure those can exist at the same time for the same
entity type and even if they could, something being a blog and review
for the same entity at the same time would just be bad data, so it's irrelevant
which icon it shows.
  • Loading branch information
reosarevok committed Nov 14, 2024
1 parent 18bd588 commit b4047b5
Showing 1 changed file with 39 additions and 51 deletions.
90 changes: 39 additions & 51 deletions root/static/scripts/edit/externalLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,42 @@ type LinksEditorState = {
+links: $ReadOnlyArray<LinkStateT>,
};

function getFaviconClass(
relationships: $ReadOnlyArray<LinkRelationshipT>,
url: string,
) {
let faviconClass = '';

for (const relationship of relationships) {
const linkType = relationship.type
? linkedEntities.link_type[relationship.type]
: null;

if (linkType) {
// If we find a homepage, that's the icon we want and we're done
if (/^official (?:homepage|site)$/.test(linkType.name)) {
return 'home';
} else if (linkType.name === 'blog') {
faviconClass = 'blog';
} else if (linkType.name === 'review') {
faviconClass = 'review';
}
}
}

if (nonEmpty(faviconClass)) {
return faviconClass;
}

for (const key of Object.keys(FAVICON_CLASSES)) {
if (url.indexOf(key) > 0) {
return FAVICON_CLASSES[key];
}
}

return 'no';
}

export class _ExternalLinksEditor
extends React.Component<LinksEditorProps, LinksEditorState> {
creditableEntityProp: 'entity0_credit' | 'entity1_credit' | null;
Expand Down Expand Up @@ -1428,57 +1464,9 @@ export class ExternalLink extends React.Component<LinkProps> {
});
const firstLink = props.relationships[0];

let faviconClass: string | void;
if (notEmpty) {
const isHomepage = props.relationships.some(link => {
const linkType = link.type
? linkedEntities.link_type[link.type]
: null;
if (linkType) {
return /^official (?:homepage|site)$/.test(linkType.name);
}
return false;
});
if (isHomepage) {
faviconClass = 'home';
} else {
const isBlog = props.relationships.some(link => {
const linkType = link.type
? linkedEntities.link_type[link.type]
: null;
if (linkType) {
return /^blog$/.test(linkType.name);
}
return false;
});
if (isBlog) {
faviconClass = 'blog';
} else {
const isReview = props.relationships.some(link => {
const linkType = link.type
? linkedEntities.link_type[link.type]
: null;
if (linkType) {
return /^review$/.test(linkType.name);
}
return false;
});
if (isReview) {
faviconClass = 'review';
} else {
for (const key of Object.keys(FAVICON_CLASSES)) {
if (props.url.indexOf(key) > 0) {
faviconClass = FAVICON_CLASSES[key];
break;
}
}
if (!faviconClass) {
faviconClass = 'no';
}
}
}
}
}
const faviconClass = notEmpty
? getFaviconClass(props.relationships, props.url)
: null;

return (
<>
Expand Down

0 comments on commit b4047b5

Please sign in to comment.