Skip to content

Commit

Permalink
fix session player initial load state
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestii committed May 6, 2024
1 parent 803a921 commit aec8822
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 35 deletions.
2 changes: 0 additions & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"react-query": "^3.39.1",
"react-reflex": "^4.0.9",
"react-select": "^5.7.0",
"react-slider": "^2.0.1",
"react-sliding-pane": "^7.3.0",
"react-sortable-hoc": "^2.0.0",
"react-syntax-highlighter": "^15.4.5",
Expand Down Expand Up @@ -109,7 +108,6 @@
"@types/react-datepicker": "^4.10.0",
"@types/react-dom": "^18.2.18",
"@types/react-grid-layout": "^1.3.2",
"@types/react-slider": "^1.3.1",
"@types/react-syntax-highlighter": "^13.5.2",
"@types/react-table": "^7.7.14",
"@types/react-virtualized-auto-sizer": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/AppNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ function SearchInput({
placeholder={placeholder}
value={value}
onChange={e => onChange(e.currentTarget.value)}
leftSection={<i className="bi bi-search fs-8 ps-1" text-slate-400 />}
leftSection={<i className="bi bi-search fs-8 ps-1 text-slate-400" />}
onKeyDown={handleKeyDown}
rightSection={
value ? (
Expand Down
54 changes: 37 additions & 17 deletions packages/app/src/DOMPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function DOMPlayer({

let currentRrwebEvent = '';

const { isFetching: isSearchResultsFetching } = useSearchEventStream(
const { isFetching: isSearchResultsFetching, abort } = useSearchEventStream(
{
apiUrlPath: `/sessions/${sessionId}/rrweb`,
q: '',
Expand Down Expand Up @@ -219,21 +219,7 @@ export default function DOMPlayer({
replayer.current?.setConfig({ speed: playerSpeed, skipInactive });
}
}
}, [playerState, playerSpeed, skipInactive]);

// Set player to the correct time based on focus
useEffect(() => {
if (focus?.setBy !== 'player' && replayer.current != null) {
pause(
focus?.ts == null
? 0
: focus?.ts - replayer.current.getMetaData().startTime,
);
if (playerState === 'playing') {
play();
}
}
}, [focus, pause, setPlayerState, playerState, play]);
}, [playerState, playerSpeed, skipInactive, pause, play]);

const handleResize = useCallback(() => {
if (wrapper.current == null || playerContainer.current == null) {
Expand Down Expand Up @@ -262,6 +248,7 @@ export default function DOMPlayer({
handleResize();
}, [resizeKey, handleResize]);

const [isReplayerInitialized, setIsReplayerInitialized] = useState(false);
// Set up player
useEffect(() => {
if (
Expand All @@ -281,6 +268,7 @@ export default function DOMPlayer({
showWarning: debug,
skipInactive: true,
});
setIsReplayerInitialized(true);

if (debug) {
// @ts-ignore
Expand Down Expand Up @@ -334,19 +322,51 @@ export default function DOMPlayer({
debug,
]);

// Set player to the correct time based on focus
useEffect(() => {
if (
!isInitialEventsLoaded ||
!isReplayerInitialized ||
lastEventTsLoaded < (focus?.ts ? focus.ts + 1000 : Infinity)
) {
return;
}
if (focus?.setBy !== 'player' && replayer.current != null) {
pause(
focus?.ts == null
? 0
: focus?.ts - replayer.current.getMetaData().startTime,
);
handleResize();
if (playerState === 'playing') {
play();
}
}
}, [
focus,
pause,
setPlayerState,
playerState,
play,
isInitialEventsLoaded,
isReplayerInitialized,
handleResize,
lastEventTsLoaded,
]);

useEffect(() => {
return () => {
if (replayer.current != null) {
replayer.current?.destroy();
replayer.current = null;
}
abort();
};
}, []);

const isLoading = isInitialEventsLoaded === false && isSearchResultsFetching;
// TODO: Handle when ts is set to a value that's outside of this session
const isBuffering =
playerState === 'playing' &&
isReplayFullyLoaded === false &&
(replayer.current?.getMetaData()?.endTime ?? 0) < (focus?.ts ?? 0);

Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/Playbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ export default function Playbar({
onChange={ts => {
setFocus({ ts, setBy: 'slider' });
}}
setPlayerState={setPlayerState}
playerState={playerState}
/>
</div>
<Checkbox
Expand Down
18 changes: 17 additions & 1 deletion packages/app/src/PlaybarSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ type PlaybarSliderProps = {
min: number;
max: number;
markers?: PlaybarMarker[];
playerState: 'playing' | 'paused';
onChange: (value: number) => void;
setPlayerState: (playerState: 'playing' | 'paused') => void;
};

export const PlaybarSlider = ({
min,
max,
value,
onChange,
markers,
playerState,
onChange,
setPlayerState,
}: PlaybarSliderProps) => {
const valueLabelFormat = React.useCallback(
(ts: number) => {
Expand All @@ -44,6 +48,7 @@ export const PlaybarSlider = ({
() =>
markers?.map(mark => (
<Tooltip
color={mark.isError ? 'red' : 'gray'}
key={mark.id}
label={truncateText(mark?.description ?? '', 240, '...', /\n/)}
position="top"
Expand All @@ -64,6 +69,15 @@ export const PlaybarSlider = ({
[markers, max, min, onChange],
);

const [prevPlayerState, setPrevPlayerState] = React.useState(playerState);
const handleMouseDown = React.useCallback(() => {
setPrevPlayerState(playerState);
setPlayerState('paused');
}, [playerState, setPlayerState]);
const handleMouseUp = React.useCallback(() => {
setPlayerState(prevPlayerState);
}, [prevPlayerState, setPlayerState]);

return (
<div className={styles.wrapper}>
<div className={styles.markers}>{markersContent}</div>
Expand All @@ -76,6 +90,8 @@ export const PlaybarSlider = ({
step={1000}
label={valueLabelFormat}
onChange={onChange}
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
/>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/SessionSubpanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,13 @@ export default function SessionSubpanel({
},
);
const prevTsQuery = usePrevious(tsQuery);

useEffect(() => {
if (prevTsQuery == null && tsQuery != null) {
_setFocus({ ts: tsQuery, setBy: 'url' });
}
}, [prevTsQuery, tsQuery]);

const debouncedSetTsQuery = useRef(
throttle(async (ts: number) => {
setTsQuery(ts, 'replaceIn');
Expand Down
5 changes: 5 additions & 0 deletions packages/app/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,17 @@ function useSearchEventStream(
[fetchResults, results.data.length, hasNextPage],
);

const abort = useCallback(() => {
lastAbortController.current?.abort();
}, []);

return {
hasNextPage,
isFetching,
results: results.data,
resultsKey: results.key,
fetchNextPage,
abort,
};
}

Expand Down
14 changes: 0 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4406,13 +4406,6 @@
dependencies:
"@types/react" "*"

"@types/react-slider@^1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@types/react-slider/-/react-slider-1.3.1.tgz#a3816989eb4fc172e7df316930f242fec90690fc"
integrity sha512-4X2yK7RyCIy643YCFL+bc6XNmcnBtt8n88uuyihvcn5G7Lut23eNQU3q3KmwF7MWIfKfsW5NxCjw0SeDZRtgaA==
dependencies:
"@types/react" "*"

"@types/react-syntax-highlighter@^13.5.2":
version "13.5.2"
resolved "https://registry.yarnpkg.com/@types/react-syntax-highlighter/-/react-syntax-highlighter-13.5.2.tgz#357cc03581dc434c57c3b31f70e0eecdbf7b3ab0"
Expand Down Expand Up @@ -12188,13 +12181,6 @@ react-select@^5.7.0:
react-transition-group "^4.3.0"
use-isomorphic-layout-effect "^1.1.2"

react-slider@^2.0.1:
version "2.0.4"
resolved "https://registry.yarnpkg.com/react-slider/-/react-slider-2.0.4.tgz#21c656ffabc3bb4481cf6b49e6d647baeda83572"
integrity sha512-sWwQD01n6v+MbeLCYthJGZPc0kzOyhQHyd0bSo0edg+IAxTVQmj3Oy4SBK65eX6gNwS9meUn6Z5sIBUVmwAd9g==
dependencies:
prop-types "^15.8.1"

react-sliding-pane@^7.3.0:
version "7.3.0"
resolved "https://registry.yarnpkg.com/react-sliding-pane/-/react-sliding-pane-7.3.0.tgz#a6a03b90db216e7ec6f746c7e649d19ba03ff4e0"
Expand Down

0 comments on commit aec8822

Please sign in to comment.