mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
refactor(media utils): memoize once, not twice
This commit is contained in:
@@ -45,45 +45,42 @@ const getPatternThumbnailUrl = (url: URL): string | undefined => {
|
||||
}
|
||||
};
|
||||
|
||||
const getLinkMediaInfo = (link: string): CommentMediaInfo | undefined => {
|
||||
if (!isValidURL(link)) {
|
||||
return;
|
||||
}
|
||||
const url = new URL(link);
|
||||
let patternThumbnailUrl: string | undefined;
|
||||
let type: string = 'webpage';
|
||||
let mime: string | undefined;
|
||||
const getLinkMediaInfo = memoize(
|
||||
(link: string): CommentMediaInfo | undefined => {
|
||||
if (!isValidURL(link)) {
|
||||
return;
|
||||
}
|
||||
const url = new URL(link);
|
||||
let patternThumbnailUrl: string | undefined;
|
||||
let type: string = 'webpage';
|
||||
let mime: string | undefined;
|
||||
|
||||
// Check for common dynamic image URL patterns
|
||||
if (link.includes('/_next/image?')) {
|
||||
// Next.js Image component
|
||||
return { url: link, type: 'image' };
|
||||
}
|
||||
|
||||
try {
|
||||
mime = extName(url.pathname.slice(url.pathname.lastIndexOf('/') + 1))[0]?.mime;
|
||||
if (mime) {
|
||||
if (mime.startsWith('image')) {
|
||||
type = mime === 'image/gif' ? 'gif' : 'image';
|
||||
} else if (mime.startsWith('video')) {
|
||||
type = 'video';
|
||||
} else if (mime.startsWith('audio')) {
|
||||
type = 'audio';
|
||||
try {
|
||||
mime = extName(url.pathname.slice(url.pathname.lastIndexOf('/') + 1))[0]?.mime;
|
||||
if (mime) {
|
||||
if (mime.startsWith('image')) {
|
||||
type = mime === 'image/gif' ? 'gif' : 'image';
|
||||
} else if (mime.startsWith('video')) {
|
||||
type = 'video';
|
||||
} else if (mime.startsWith('audio')) {
|
||||
type = 'audio';
|
||||
}
|
||||
}
|
||||
|
||||
if (canEmbed(url) || url.host.startsWith('yt.')) {
|
||||
type = 'iframe';
|
||||
patternThumbnailUrl = getPatternThumbnailUrl(url);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
if (canEmbed(url) || url.host.startsWith('yt.')) {
|
||||
type = 'iframe';
|
||||
patternThumbnailUrl = getPatternThumbnailUrl(url);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return { url: link, type, patternThumbnailUrl };
|
||||
},
|
||||
{ max: 1000 },
|
||||
);
|
||||
|
||||
return { url: link, type, patternThumbnailUrl };
|
||||
};
|
||||
|
||||
const getCommentMediaInfo = (comment: Comment): CommentMediaInfo | undefined => {
|
||||
export const getCommentMediaInfo = (comment: Comment): CommentMediaInfo | undefined => {
|
||||
if (!comment?.thumbnailUrl && !comment?.link) {
|
||||
return;
|
||||
}
|
||||
@@ -94,6 +91,3 @@ const getCommentMediaInfo = (comment: Comment): CommentMediaInfo | undefined =>
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
export const getCommentMediaInfoMemoized = memoize(getCommentMediaInfo, { max: 1000 });
|
||||
export const getLinkMediaInfoMemoized = memoize(getLinkMediaInfo, { max: 1000 });
|
||||
|
||||
Reference in New Issue
Block a user