mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
optimizedbig amount of data rendering
This commit is contained in:
parent
383990d556
commit
d9f39a8d67
3 changed files with 92 additions and 50 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React, { FC, useState, useCallback, useEffect } from 'react';
|
import React, { FC, useState, useCallback, useEffect, useRef } from 'react';
|
||||||
import { INode } from '~/redux/types';
|
import { INode } from '~/redux/types';
|
||||||
import { getURL, formatCellText } from '~/utils/dom';
|
import { getURL, formatCellText } from '~/utils/dom';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
@ -7,6 +7,7 @@ import * as styles from './styles.scss';
|
||||||
import { Icon } from '~/components/input/Icon';
|
import { Icon } from '~/components/input/Icon';
|
||||||
import { flowSetCellView } from '~/redux/flow/actions';
|
import { flowSetCellView } from '~/redux/flow/actions';
|
||||||
import { PRESETS } from '~/constants/urls';
|
import { PRESETS } from '~/constants/urls';
|
||||||
|
import { debounce } from 'throttle-debounce';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
node: INode;
|
node: INode;
|
||||||
|
@ -23,7 +24,33 @@ const Cell: FC<IProps> = ({
|
||||||
onSelect,
|
onSelect,
|
||||||
onChangeCellView,
|
onChangeCellView,
|
||||||
}) => {
|
}) => {
|
||||||
|
const ref = useRef(null);
|
||||||
const [is_loaded, setIsLoaded] = useState(false);
|
const [is_loaded, setIsLoaded] = useState(false);
|
||||||
|
const [is_visible, setIsVisible] = useState(false);
|
||||||
|
|
||||||
|
const checkIfVisible = useCallback(() => {
|
||||||
|
if (!ref.current) return;
|
||||||
|
|
||||||
|
const { top, height } = ref.current.getBoundingClientRect();
|
||||||
|
|
||||||
|
const visibility = top + height > -window.innerHeight && top < window.innerHeight * 2;
|
||||||
|
|
||||||
|
if (visibility !== is_visible) setIsVisible(visibility);
|
||||||
|
}, [ref, is_visible, setIsVisible]);
|
||||||
|
|
||||||
|
const checkIfVisibleDebounced = useCallback(debounce(200 + Math.random() * 200, checkIfVisible), [
|
||||||
|
checkIfVisible,
|
||||||
|
]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
checkIfVisible();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('scroll', checkIfVisibleDebounced);
|
||||||
|
|
||||||
|
return () => window.removeEventListener('scroll', checkIfVisibleDebounced);
|
||||||
|
}, [checkIfVisibleDebounced]);
|
||||||
|
|
||||||
const onImageLoad = useCallback(() => {
|
const onImageLoad = useCallback(() => {
|
||||||
setIsLoaded(true);
|
setIsLoaded(true);
|
||||||
|
@ -67,51 +94,56 @@ const Cell: FC<IProps> = ({
|
||||||
className={classNames(styles.cell, styles[(flow && flow.display) || 'single'], {
|
className={classNames(styles.cell, styles[(flow && flow.display) || 'single'], {
|
||||||
[styles.is_text]: false,
|
[styles.is_text]: false,
|
||||||
})}
|
})}
|
||||||
|
ref={ref}
|
||||||
>
|
>
|
||||||
{can_edit && (
|
{is_visible && (
|
||||||
<div className={styles.menu}>
|
<>
|
||||||
<div className={styles.menu_button}>
|
{can_edit && (
|
||||||
<Icon icon="dots-vertical" />
|
<div className={styles.menu}>
|
||||||
</div>
|
<div className={styles.menu_button}>
|
||||||
|
<Icon icon="dots-vertical" />
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className={styles.menu_content}>
|
<div className={styles.menu_content}>
|
||||||
{has_description && (
|
{has_description && (
|
||||||
<>
|
<>
|
||||||
<Icon icon="text" onClick={toggleViewDescription} />
|
<Icon icon="text" onClick={toggleViewDescription} />
|
||||||
<div className={styles.menu_sep} />
|
<div className={styles.menu_sep} />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<Icon icon="cell-single" onClick={setViewSingle} />
|
<Icon icon="cell-single" onClick={setViewSingle} />
|
||||||
<Icon icon="cell-double-h" onClick={setViewHorizontal} />
|
<Icon icon="cell-double-h" onClick={setViewHorizontal} />
|
||||||
<Icon icon="cell-double-v" onClick={setViewVertical} />
|
<Icon icon="cell-double-v" onClick={setViewVertical} />
|
||||||
<Icon icon="cell-quadro" onClick={setViewQuadro} />
|
<Icon icon="cell-quadro" onClick={setViewQuadro} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
|
||||||
|
|
||||||
<div className={classNames(styles.face, { [styles.has_text]: text })}>
|
|
||||||
<div className={styles.face_content}>
|
|
||||||
{title && <div className={styles.title}>{title}</div>}
|
|
||||||
{text && (
|
|
||||||
<div
|
|
||||||
className={styles.text}
|
|
||||||
dangerouslySetInnerHTML={{ __html: formatCellText(text) }}
|
|
||||||
/>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{thumbnail && (
|
<div className={classNames(styles.face, { [styles.has_text]: text })}>
|
||||||
<div
|
<div className={styles.face_content}>
|
||||||
className={styles.thumbnail}
|
{title && <div className={styles.title}>{title}</div>}
|
||||||
style={{
|
{text && (
|
||||||
backgroundImage: `url("${getURL({ url: thumbnail }, PRESETS.cover)}")`,
|
<div
|
||||||
opacity: is_loaded ? 1 : 0,
|
className={styles.text}
|
||||||
}}
|
dangerouslySetInnerHTML={{ __html: formatCellText(text) }}
|
||||||
onClick={onClick}
|
/>
|
||||||
>
|
)}
|
||||||
<img src={getURL({ url: thumbnail })} onLoad={onImageLoad} alt="" />
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{thumbnail && (
|
||||||
|
<div
|
||||||
|
className={styles.thumbnail}
|
||||||
|
style={{
|
||||||
|
backgroundImage: `url("${getURL({ url: thumbnail }, PRESETS.cover)}")`,
|
||||||
|
opacity: is_loaded ? 1 : 0,
|
||||||
|
}}
|
||||||
|
onClick={onClick}
|
||||||
|
>
|
||||||
|
<img src={getURL({ url: thumbnail })} onLoad={onImageLoad} alt="" />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -124,6 +124,15 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@keyframes appear {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.face {
|
.face {
|
||||||
@include outer_shadow();
|
@include outer_shadow();
|
||||||
|
|
||||||
|
@ -141,6 +150,7 @@
|
||||||
padding: $gap;
|
padding: $gap;
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
touch-action: none;
|
touch-action: none;
|
||||||
|
animation: appear 1s forwards;
|
||||||
|
|
||||||
@media (min-width: $cell * 2 + $grid_line) {
|
@media (min-width: $cell * 2 + $grid_line) {
|
||||||
.vertical > &.has_text,
|
.vertical > &.has_text,
|
||||||
|
|
|
@ -17,7 +17,7 @@ import { IFlowState } from './reducer';
|
||||||
|
|
||||||
function* onGetFlow() {
|
function* onGetFlow() {
|
||||||
const {
|
const {
|
||||||
data: { nodes = null, heroes = null, recent = [], updated = [] },
|
data: { nodes = [], heroes = [], recent = [], updated = [] },
|
||||||
}: IResultWithStatus<{
|
}: IResultWithStatus<{
|
||||||
nodes: IFlowState['nodes'];
|
nodes: IFlowState['nodes'];
|
||||||
heroes: IFlowState['heroes'];
|
heroes: IFlowState['heroes'];
|
||||||
|
@ -25,13 +25,13 @@ function* onGetFlow() {
|
||||||
updated: IFlowState['updated'];
|
updated: IFlowState['updated'];
|
||||||
}> = yield call(reqWrapper, getNodes, {});
|
}> = yield call(reqWrapper, getNodes, {});
|
||||||
|
|
||||||
if (!nodes || !nodes.length) {
|
// if (!nodes || !nodes.length) {
|
||||||
yield put(flowSetNodes([]));
|
// yield put(flowSetNodes([]));
|
||||||
yield put(flowSetHeroes([]));
|
// yield put(flowSetHeroes([]));
|
||||||
yield put(flowSetRecent([]));
|
// yield put(flowSetRecent([]));
|
||||||
yield put(flowSetUpdated([]));
|
// yield put(flowSetUpdated([]));
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
yield put(flowSetNodes(nodes));
|
yield put(flowSetNodes(nodes));
|
||||||
yield put(flowSetHeroes(heroes));
|
yield put(flowSetHeroes(heroes));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue