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

Add dependency array to useCreateSavedSearch hook #21338

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/issue-20465.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type = "f"
message = "Fixed the issue when the streams link in the message table was not working on the streams page."

issues=["20465"]
pulls = ["21338"]
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@ import type { ElasticsearchQueryString, TimeRange } from 'views/logic/queries/Qu
import ViewGenerator from 'views/logic/views/ViewGenerator';
import type Parameter from 'views/logic/parameters/Parameter';

type Props = {
streamId?: string | string[],
streamCategory?: string | string[],
timeRange?: TimeRange,
queryString?: ElasticsearchQueryString,
parameters?: Array<Parameter>,
}

type Deps = Array<Props[keyof Props]> | [];
const useCreateSavedSearch = ({
streamId,
streamCategory,
timeRange,
queryString,
parameters,
}:{
streamId?: string | string[],
streamCategory?: string | string[],
timeRange?: TimeRange,
queryString?: ElasticsearchQueryString,
parameters?: Array<Parameter>,
}) => useMemo(
}:Props, deps: Deps = []) => useMemo(
() => ViewGenerator({ type: View.Type.Search, streamId, streamCategory, timeRange, queryString, parameters }),
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
deps,
);

export default useCreateSavedSearch;
11 changes: 6 additions & 5 deletions graylog2-web-interface/src/views/pages/StreamSearchPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,28 @@ describe('StreamSearchPage', () => {
queryString: undefined,
streamId: 'stream-id-1',
timeRange: undefined,
}));
}, [streamId]));
});

it('should recreate view when streamId passed from props changes', async () => {
it('should rerender hook when streamId passed from props changes', async () => {
const { rerender } = render(<SimpleStreamSearchPage />);

await waitFor(() => expect(useCreateSavedSearch).toHaveBeenCalledWith({
queryString: undefined,
streamId: 'stream-id-1',
timeRange: undefined,
}));
}, [streamId]));

asMock(useParams).mockReturnValue({ streamId: 'stream-id-2' });
const secondStream = 'stream-id-2';
asMock(useParams).mockReturnValue({ streamId: secondStream });

rerender(<SimpleStreamSearchPage />);

await waitFor(() => expect(useCreateSavedSearch).toHaveBeenLastCalledWith({
queryString: undefined,
streamId: 'stream-id-2',
timeRange: undefined,
}));
}, [secondStream]));
});

describe('loading another view', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const StreamSearchPage = () => {
}

const { timeRange, queryString } = useSearchURLQueryParams();
const viewPromise = useCreateSavedSearch({ streamId, timeRange, queryString });
const viewPromise = useCreateSavedSearch({ streamId, timeRange, queryString }, [streamId]);
const view = useCreateSearch(viewPromise);

const _loadNewView = useCallback(() => loadNewViewForStream(history, streamId), [history, streamId]);
Expand Down
Loading