21364: added backup search when first search after f5 fails Arvados-DCO-1.1-Signed...
[arvados.git] / services / workbench2 / src / views-components / search-bar / search-bar-view.tsx
index eba281c9f0854c275454f61ee91142d7d59ab110..0c08e38b7d860533dd9ad7203a46ce0876f0649d 100644 (file)
@@ -24,6 +24,7 @@ import { KEY_CODE_DOWN, KEY_CODE_ESC, KEY_CODE_UP, KEY_ENTER } from "common/code
 import { debounce } from "debounce";
 import { Vocabulary } from "models/vocabulary";
 import { connectVocabulary } from "../resource-properties-form/property-field-common";
+import { Session } from "models/session";
 
 type CssRules = "container" | "containerSearchViewOpened" | "input" | "view";
 
@@ -64,6 +65,7 @@ interface SearchBarViewDataProps {
     isPopoverOpen: boolean;
     debounce?: number;
     vocabulary?: Vocabulary;
+    sessions: Session[];
 }
 
 export type SearchBarActionProps = SearchBarViewActionProps &
@@ -81,6 +83,7 @@ interface SearchBarViewActionProps {
     moveUp: () => void;
     moveDown: () => void;
     setAdvancedDataFromSearchValue: (search: string, vocabulary?: Vocabulary) => void;
+    searchSingleCluster: (session: Session, searchValue: string) => any;
 }
 
 type SearchBarViewProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
@@ -137,6 +140,10 @@ export const SearchBarView = compose(
     withStyles(styles)
 )(
     class extends React.Component<SearchBarViewProps> {
+        state={
+            loggedInSessions: [],
+        }
+
         debouncedSearch = debounce(() => {
             this.props.onSearch(this.props.searchValue);
         }, 1000);
@@ -151,6 +158,21 @@ export const SearchBarView = compose(
             this.props.onSubmit(event);
         };
 
+        componentDidMount(): void {
+            this.setState({ loggedInSessions: this.props.sessions.filter((ss) => ss.loggedIn && ss.userIsActive)});
+        }
+
+        componentDidUpdate( prevProps: Readonly<SearchBarViewProps>, prevState: Readonly<{loggedInSessions: Session[]}>, snapshot?: any ): void {
+            if (prevProps.sessions !== this.props.sessions) {
+                this.setState({ loggedInSessions: this.props.sessions.filter((ss) => ss.loggedIn)});
+            }
+            //if a new session is logged in after a search is started, search the new cluster and append those to the results
+            if(prevState.loggedInSessions.length !== this.state.loggedInSessions.length){
+                const newLogin = this.state.loggedInSessions.filter((ss) => !prevState.loggedInSessions.includes(ss));
+                this.props.searchSingleCluster(newLogin[0], this.props.searchValue);
+            }
+        }
+
         componentWillUnmount() {
             this.debouncedSearch.clear();
         }