mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
optimized node layout rerenders
This commit is contained in:
parent
16af90fd50
commit
948817e8fc
7 changed files with 2954 additions and 2933 deletions
5624
package-lock.json
generated
5624
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -104,6 +104,7 @@
|
|||
"sass-loader": "^7.3.1",
|
||||
"sass-resources-loader": "^2.0.0",
|
||||
"scrypt": "^6.0.3",
|
||||
"sticky-sidebar": "^3.3.1",
|
||||
"throttle-debounce": "^2.1.0",
|
||||
"tslint": "^5.20.0",
|
||||
"tslint-config-airbnb": "^5.11.2",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import classnames from 'classnames';
|
||||
import React, { ButtonHTMLAttributes, DetailedHTMLProps, FC, createElement } from 'react';
|
||||
import React, { ButtonHTMLAttributes, DetailedHTMLProps, FC, createElement, memo } from 'react';
|
||||
import * as styles from './styles.scss';
|
||||
import { Icon } from '~/components/input/Icon';
|
||||
import { IIcon } from '~/redux/types';
|
||||
|
@ -22,46 +22,48 @@ type IButtonProps = DetailedHTMLProps<
|
|||
iconOnly?: boolean;
|
||||
};
|
||||
|
||||
const Button: FC<IButtonProps> = ({
|
||||
className = '',
|
||||
size = 'normal',
|
||||
iconLeft,
|
||||
iconRight,
|
||||
children,
|
||||
seamless = false,
|
||||
transparent = false,
|
||||
non_submitting = false,
|
||||
red = false,
|
||||
grey = false,
|
||||
is_loading,
|
||||
title,
|
||||
stretchy,
|
||||
disabled,
|
||||
iconOnly,
|
||||
...props
|
||||
}) =>
|
||||
createElement(
|
||||
seamless || non_submitting ? 'div' : 'button',
|
||||
{
|
||||
className: classnames(styles.button, className, styles[size], {
|
||||
red,
|
||||
grey,
|
||||
seamless,
|
||||
transparent,
|
||||
disabled,
|
||||
is_loading,
|
||||
stretchy,
|
||||
icon: ((iconLeft || iconRight) && !title && !children) || iconOnly,
|
||||
has_icon_left: !!iconLeft,
|
||||
has_icon_right: !!iconRight,
|
||||
}),
|
||||
...props,
|
||||
},
|
||||
[
|
||||
iconLeft && <Icon icon={iconLeft} size={20} key={0} />,
|
||||
title ? <span>{title}</span> : children || null,
|
||||
iconRight && <Icon icon={iconRight} size={20} key={2} />,
|
||||
]
|
||||
);
|
||||
const Button: FC<IButtonProps> = memo(
|
||||
({
|
||||
className = '',
|
||||
size = 'normal',
|
||||
iconLeft,
|
||||
iconRight,
|
||||
children,
|
||||
seamless = false,
|
||||
transparent = false,
|
||||
non_submitting = false,
|
||||
red = false,
|
||||
grey = false,
|
||||
is_loading,
|
||||
title,
|
||||
stretchy,
|
||||
disabled,
|
||||
iconOnly,
|
||||
...props
|
||||
}) =>
|
||||
createElement(
|
||||
seamless || non_submitting ? 'div' : 'button',
|
||||
{
|
||||
className: classnames(styles.button, className, styles[size], {
|
||||
red,
|
||||
grey,
|
||||
seamless,
|
||||
transparent,
|
||||
disabled,
|
||||
is_loading,
|
||||
stretchy,
|
||||
icon: ((iconLeft || iconRight) && !title && !children) || iconOnly,
|
||||
has_icon_left: !!iconLeft,
|
||||
has_icon_right: !!iconRight,
|
||||
}),
|
||||
...props,
|
||||
},
|
||||
[
|
||||
iconLeft && <Icon icon={iconLeft} size={20} key={0} />,
|
||||
title ? <span>{title}</span> : children || null,
|
||||
iconRight && <Icon icon={iconRight} size={20} key={2} />,
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
export { Button };
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import React, { FC, useMemo } from 'react';
|
||||
import React, { FC, useMemo, memo } from 'react';
|
||||
import { Comment } from '../Comment';
|
||||
import { Filler } from '~/components/containers/Filler';
|
||||
|
||||
|
@ -10,7 +10,7 @@ interface IProps {
|
|||
comments?: IComment[];
|
||||
}
|
||||
|
||||
const NodeComments: FC<IProps> = ({ comments }) => {
|
||||
const NodeComments: FC<IProps> = memo(({ comments }) => {
|
||||
const groupped: ICommentGroup[] = useMemo(() => comments.reduce(groupCommentsByUser, []), [
|
||||
comments,
|
||||
]);
|
||||
|
@ -24,6 +24,6 @@ const NodeComments: FC<IProps> = ({ comments }) => {
|
|||
<Filler />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
});
|
||||
|
||||
export { NodeComments };
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import React, { FC, useCallback, useEffect, useRef, useState } from 'react';
|
||||
import React, { FC, useCallback, useEffect, useRef, useState, memo } from 'react';
|
||||
import * as styles from './styles.scss';
|
||||
import { INode } from '~/redux/types';
|
||||
import { createPortal } from 'react-dom';
|
||||
|
@ -14,7 +14,7 @@ interface IProps {
|
|||
onLike: () => void;
|
||||
}
|
||||
|
||||
const NodePanel: FC<IProps> = ({ node, layout, can_edit, can_like, onEdit, onLike }) => {
|
||||
const NodePanel: FC<IProps> = memo(({ node, layout, can_edit, can_like, onEdit, onLike }) => {
|
||||
const [stack, setStack] = useState(false);
|
||||
|
||||
const ref = useRef(null);
|
||||
|
@ -64,6 +64,6 @@ const NodePanel: FC<IProps> = ({ node, layout, can_edit, can_like, onEdit, onLik
|
|||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
});
|
||||
|
||||
export { NodePanel };
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import React, { FC } from 'react';
|
||||
import React, { FC, memo } from 'react';
|
||||
import { Tags } from '../Tags';
|
||||
import { ITag } from '~/redux/types';
|
||||
|
||||
|
@ -8,8 +8,8 @@ interface IProps {
|
|||
onChange?: (tags: string[]) => void;
|
||||
}
|
||||
|
||||
const NodeTags: FC<IProps> = ({ is_editable, tags, onChange }) => (
|
||||
const NodeTags: FC<IProps> = memo(({ is_editable, tags, onChange }) => (
|
||||
<Tags tags={tags} is_editable={is_editable} onTagsChange={onChange} />
|
||||
);
|
||||
));
|
||||
|
||||
export { NodeTags };
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import React, { FC, createElement, useEffect, useCallback, useState, useMemo } from 'react';
|
||||
import React, { FC, createElement, useEffect, useCallback, useState, useMemo, memo } from 'react';
|
||||
import { RouteComponentProps } from 'react-router';
|
||||
import { connect } from 'react-redux';
|
||||
import { canEditNode, canLikeNode } from '~/utils/node';
|
||||
|
@ -35,97 +35,99 @@ type IProps = ReturnType<typeof mapStateToProps> &
|
|||
typeof mapDispatchToProps &
|
||||
RouteComponentProps<{ id: string }> & {};
|
||||
|
||||
const NodeLayoutUnconnected: FC<IProps> = ({
|
||||
match: {
|
||||
params: { id },
|
||||
},
|
||||
node: { is_loading, is_loading_comments, comments = [], current: node, current_cover_image },
|
||||
user,
|
||||
user: { is_user },
|
||||
nodeLoadNode,
|
||||
nodeUpdateTags,
|
||||
nodeEdit,
|
||||
nodeLike,
|
||||
nodeSetCoverImage,
|
||||
}) => {
|
||||
const [layout, setLayout] = useState({});
|
||||
|
||||
const updateLayout = useCallback(() => setLayout({}), []);
|
||||
|
||||
useEffect(() => {
|
||||
if (is_loading) return;
|
||||
nodeLoadNode(parseInt(id, 10), null);
|
||||
}, [nodeLoadNode, id]);
|
||||
|
||||
const onTagsChange = useCallback(
|
||||
(tags: string[]) => {
|
||||
nodeUpdateTags(node.id, tags);
|
||||
const NodeLayoutUnconnected: FC<IProps> = memo(
|
||||
({
|
||||
match: {
|
||||
params: { id },
|
||||
},
|
||||
[node, nodeUpdateTags]
|
||||
);
|
||||
node: { is_loading, is_loading_comments, comments = [], current: node, current_cover_image },
|
||||
user,
|
||||
user: { is_user },
|
||||
nodeLoadNode,
|
||||
nodeUpdateTags,
|
||||
nodeEdit,
|
||||
nodeLike,
|
||||
nodeSetCoverImage,
|
||||
}) => {
|
||||
const [layout, setLayout] = useState({});
|
||||
|
||||
const can_edit = useMemo(() => canEditNode(node, user), [node, user]);
|
||||
const can_like = useMemo(() => canLikeNode(node, user), [node, user]);
|
||||
const updateLayout = useCallback(() => setLayout({}), []);
|
||||
|
||||
const block = node && node.type && NODE_COMPONENTS[node.type];
|
||||
const inline_block = node && node.type && NODE_INLINES[node.type];
|
||||
useEffect(() => {
|
||||
if (is_loading) return;
|
||||
nodeLoadNode(parseInt(id, 10), null);
|
||||
}, [nodeLoadNode, id]);
|
||||
|
||||
const onEdit = useCallback(() => nodeEdit(node.id), [nodeEdit, node]);
|
||||
const onLike = useCallback(() => nodeLike(node.id), [nodeLike, node]);
|
||||
const onTagsChange = useCallback(
|
||||
(tags: string[]) => {
|
||||
nodeUpdateTags(node.id, tags);
|
||||
},
|
||||
[node, nodeUpdateTags]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!node.cover) return;
|
||||
nodeSetCoverImage(node.cover);
|
||||
return () => nodeSetCoverImage(null);
|
||||
}, [nodeSetCoverImage, node.cover]);
|
||||
const can_edit = useMemo(() => canEditNode(node, user), [node, user]);
|
||||
const can_like = useMemo(() => canLikeNode(node, user), [node, user]);
|
||||
|
||||
return (
|
||||
<Card className={styles.node} seamless>
|
||||
{block && createElement(block, { node, is_loading, updateLayout, layout })}
|
||||
const block = node && node.type && NODE_COMPONENTS[node.type];
|
||||
const inline_block = node && node.type && NODE_INLINES[node.type];
|
||||
|
||||
<NodePanel
|
||||
node={node}
|
||||
layout={layout}
|
||||
can_edit={can_edit}
|
||||
can_like={can_like}
|
||||
onEdit={onEdit}
|
||||
onLike={onLike}
|
||||
/>
|
||||
const onEdit = useCallback(() => nodeEdit(node.id), [nodeEdit, node]);
|
||||
const onLike = useCallback(() => nodeLike(node.id), [nodeLike, node]);
|
||||
|
||||
<Group>
|
||||
<Padder>
|
||||
<Group horizontal className={styles.content}>
|
||||
<Group className={styles.comments}>
|
||||
{inline_block && (
|
||||
<div className={styles.inline_block}>
|
||||
{createElement(inline_block, { node, is_loading, updateLayout, layout })}
|
||||
</div>
|
||||
)}
|
||||
useEffect(() => {
|
||||
if (!node.cover) return;
|
||||
nodeSetCoverImage(node.cover);
|
||||
return () => nodeSetCoverImage(null);
|
||||
}, [nodeSetCoverImage, node.cover]);
|
||||
|
||||
{is_loading_comments || !comments.length ? (
|
||||
<NodeNoComments is_loading={is_loading_comments} />
|
||||
) : (
|
||||
<NodeComments comments={comments} />
|
||||
)}
|
||||
return (
|
||||
<Card className={styles.node} seamless>
|
||||
{block && createElement(block, { node, is_loading, updateLayout, layout })}
|
||||
|
||||
{is_user && <CommentForm id={0} />}
|
||||
</Group>
|
||||
<NodePanel
|
||||
node={node}
|
||||
layout={layout}
|
||||
can_edit={can_edit}
|
||||
can_like={can_like}
|
||||
onEdit={onEdit}
|
||||
onLike={onLike}
|
||||
/>
|
||||
|
||||
<div className={styles.panel}>
|
||||
<Group style={{ flex: 1, minWidth: 0 }}>
|
||||
<NodeTags is_editable={is_user} tags={node.tags} onChange={onTagsChange} />
|
||||
<Group>
|
||||
<Padder>
|
||||
<Group horizontal className={styles.content}>
|
||||
<Group className={styles.comments}>
|
||||
{inline_block && (
|
||||
<div className={styles.inline_block}>
|
||||
{createElement(inline_block, { node, is_loading, updateLayout, layout })}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<NodeRelated title="First album" />
|
||||
{is_loading_comments || !comments.length ? (
|
||||
<NodeNoComments is_loading={is_loading_comments} />
|
||||
) : (
|
||||
<NodeComments comments={comments} />
|
||||
)}
|
||||
|
||||
<NodeRelated title="Second album" />
|
||||
{is_user && <CommentForm id={0} />}
|
||||
</Group>
|
||||
</div>
|
||||
</Group>
|
||||
</Padder>
|
||||
</Group>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
<div className={styles.panel}>
|
||||
<Group style={{ flex: 1, minWidth: 0 }}>
|
||||
<NodeTags is_editable={is_user} tags={node.tags} onChange={onTagsChange} />
|
||||
|
||||
<NodeRelated title="First album" />
|
||||
|
||||
<NodeRelated title="Second album" />
|
||||
</Group>
|
||||
</div>
|
||||
</Group>
|
||||
</Padder>
|
||||
</Group>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
const NodeLayout = connect(
|
||||
mapStateToProps,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue