Skip to content
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

Checking that there is indeed a selection before scrolling #257

Merged
merged 11 commits into from
Feb 20, 2024
31 changes: 26 additions & 5 deletions src/SeqViewerContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ class SeqViewerContainer extends React.Component<SeqViewerContainerProps, SeqVie
};
}

selectionIsProgramatic(selection: any): selection is Selection {
// If the selection was done programatically, it has not type
return !selection.type;
}

// If the selection prop updates, also scroll the linear view to the new selection
componentDidUpdate = (prevProps: SeqViewerContainerProps) => {
// Only scroll if the selection was done passed in as a prop by a user of SeqViz. Otherwise the selection was
// made by the user clicking an element or selecting a range of sequences
if (this.selectionIsProgramatic(this.props.selection)) {
if (
this.props.selection?.start !== prevProps.selection?.start &&
this.props.selection?.start !== this.props.selection?.end
) {
this.setCentralIndex("LINEAR", this.props.selection?.start || 0);
}
}
};

/** this is here because the size listener is returning a new "size" prop every time */
shouldComponentUpdate = (nextProps: SeqViewerContainerProps, nextState: any) =>
!isEqual(nextProps, this.props) || !isEqual(nextState, this.state);
Expand Down Expand Up @@ -125,11 +144,13 @@ class SeqViewerContainer extends React.Component<SeqViewerContainerProps, SeqVie
*/
getSelection = (
state: Selection,
prop?: {
clockwise?: boolean;
end: number;
start: number;
}
prop?:
| {
clockwise?: boolean;
end: number;
start: number;
}
| Selection
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't need to change either, it's 1:1 w/ the prop that users pass in so it doesn't have those other fields.

Looking thru it quickly, a few lines (maybe more) that need to change:

Copy link
Collaborator Author

@guzmanvig guzmanvig Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is right, the selection in the state has to be a Selection because it is then use everywhere else by passing it in <SelectionContext.Provider value={mergedSelection}>. And the getSelection function purpose is to convert the selection in props to a Selection:

  getSelection = (state: Selection, prop?: ExternalSelection): Selection => {
    if (prop) {
      return { ...prop, clockwise: typeof prop.clockwise === "undefined" || !!prop.clockwise, type: "" };
    }
    return state;
  };

I've added an ExternalSelection interface now to make it clearer, let me know what you think.

): Selection => {
if (prop) {
return { ...prop, clockwise: typeof prop.clockwise === "undefined" || !!prop.clockwise, type: "" };
Expand Down
Loading