Skip to content

Commit ea0139b

Browse files
committed
feat: option to enable scroll to new log lines
1 parent 87ba4d8 commit ea0139b

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

electron/renderer/pages/grid.tsx

+33-4
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ interface GameLogLine {
4444
}
4545

4646
interface DougCmpProps {
47+
/**
48+
* The stream of game text to display.
49+
* The stream is additive, so each new line will be appended to the end.
50+
* The special log line text '__CLEAR_STREAM__' will clear all prior lines.
51+
*/
4752
stream$: rxjs.Observable<GameLogLine>;
53+
/**
54+
* Enable to automatically scroll to the bottom of the game stream window
55+
* as new log lines are added. This effect only occurs if the user
56+
* is already scrolled to the bottom to ensure they see latest content.
57+
*/
58+
enableScrollToNewLogLines: boolean;
4859
}
4960

5061
const DougCmp: React.FC<DougCmpProps> = (props: DougCmpProps): ReactNode => {
51-
const { stream$ } = props;
62+
const { stream$, enableScrollToNewLogLines } = props;
5263

5364
useSubscription(stream$, (logLine) => {
5465
if (logLine.text === '__CLEAR_STREAM__') {
@@ -60,7 +71,10 @@ const DougCmp: React.FC<DougCmpProps> = (props: DougCmpProps): ReactNode => {
6071

6172
const scrollableRef = useRef<HTMLDivElement>(null);
6273
const scrollBottomRef = useRef<HTMLSpanElement>(null);
63-
const [autoScrollEnabled, setAutoScrollEnabled] = useState<boolean>(true);
74+
75+
const [autoScrollEnabled, setAutoScrollEnabled] = useState<boolean>(
76+
enableScrollToNewLogLines
77+
);
6478

6579
const [gameLogLines, setGameLogLines] = useState<Array<GameLogLine>>([]);
6680

@@ -77,6 +91,10 @@ const DougCmp: React.FC<DougCmpProps> = (props: DougCmpProps): ReactNode => {
7791
}, []);
7892

7993
useEffect(() => {
94+
if (!enableScrollToNewLogLines) {
95+
return;
96+
}
97+
8098
let scrollableElmt = scrollableRef.current;
8199

82100
const onScroll = () => {
@@ -98,7 +116,7 @@ const DougCmp: React.FC<DougCmpProps> = (props: DougCmpProps): ReactNode => {
98116
return () => {
99117
scrollableElmt?.removeEventListener('scroll', onScroll);
100118
};
101-
}, []);
119+
}, [enableScrollToNewLogLines]);
102120

103121
if (autoScrollEnabled) {
104122
scrollBottomRef.current?.scrollIntoView({
@@ -157,8 +175,9 @@ const GridPage: React.FC = (): ReactNode => {
157175
const { skill, rank, percent, mindState } = gameEvent;
158176
const txtSkill = skill.padStart(15);
159177
const txtRank = String(rank).padStart(3);
160-
const txtPercent = String(percent).padStart(2);
178+
const txtPercent = String(percent).padStart(2) + '%';
161179
const txtMindState = mindState.padEnd(15);
180+
// TODO add option to show mind state as the numbers (x/34)
162181
return `${txtSkill} ${txtRank} ${txtPercent} ${txtMindState}`;
163182
},
164183
[]
@@ -376,6 +395,7 @@ const GridPage: React.FC = (): ReactNode => {
376395
title: 'Room',
377396
content: (
378397
<DougCmp
398+
enableScrollToNewLogLines={false}
379399
stream$={gameLogLineSubject$.pipe(
380400
rxjs.filter((m) => m.streamId === 'room')
381401
)}
@@ -387,6 +407,7 @@ const GridPage: React.FC = (): ReactNode => {
387407
title: 'Experience',
388408
content: (
389409
<DougCmp
410+
enableScrollToNewLogLines={false}
390411
stream$={gameLogLineSubject$.pipe(
391412
rxjs.filter((m) => m.streamId === 'experience')
392413
)}
@@ -398,6 +419,7 @@ const GridPage: React.FC = (): ReactNode => {
398419
title: 'Spells',
399420
content: (
400421
<DougCmp
422+
enableScrollToNewLogLines={false}
401423
stream$={gameLogLineSubject$.pipe(
402424
rxjs.filter((m) => m.streamId === 'percWindow')
403425
)}
@@ -409,6 +431,7 @@ const GridPage: React.FC = (): ReactNode => {
409431
title: 'Inventory',
410432
content: (
411433
<DougCmp
434+
enableScrollToNewLogLines={false}
412435
stream$={gameLogLineSubject$.pipe(
413436
rxjs.filter((m) => m.streamId === 'inv')
414437
)}
@@ -420,6 +443,7 @@ const GridPage: React.FC = (): ReactNode => {
420443
title: 'Familiar',
421444
content: (
422445
<DougCmp
446+
enableScrollToNewLogLines={true}
423447
stream$={gameLogLineSubject$.pipe(
424448
rxjs.filter((m) => m.streamId === 'familiar')
425449
)}
@@ -431,6 +455,7 @@ const GridPage: React.FC = (): ReactNode => {
431455
title: 'Thoughts',
432456
content: (
433457
<DougCmp
458+
enableScrollToNewLogLines={true}
434459
stream$={gameLogLineSubject$.pipe(
435460
rxjs.filter((m) => m.streamId === 'thoughts')
436461
)}
@@ -442,6 +467,7 @@ const GridPage: React.FC = (): ReactNode => {
442467
title: 'Combat',
443468
content: (
444469
<DougCmp
470+
enableScrollToNewLogLines={true}
445471
stream$={gameLogLineSubject$.pipe(
446472
rxjs.filter((m) => m.streamId === 'combat')
447473
)}
@@ -453,6 +479,7 @@ const GridPage: React.FC = (): ReactNode => {
453479
title: 'Logons',
454480
content: (
455481
<DougCmp
482+
enableScrollToNewLogLines={true}
456483
stream$={gameLogLineSubject$.pipe(
457484
rxjs.filter((m) => m.streamId === 'logons')
458485
)}
@@ -464,6 +491,7 @@ const GridPage: React.FC = (): ReactNode => {
464491
title: 'Deaths',
465492
content: (
466493
<DougCmp
494+
enableScrollToNewLogLines={true}
467495
stream$={gameLogLineSubject$.pipe(
468496
rxjs.filter((m) => m.streamId === 'death')
469497
)}
@@ -475,6 +503,7 @@ const GridPage: React.FC = (): ReactNode => {
475503
title: 'Main',
476504
content: (
477505
<DougCmp
506+
enableScrollToNewLogLines={true}
478507
stream$={gameLogLineSubject$.pipe(
479508
rxjs.filter((m) => m.streamId === '')
480509
)}

0 commit comments

Comments
 (0)