Squeeze all the micro-perf
This commit is contained in:
parent
a2d995ec07
commit
5cfcfdc98b
|
@ -1,3 +1,5 @@
|
||||||
|
import { memo } from 'preact/compat';
|
||||||
|
|
||||||
function EmojiText({ text, emojis }) {
|
function EmojiText({ text, emojis }) {
|
||||||
if (!text) return '';
|
if (!text) return '';
|
||||||
if (!emojis?.length) return text;
|
if (!emojis?.length) return text;
|
||||||
|
@ -31,4 +33,9 @@ function EmojiText({ text, emojis }) {
|
||||||
return elements;
|
return elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default EmojiText;
|
export default memo(
|
||||||
|
EmojiText,
|
||||||
|
(oldProps, newProps) =>
|
||||||
|
oldProps.text === newProps.text &&
|
||||||
|
oldProps.emojis?.length === newProps.emojis?.length,
|
||||||
|
);
|
||||||
|
|
|
@ -15,11 +15,9 @@ const ICONDATA = {};
|
||||||
|
|
||||||
// Memoize the dangerouslySetInnerHTML of the SVGs
|
// Memoize the dangerouslySetInnerHTML of the SVGs
|
||||||
const SVGICon = moize(
|
const SVGICon = moize(
|
||||||
function ({ size, width, height, body, rotate, flip }) {
|
function ({ width, height, body, rotate, flip }) {
|
||||||
return (
|
return (
|
||||||
<svg
|
<svg
|
||||||
// width={size}
|
|
||||||
// height={size}
|
|
||||||
viewBox={`0 0 ${width} ${height}`}
|
viewBox={`0 0 ${width} ${height}`}
|
||||||
dangerouslySetInnerHTML={{ __html: body }}
|
dangerouslySetInnerHTML={{ __html: body }}
|
||||||
style={{
|
style={{
|
||||||
|
@ -33,6 +31,7 @@ const SVGICon = moize(
|
||||||
{
|
{
|
||||||
isShallowEqual: true,
|
isShallowEqual: true,
|
||||||
maxSize: Object.keys(ICONS).length,
|
maxSize: Object.keys(ICONS).length,
|
||||||
|
matchesArg: (cacheKeyArg, keyArg) => cacheKeyArg.icon === keyArg.icon,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -93,7 +92,7 @@ function Icon({
|
||||||
// }}
|
// }}
|
||||||
// />
|
// />
|
||||||
<SVGICon
|
<SVGICon
|
||||||
size={iconSize}
|
icon={icon}
|
||||||
width={iconData.width}
|
width={iconData.width}
|
||||||
height={iconData.height}
|
height={iconData.height}
|
||||||
body={iconData.body}
|
body={iconData.body}
|
||||||
|
|
|
@ -97,4 +97,9 @@ function NameText({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default memo(NameText);
|
export default memo(NameText, (oldProps, newProps) => {
|
||||||
|
// Only care about account.id, the other props usually don't change
|
||||||
|
const { account } = oldProps;
|
||||||
|
const { account: newAccount } = newProps;
|
||||||
|
return account?.acct === newAccount?.acct;
|
||||||
|
});
|
||||||
|
|
|
@ -8,6 +8,7 @@ import dayjs from 'dayjs';
|
||||||
import dayjsTwitter from 'dayjs-twitter';
|
import dayjsTwitter from 'dayjs-twitter';
|
||||||
import localizedFormat from 'dayjs/plugin/localizedFormat';
|
import localizedFormat from 'dayjs/plugin/localizedFormat';
|
||||||
import relativeTime from 'dayjs/plugin/relativeTime';
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
||||||
|
import { useMemo } from 'preact/hooks';
|
||||||
|
|
||||||
dayjs.extend(dayjsTwitter);
|
dayjs.extend(dayjsTwitter);
|
||||||
dayjs.extend(localizedFormat);
|
dayjs.extend(localizedFormat);
|
||||||
|
@ -17,23 +18,25 @@ const dtf = new Intl.DateTimeFormat();
|
||||||
|
|
||||||
export default function RelativeTime({ datetime, format }) {
|
export default function RelativeTime({ datetime, format }) {
|
||||||
if (!datetime) return null;
|
if (!datetime) return null;
|
||||||
const date = dayjs(datetime);
|
const date = useMemo(() => dayjs(datetime), [datetime]);
|
||||||
let dateStr;
|
const dateStr = useMemo(() => {
|
||||||
if (format === 'micro') {
|
if (format === 'micro') {
|
||||||
// If date <= 1 day ago or day is within this year
|
// If date <= 1 day ago or day is within this year
|
||||||
const now = dayjs();
|
const now = dayjs();
|
||||||
const dayDiff = now.diff(date, 'day');
|
const dayDiff = now.diff(date, 'day');
|
||||||
if (dayDiff <= 1 || now.year() === date.year()) {
|
if (dayDiff <= 1 || now.year() === date.year()) {
|
||||||
dateStr = date.twitter();
|
return date.twitter();
|
||||||
} else {
|
} else {
|
||||||
dateStr = dtf.format(date.toDate());
|
return dtf.format(date.toDate());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
return date.fromNow();
|
||||||
dateStr = date.fromNow();
|
}, [date, format]);
|
||||||
}
|
const dt = useMemo(() => date.toISOString(), [date]);
|
||||||
|
const title = useMemo(() => date.format('LLLL'), [date]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<time datetime={date.toISOString()} title={date.format('LLLL')}>
|
<time datetime={dt} title={title}>
|
||||||
{dateStr}
|
{dateStr}
|
||||||
</time>
|
</time>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue