useViewRange.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { useCallback, useState } from 'react';
  2. import { ViewRangeTimeUpdate, ViewRange } from '@jaegertracing/jaeger-ui-components';
  3. /**
  4. * Controls state of the zoom function that can be used through minimap in header or on the timeline. ViewRange contains
  5. * state not only for current range that is showing but range that is currently being selected by the user.
  6. */
  7. export function useViewRange() {
  8. const [viewRange, setViewRange] = useState<ViewRange>({
  9. time: {
  10. current: [0, 1],
  11. },
  12. });
  13. const updateNextViewRangeTime = useCallback(function updateNextViewRangeTime(update: ViewRangeTimeUpdate) {
  14. setViewRange((prevRange): ViewRange => {
  15. const time = { ...prevRange.time, ...update };
  16. return { ...prevRange, time };
  17. });
  18. }, []);
  19. const updateViewRangeTime = useCallback(function updateViewRangeTime(start: number, end: number) {
  20. const current: [number, number] = [start, end];
  21. const time = { current };
  22. setViewRange((prevRange): ViewRange => {
  23. return { ...prevRange, time };
  24. });
  25. }, []);
  26. return { viewRange, updateViewRangeTime, updateNextViewRangeTime };
  27. }