Skip to content

Commit a148935

Browse files
committed
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
1 parent 738f2ca commit a148935

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/components/Thumbnail/Thumbnail.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import classnames from 'classnames';
22
import React, { MouseEvent, ReactNode, ReactText, useRef } from 'react';
33
import { FileLike } from '../../data';
4-
import { useAccessibleFocus, useFile, useFocus } from '../../hooks';
4+
import { useAccessibleFocus, useFileSubscribe, useFocus } from '../../hooks';
55
import { ClickableDiv, ClickableDivProps } from '../ClickableDiv';
66
import { EditableText } from '../EditableText';
77
import { FilePlaceholder } from '../FilePlaceholder';
@@ -75,7 +75,7 @@ export interface ThumbnailProps<F> extends ClickableDivProps {
7575
}
7676

7777
export function Thumbnail<F extends FileLike>({
78-
file: _file,
78+
file,
7979
label,
8080
selected,
8181
dragging,
@@ -99,19 +99,21 @@ export function Thumbnail<F extends FileLike>({
9999

100100
const { focused, handleOnFocus, handleOnBlur } = useFocus(onFocus, onBlur);
101101

102-
const file = useFile(_file, isShownOnLoad ? 0 : throttle);
102+
const toThrottle = isShownOnLoad ? 0 : throttle;
103+
const [thumbnail, thumbnailErr] = useFileSubscribe(file, f => f.thumbnail, 'onthumbnailchange', toThrottle);
104+
const [name] = useFileSubscribe(file, f => f.name, 'onnamechange');
103105

104106
const handleOnSave = (newName: string) => {
105-
onRename?.(newName, file.file);
106-
onEditingChange?.(false, file.file);
107+
onRename?.(newName, file);
108+
onEditingChange?.(false, file);
107109
};
108110

109111
const handleOnCancel = () => {
110-
onEditingChange?.(false, file.file);
112+
onEditingChange?.(false, file);
111113
};
112114

113115
const handleOnEdit = () => {
114-
onEditingChange?.(true, file.file);
116+
onEditingChange?.(true, file);
115117
};
116118

117119
const thumbnailClass = classnames(
@@ -139,9 +141,9 @@ export function Thumbnail<F extends FileLike>({
139141
>
140142
<div className="ui__thumbnail__image">
141143
<Image
142-
src={file.thumbnail}
143-
alt={file.name}
144-
pending={!file.thumbnail && !file.errors.thumbnail}
144+
src={thumbnail}
145+
alt={name}
146+
pending={!thumbnail && !thumbnailErr}
145147
onRenderLoading={() => <FileSkeleton className="ui__thumbnail__image__skeleton" />}
146148
onRenderFallback={() => (
147149
<FilePlaceholder className="ui__thumbnail__image__placeholder" extension={file.extension} />
@@ -151,15 +153,15 @@ export function Thumbnail<F extends FileLike>({
151153
</div>
152154
<div className="ui__thumbnail__controls">
153155
{buttonProps?.map(({ key, ...buttonPropObject }) => (
154-
<ToolButton key={key} disabled={disabled} onClick={e => buttonPropObject.onClick(e, file.file)}>
156+
<ToolButton key={key} disabled={disabled} onClick={e => buttonPropObject.onClick(e, file)}>
155157
{buttonPropObject.children}
156158
</ToolButton>
157159
))}
158160
</div>
159161
{selectedIcon ? <div className="ui__thumbnail__selectedIcon">{selectedIcon}</div> : undefined}
160162
<EditableText
161163
className="ui__thumbnail__label"
162-
value={label ?? file.name}
164+
value={label ?? name}
163165
centerText
164166
disabled={disabled}
165167
locked={!onRename || otherDragging}

0 commit comments

Comments
 (0)