Skip to content

Commit 106959e

Browse files
committed
Refactor number translator
Signed-off-by: Anh Hoang Nguyen <[email protected]>
1 parent 49b9e1c commit 106959e

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

packages/react-components/src/components/trace-context-component.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,7 @@ export class TraceContextComponent extends React.Component<TraceContextProps, Tr
191191
const viewRangeStart = viewRange.getStart() - (offset ? offset : BigInt(0));
192192
const viewRangeEnd = viewRange.getEnd() - (offset ? offset : BigInt(0));
193193
this.unitController = new TimeGraphUnitController(absoluteRange, { start: viewRangeStart, end: viewRangeEnd });
194-
this.unitController.numberTranslator = this.unitController.numberTranslator = (theNumber: bigint) => {
195-
const originalStart = this.state.currentRange.getStart();
196-
theNumber += originalStart;
197-
const zeroPad = (num: bigint) => String(num).padStart(3, '0');
198-
const seconds = theNumber / BigInt(1000000000);
199-
const millis = zeroPad((theNumber / BigInt(1000000)) % BigInt(1000));
200-
const micros = zeroPad((theNumber / BigInt(1000)) % BigInt(1000));
201-
const nanos = zeroPad(theNumber % BigInt(1000));
202-
return seconds + '.' + millis + ' ' + micros + ' ' + nanos;
203-
};
194+
this.unitController.numberTranslator = createNumberTranslator(true, this.state.currentRange.getStart());
204195
this.unitController.worldRenderFactor = 0.25;
205196
this.historyHandler = new UnitControllerHistoryHandler(this.unitController);
206197
if (this.props.persistedState?.currentTimeSelection) {
@@ -793,14 +784,19 @@ export class TraceContextComponent extends React.Component<TraceContextProps, Tr
793784
ganttChartRange.getDuration(),
794785
{ start: ganttChartRange.getStart(), end: ganttChartRange.getEnd() }
795786
);
796-
ganttChartUnitController.numberTranslator = (theNumber: bigint) => {
797-
const zeroPad = (num: bigint) => String(num).padStart(3, '0');
798-
const seconds = theNumber / BigInt(1000000000);
799-
const millis = zeroPad((theNumber / BigInt(1000000)) % BigInt(1000));
800-
const micros = zeroPad((theNumber / BigInt(1000)) % BigInt(1000));
801-
const nanos = zeroPad(theNumber % BigInt(1000));
802-
return seconds + '.' + millis + ' ' + micros + ' ' + nanos;
803-
};
787+
ganttChartUnitController.numberTranslator = createNumberTranslator(false);
788+
// Restore view range if available, otherwise set to global view range
789+
const fgViewRange = this.state.ganttChartRanges?.[output.id];
790+
if (fgViewRange) {
791+
ganttChartUnitController.viewRange = fgViewRange;
792+
} else {
793+
// Use the global view range from the parent unit controller
794+
const globalViewRange = this.unitController.viewRange;
795+
ganttChartUnitController.viewRange = {
796+
start: globalViewRange.start,
797+
end: globalViewRange.end
798+
};
799+
}
804800
// Listen for view range changes
805801
ganttChartUnitController.onViewRangeChanged((_old, newRange) => {
806802
this.handleGanttChartViewRangeChange(output.id, newRange);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default function createNumberTranslator(
2+
isTimeBased: boolean,
3+
originalStart?: bigint
4+
): (theNumber: bigint) => string {
5+
return function (theNumber: bigint): string {
6+
if (isTimeBased && originalStart) {
7+
theNumber += originalStart;
8+
}
9+
const zeroPad = (num: bigint) => String(num).padStart(3, '0');
10+
const seconds = theNumber / BigInt(1000000000);
11+
const millis = zeroPad((theNumber / BigInt(1000000)) % BigInt(1000));
12+
const micros = zeroPad((theNumber / BigInt(1000)) % BigInt(1000));
13+
const nanos = zeroPad(theNumber % BigInt(1000));
14+
return seconds + '.' + millis + ' ' + micros + ' ' + nanos;
15+
};
16+
}

0 commit comments

Comments
 (0)