Skip to content

Commit

Permalink
Fix scene missing flicker on scene page (stashapp#3857)
Browse files Browse the repository at this point in the history
* use useLayoutEffect
* Remove unnecessary nullability in ScenePlayer
  • Loading branch information
DingDongSoLong4 committed Jul 11, 2023
1 parent 0268565 commit cec9195
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
34 changes: 17 additions & 17 deletions ui/v2.5/src/components/ScenePlayer/ScenePlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function getMarkerTitle(marker: MarkerFragment) {
}

interface IScenePlayerProps {
scene: GQL.SceneDataFragment | undefined | null;
scene: GQL.SceneDataFragment;
hideScrubberOverride: boolean;
autoplay?: boolean;
permitLoop?: boolean;
Expand Down Expand Up @@ -217,7 +217,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
const vrTag = uiConfig?.vrTag ?? undefined;

const file = useMemo(
() => ((scene?.files.length ?? 0) > 0 ? scene?.files[0] : undefined),
() => (scene.files.length > 0 ? scene.files[0] : undefined),
[scene]
);

Expand Down Expand Up @@ -363,7 +363,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
}, [getPlayer, onNext, onPrevious]);

useEffect(() => {
if (scene?.interactive && interactiveInitialised) {
if (scene.interactive && interactiveInitialised) {
interactiveReady.current = false;
uploadScript(scene.paths.funscript || "").then(() => {
interactiveReady.current = true;
Expand All @@ -372,8 +372,8 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
}, [
uploadScript,
interactiveInitialised,
scene?.interactive,
scene?.paths.funscript,
scene.interactive,
scene.paths.funscript,
]);

useEffect(() => {
Expand All @@ -384,7 +384,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({

let showButton = false;

if (scene && vrTag) {
if (vrTag) {
showButton = scene.tags.some((tag) => vrTag === tag.name);
}

Expand Down Expand Up @@ -438,7 +438,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({

function onplay(this: VideoJsPlayer) {
this.persistVolume().enabled = true;
if (scene?.interactive && interactiveReady.current) {
if (scene.interactive && interactiveReady.current) {
interactiveClient.play(this.currentTime());
}
}
Expand All @@ -449,14 +449,14 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({

function seeking(this: VideoJsPlayer) {
if (this.paused()) return;
if (scene?.interactive && interactiveReady.current) {
if (scene.interactive && interactiveReady.current) {
interactiveClient.play(this.currentTime());
}
}

function timeupdate(this: VideoJsPlayer) {
if (this.paused()) return;
if (scene?.interactive && interactiveReady.current) {
if (scene.interactive && interactiveReady.current) {
interactiveClient.ensurePlaying(this.currentTime());
}
setTime(this.currentTime());
Expand All @@ -480,7 +480,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
if (!player) return;

// don't re-initialise the player unless the scene has changed
if (!scene || !file || scene.id === sceneId.current) return;
if (!file || scene.id === sceneId.current) return;

sceneId.current = scene.id;

Expand Down Expand Up @@ -629,7 +629,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({

useEffect(() => {
const player = getPlayer();
if (!player || !scene) return;
if (!player) return;

const markers = player.markers();
markers.clearMarkers();
Expand All @@ -652,7 +652,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
if (!player) return;

async function saveActivity(resumeTime: number, playDuration: number) {
if (!scene?.id) return;
if (!scene.id) return;

await sceneSaveActivity({
variables: {
Expand All @@ -664,7 +664,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
}

async function incrementPlayCount() {
if (!scene?.id) return;
if (!scene.id) return;

await sceneIncrementPlayCount({
variables: {
Expand Down Expand Up @@ -698,7 +698,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({

useEffect(() => {
const player = getPlayer();
if (!player || !scene || !ready || !auto.current) {
if (!player || !ready || !auto.current) {
return;
}

Expand Down Expand Up @@ -766,18 +766,18 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
}

const isPortrait =
scene && file && file.height && file.width && file.height > file.width;
file && file.height && file.width && file.height > file.width;

return (
<div
className={cx("VideoPlayer", { portrait: isPortrait })}
onKeyDownCapture={onKeyDown}
>
<div className="video-wrapper" ref={videoRef} />
{scene?.interactive &&
{scene.interactive &&
(interactiveState !== ConnectionState.Ready ||
getPlayer()?.paused()) && <SceneInteractiveStatus />}
{scene && file && showScrubber && (
{file && showScrubber && (
<ScenePlayerScrubber
file={file}
scene={scene}
Expand Down
64 changes: 34 additions & 30 deletions ui/v2.5/src/components/Scenes/SceneDetails/Scene.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Tab, Nav, Dropdown, Button, ButtonGroup } from "react-bootstrap";
import React, { useEffect, useState, useMemo, useContext, useRef } from "react";
import React, {
useEffect,
useState,
useMemo,
useContext,
useRef,
useLayoutEffect,
} from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { useParams, useLocation, useHistory, Link } from "react-router-dom";
import { Helmet } from "react-helmet";
Expand Down Expand Up @@ -541,12 +548,11 @@ const SceneLoader: React.FC = () => {
const { configuration } = useContext(ConfigurationContext);
const { data, loading, error } = useFindScene(id ?? "");

const [scene, setScene] = useState<GQL.SceneDataFragment | undefined>(
data?.findScene ?? undefined
);
const [scene, setScene] = useState<GQL.SceneDataFragment>();

// only update scene when loading is done
useEffect(() => {
// useLayoutEffect to update before paint
useLayoutEffect(() => {
// only update scene when loading is done
if (!loading) {
setScene(data?.findScene ?? undefined);
}
Expand Down Expand Up @@ -763,34 +769,32 @@ const SceneLoader: React.FC = () => {
}
}

if (!scene && loading) return <LoadingIndicator />;
if (error) return <ErrorMessage error={error.message} />;

if (!loading && !scene)
if (!scene) {
if (loading) return <LoadingIndicator />;
if (error) return <ErrorMessage error={error.message} />;
return <ErrorMessage error={`No scene found with id ${id}.`} />;
}

return (
<div className="row">
{scene && (
<ScenePage
scene={scene}
setTimestamp={setTimestamp}
queueScenes={queueScenes ?? []}
queueStart={queueStart}
onDelete={onDelete}
onQueueNext={onQueueNext}
onQueuePrevious={onQueuePrevious}
onQueueRandom={onQueueRandom}
continuePlaylist={continuePlaylist}
loadScene={loadScene}
queueHasMoreScenes={queueHasMoreScenes}
onQueueLessScenes={onQueueLessScenes}
onQueueMoreScenes={onQueueMoreScenes}
collapsed={collapsed}
setCollapsed={setCollapsed}
setContinuePlaylist={setContinuePlaylist}
/>
)}
<ScenePage
scene={scene}
setTimestamp={setTimestamp}
queueScenes={queueScenes ?? []}
queueStart={queueStart}
onDelete={onDelete}
onQueueNext={onQueueNext}
onQueuePrevious={onQueuePrevious}
onQueueRandom={onQueueRandom}
continuePlaylist={continuePlaylist}
loadScene={loadScene}
queueHasMoreScenes={queueHasMoreScenes}
onQueueLessScenes={onQueueLessScenes}
onQueueMoreScenes={onQueueMoreScenes}
collapsed={collapsed}
setCollapsed={setCollapsed}
setContinuePlaylist={setContinuePlaylist}
/>
<div className={`scene-player-container ${collapsed ? "expanded" : ""}`}>
<ScenePlayer
key="ScenePlayer"
Expand Down

0 comments on commit cec9195

Please sign in to comment.