Skip to content

Commit

Permalink
Reduce amount of renders
Browse files Browse the repository at this point in the history
  • Loading branch information
undyingwraith committed Jan 31, 2025
1 parent d96c62d commit 52f8be0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
15 changes: 8 additions & 7 deletions packages/ui/src/components/atoms/TimeDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Signal, useComputed } from '@preact/signals-react';
import React from 'react';

export function TimeDisplay(props: { time: number; }) {
export function TimeDisplay(props: { time: Signal<number>; }) {
const { time } = props;

function pad(num: number) {
const test = '00' + num;
return (test).substring(test.length - 2);
const str = '00' + num;
return (str).substring(str.length - 2);
}

const hours = Math.floor(time / 3600);
const minutes = Math.floor(time % 3600 / 60);
const seconds = Math.floor(time % 3600 % 60);
const hours = useComputed(() => pad(Math.floor(time.value / 3600)));
const minutes = useComputed(() => pad(Math.floor(time.value % 3600 / 60)));
const seconds = useComputed(() => pad(Math.floor(time.value % 3600 % 60)));

return (
<div>
{`${pad(hours)}:${pad(minutes)}:${pad(seconds)}`}
{hours}:{minutes}:{seconds}
</div>
);
}
2 changes: 2 additions & 0 deletions packages/ui/src/components/atoms/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { ErrorBoundary } from './ErrorBoundary';
export { FileInfoDisplay } from './FileInfoDisplay';
export { FileInput } from './FileInput';
export { FormList } from './FormList';
export { Identicon } from './Identicon';
Expand All @@ -8,3 +9,4 @@ export { SelectInput } from './SelectInput';
export { Spacer } from './Spacer';
export { TextInput } from './TextInput';
export { ThemeToggle } from './ThemeToggle';
export { TimeDisplay } from './TimeDisplay';
17 changes: 6 additions & 11 deletions packages/ui/src/components/organisms/VideoPlayer/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import React from 'react';
import shaka from 'shaka-player';
import { useService } from '../../../context';
import { useHotkey } from '../../../hooks';
import { FileInfoDisplay } from '../../atoms/FileInfoDisplay';
import { FileInfoDisplay, TimeDisplay } from '../../atoms';
import styles from './VideoPlayer.module.css';
import { TimeDisplay } from '../../atoms/TimeDisplay';

function createShakaIpfsPlugin(ipfs: IIpfsService): shaka.extern.SchemePlugin {
return async (uri: string, request: shaka.extern.Request, requestType: shaka.net.NetworkingEngine.RequestType, progressUpdated: shaka.extern.ProgressUpdated, headersReceived: shaka.extern.HeadersReceived, config: shaka.extern.SchemePluginConfig) => {
Expand Down Expand Up @@ -168,20 +167,16 @@ export function VideoPlayer(props: { file: IVideoFile; autoPlay?: boolean; }) {
useHotkey({ key: 'F' }, () => toggleFullScreen());
useHotkey({ key: 'Space' }, () => togglePlay());

const progressBar = useComputed(() => (
<div className={styles.progress} ref={(ref) => progressRef.value = ref}>
<div className={styles.progressFilled} ref={(ref) => progressBarRef.value = ref} />
</div>
));

const progress = useComputed(() => (
<div className={styles.progressContainer}>
<TimeDisplay
time={currentPlayTime.value}
time={currentPlayTime}
/>
{progressBar}
<div className={styles.progress} ref={(ref) => progressRef.value = ref}>
<div className={styles.progressFilled} ref={(ref) => progressBarRef.value = ref} />
</div>
<TimeDisplay
time={movieDuration.value}
time={movieDuration}
/>
</div>
));
Expand Down

0 comments on commit 52f8be0

Please sign in to comment.