From a148935c593ced4652e5b20284a5ea47b8cfff4d Mon Sep 17 00:00:00 2001 From: Liam Ross Date: Wed, 20 May 2020 12:11:41 -0700 Subject: [PATCH] refactor(Thumbnail): fewer file value subscriptions by only subscribing to thumbnail and name, reduced performance overhead per Thumbnail by not requiring explicit document object or file object generation --- src/components/Thumbnail/Thumbnail.tsx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/components/Thumbnail/Thumbnail.tsx b/src/components/Thumbnail/Thumbnail.tsx index 8339d85..df48f2e 100644 --- a/src/components/Thumbnail/Thumbnail.tsx +++ b/src/components/Thumbnail/Thumbnail.tsx @@ -1,7 +1,7 @@ import classnames from 'classnames'; import React, { MouseEvent, ReactNode, ReactText, useRef } from 'react'; import { FileLike } from '../../data'; -import { useAccessibleFocus, useFile, useFocus } from '../../hooks'; +import { useAccessibleFocus, useFileSubscribe, useFocus } from '../../hooks'; import { ClickableDiv, ClickableDivProps } from '../ClickableDiv'; import { EditableText } from '../EditableText'; import { FilePlaceholder } from '../FilePlaceholder'; @@ -75,7 +75,7 @@ export interface ThumbnailProps extends ClickableDivProps { } export function Thumbnail({ - file: _file, + file, label, selected, dragging, @@ -99,19 +99,21 @@ export function Thumbnail({ const { focused, handleOnFocus, handleOnBlur } = useFocus(onFocus, onBlur); - const file = useFile(_file, isShownOnLoad ? 0 : throttle); + const toThrottle = isShownOnLoad ? 0 : throttle; + const [thumbnail, thumbnailErr] = useFileSubscribe(file, f => f.thumbnail, 'onthumbnailchange', toThrottle); + const [name] = useFileSubscribe(file, f => f.name, 'onnamechange'); const handleOnSave = (newName: string) => { - onRename?.(newName, file.file); - onEditingChange?.(false, file.file); + onRename?.(newName, file); + onEditingChange?.(false, file); }; const handleOnCancel = () => { - onEditingChange?.(false, file.file); + onEditingChange?.(false, file); }; const handleOnEdit = () => { - onEditingChange?.(true, file.file); + onEditingChange?.(true, file); }; const thumbnailClass = classnames( @@ -139,9 +141,9 @@ export function Thumbnail({ >
{file.name} } onRenderFallback={() => ( @@ -151,7 +153,7 @@ export function Thumbnail({
{buttonProps?.map(({ key, ...buttonPropObject }) => ( - buttonPropObject.onClick(e, file.file)}> + buttonPropObject.onClick(e, file)}> {buttonPropObject.children} ))} @@ -159,7 +161,7 @@ export function Thumbnail({ {selectedIcon ?
{selectedIcon}
: undefined}