-
Notifications
You must be signed in to change notification settings - Fork 2
History navigation #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
History navigation #495
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0806734
Added new navigation
opoliarush 5723888
raname
opoliarush b90779d
inital naviagtion
opoliarush 749a9de
update navigation steps
opoliarush 71a6378
Merge branch 'feature/navigation' into history-navigation
opoliarush f1d47e9
navigation
opoliarush b2999fe
fix navigation
opoliarush 14b6c91
Merge branch 'feature/navigation' into history-navigation
opoliarush 185ee2b
clean-up
opoliarush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/components/Navigation/ScopeNavigation/ScopeNavigation.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { Meta, StoryObj } from "@storybook/react"; | ||
|
|
||
| import { ScopeNavigation } from "."; | ||
|
|
||
| // More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction | ||
| const meta: Meta<typeof ScopeNavigation> = { | ||
| title: "Navigation/ScopeNavigation", | ||
| component: ScopeNavigation, | ||
| parameters: { | ||
| // More on how to position stories at: https://storybook.js.org/docs/react/configure/story-layout | ||
| layout: "fullscreen" | ||
| } | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| // More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args | ||
| export const Default: Story = { | ||
| args: {} | ||
| }; | ||
|
|
||
| export const Disabled: Story = { | ||
| args: {} | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { useContext, useEffect, useState } from "react"; | ||
| import { actions } from "../../../actions"; | ||
| import { dispatcher } from "../../../dispatcher"; | ||
| import { usePrevious } from "../../../hooks/usePrevious"; | ||
| import { HistoryManager, HistoryStep } from "../../../utils/historyManager"; | ||
| import { ConfigContext } from "../../common/App/ConfigContext"; | ||
| import { Scope } from "../../common/App/types"; | ||
| import { HistoryNavigationPanel } from "../HistoryNavigationPanel"; | ||
| import { ChangeEnvironmentPayload, ChangeViewPayload } from "../types"; | ||
| import { actions as globalActions } from "./../actions"; | ||
| import { ScopeNavigationProps } from "./types"; | ||
|
|
||
| const sendMessage = (historyStep: HistoryStep) => { | ||
| if (historyStep.scope) { | ||
| window.sendMessageToDigma({ | ||
| action: globalActions.CHANGE_SCOPE, | ||
| payload: { | ||
| ...historyStep.scope | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export const ScopeNavigation = (props: ScopeNavigationProps) => { | ||
| const [historyManager, setHistoryManager] = useState<HistoryManager>( | ||
| new HistoryManager() | ||
| ); | ||
| const { environment } = useContext(ConfigContext); | ||
| const previousTabId = usePrevious(props.currentTabId); | ||
| const previousSate = usePrevious(historyManager.getCurrent()); | ||
|
|
||
| useEffect(() => { | ||
| const currentStep = historyManager.getCurrent(); | ||
| if ( | ||
| previousSate?.scope.span?.spanCodeObjectId === | ||
| currentStep?.scope.span?.spanCodeObjectId | ||
| ) { | ||
| if (previousTabId !== props.currentTabId) { | ||
| historyManager.updateCurrent({ tabId: props.currentTabId }); | ||
| } | ||
| } | ||
| }, [previousTabId, props.currentTabId, previousSate]); | ||
|
|
||
| useEffect(() => { | ||
| const handleSetScope = (data: unknown) => { | ||
| const newScope = data as Scope; | ||
| const currentScope = historyManager.getCurrent()?.scope; | ||
| if ( | ||
| !currentScope || | ||
| currentScope.span?.spanCodeObjectId !== newScope.span?.spanCodeObjectId | ||
| ) { | ||
| historyManager.push({ | ||
| environment: environment || null, | ||
| scope: newScope, | ||
| tabId: null | ||
| }); | ||
| } else { | ||
| const historyStep = historyManager.getCurrent(); | ||
|
|
||
| if (historyStep && historyStep.tabId) { | ||
| window.sendMessageToDigma<ChangeViewPayload>({ | ||
| action: globalActions.CHANGE_VIEW, | ||
| payload: { | ||
| view: historyStep.tabId | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (historyStep && historyStep.environment) { | ||
| window.sendMessageToDigma<ChangeEnvironmentPayload>({ | ||
| action: globalActions.CHANGE_ENVIRONMENT, | ||
| payload: { | ||
| environment: historyStep.environment | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| dispatcher.addActionListener(actions.SET_SCOPE, handleSetScope); | ||
|
|
||
| return () => { | ||
| dispatcher.removeActionListener(actions.SET_SCOPE, handleSetScope); | ||
| }; | ||
| }, [environment, props.currentTabId, historyManager]); | ||
|
|
||
| const handleBackClick = () => { | ||
| const currentStep = historyManager.back(); | ||
| if (currentStep) { | ||
| sendMessage(currentStep); | ||
| } | ||
| }; | ||
|
|
||
| const handleNexClick = () => { | ||
| const currentStep = historyManager.forward(); | ||
| if (currentStep) { | ||
| sendMessage(currentStep); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <HistoryNavigationPanel | ||
| isBackDisabled={!historyManager.canMoveBack()} | ||
| isForwardDisabled={!historyManager.canMoveForward()} | ||
| onGoBack={handleBackClick} | ||
| onGoForward={handleNexClick} | ||
| /> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import styled from "styled-components"; | ||
|
|
||
| export const NavigationButton = styled.button` | ||
| height: 28px; | ||
| width: 28px; | ||
|
|
||
| &:not([disabled]) { | ||
| border: none; | ||
| } | ||
|
|
||
| &:disabled { | ||
| color: ${({ theme }) => theme.colors.v3.icon.disabled}; | ||
| } | ||
| `; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export interface ScopeNavigationProps { | ||
| currentTabId: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { Environment, Scope } from "../components/common/App/types"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename this file with |
||
|
|
||
| const MAX_STEPS = 15; | ||
|
|
||
| export interface HistoryStep { | ||
| scope: Scope; | ||
| environment?: Environment | null; | ||
| tabId: string | null; | ||
| } | ||
|
|
||
| export interface UpdateStepParams { | ||
| scope?: Scope; | ||
| environment?: Environment | null; | ||
| tabId?: string; | ||
| } | ||
|
|
||
| export interface HistoryData { | ||
| steps: HistoryStep[]; | ||
| currentStepIndex: number; | ||
| } | ||
|
|
||
| interface Node<T> { | ||
| next: Node<T> | null; | ||
| previous: Node<T> | null; | ||
| value: T; | ||
| } | ||
|
|
||
| export class HistoryManager { | ||
| private head: Node<HistoryStep> | null = null; | ||
| private tail: Node<HistoryStep> | null = null; | ||
|
|
||
| private current: Node<HistoryStep> | null = null; | ||
| private itemsCount = 0; | ||
| private currentIndex = -1; | ||
|
|
||
| constructor(data?: HistoryData) { | ||
| if (data) { | ||
| this.init(data.steps, data.currentStepIndex); | ||
| } | ||
| } | ||
|
|
||
| push(step: HistoryStep) { | ||
| const newNode: Node<HistoryStep> = { | ||
| value: step, | ||
| previous: null, | ||
| next: null | ||
| }; | ||
|
|
||
| if (this.current != this.tail) { | ||
| this.tail = this.current; | ||
| this.itemsCount = this.itemsCount - (this.itemsCount - this.currentIndex); | ||
| } | ||
|
|
||
| if (this.tail) { | ||
| this.tail.next = newNode; | ||
| newNode.previous = this.tail; | ||
| } | ||
|
|
||
| if (!this.head) { | ||
| this.head = newNode; | ||
| } | ||
|
|
||
| this.tail = newNode; | ||
| this.current = newNode; | ||
| this.currentIndex++; | ||
|
|
||
| if (this.itemsCount === MAX_STEPS && this.head?.next) { | ||
| this.head = this.head.next; | ||
| } else { | ||
| this.itemsCount++; | ||
| } | ||
| } | ||
|
|
||
| canMoveBack() { | ||
| if (!this.current?.previous) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| back() { | ||
| if (!this.current?.previous) { | ||
| return null; | ||
| } | ||
|
|
||
| this.current = this.current.previous; | ||
| this.currentIndex--; | ||
| return this.getCurrent(); | ||
| } | ||
|
|
||
| canMoveForward() { | ||
| if (!this.current?.next) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| forward() { | ||
| if (!this.current?.next) { | ||
| return null; | ||
| } | ||
|
|
||
| this.current = this.current.next; | ||
| this.currentIndex++; | ||
| return this.getCurrent(); | ||
| } | ||
|
|
||
| getLast() { | ||
| return this.tail?.value; | ||
| } | ||
|
|
||
| getCurrent() { | ||
| return this.current?.value; | ||
| } | ||
|
|
||
| updateCurrent(newValue: UpdateStepParams) { | ||
| if (this.current) { | ||
| this.current.value = { ...this.current.value, ...newValue }; | ||
| } | ||
| } | ||
|
|
||
| getHistoryData() { | ||
| const steps = []; | ||
| let step = this.head; | ||
| while (step) { | ||
| steps.push(step.value); | ||
| step = step.next; | ||
| } | ||
|
|
||
| return { | ||
| steps, | ||
| currentStepIndex: this.currentIndex | ||
| }; | ||
| } | ||
|
|
||
| private init(steps: HistoryStep[], selectedStep: number) { | ||
| if (!steps.length) { | ||
| return; | ||
| } | ||
|
|
||
| let currentIndex = 0; | ||
|
|
||
| let localCurrent = null; | ||
| do { | ||
| this.push(steps[currentIndex]); | ||
|
|
||
| if (selectedStep === currentIndex) { | ||
| localCurrent = this.current; | ||
| } | ||
|
|
||
| currentIndex++; | ||
| } while (currentIndex < steps.length); | ||
|
|
||
| this.itemsCount = steps.length; | ||
| this.currentIndex = selectedStep; | ||
| this.current = localCurrent; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file as not used