Skip to content

Commit

Permalink
ui: save search query on cache for Transactions page
Browse files Browse the repository at this point in the history
Previously, a search  was not maintained when
the page change (e.g. coming back from Transaction details).
This commits saves the selected value to be used.

Partially adresses cockroachdb#71851

Release note: None
  • Loading branch information
maryliag committed Nov 24, 2021
1 parent 2965602 commit 68de9ab
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type Page =
| "Statements"
| "Statement Details"
| "Sessions"
| "Sessions Details";
| "Sessions Details"
| "Transactions"
| "Transaction Details";

type SearchEvent = {
name: "Keyword Searched";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type LocalStorageState = {
"filters/StatementsPage": Filters;
"filters/TransactionsPage": Filters;
"search/StatementsPage": string;
"search/TransactionsPage": string;
};

type Payload = {
Expand Down Expand Up @@ -88,6 +89,8 @@ const initialState: LocalStorageState = {
defaultFilters,
"search/StatementsPage":
JSON.parse(localStorage.getItem("search/StatementsPage")) || null,
"search/TransactionsPage":
JSON.parse(localStorage.getItem("search/TransactionsPage")) || null,
};

const localStorageSlice = createSlice({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ export const selectFilters = createSelector(
localStorageSelector,
localStorage => localStorage["filters/TransactionsPage"],
);

export const selectSearch = createSelector(
localStorageSelector,
localStorage => localStorage["search/TransactionsPage"],
);
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,34 @@ storiesOf("Transactions Page", module)
.add("with data", () => (
<TransactionsPage
{...routeProps}
columns={columns}
data={data}
dateRange={dateRange}
filters={filters}
nodeRegions={nodeRegions}
columns={columns}
onFilterChange={noop}
onSortingChange={noop}
refreshData={noop}
resetSQLStats={noop}
search={""}
sortSetting={sortSetting}
onSortingChange={noop}
filters={filters}
onFilterChange={noop}
/>
))
.add("without data", () => {
return (
<TransactionsPage
{...routeProps}
columns={columns}
data={getEmptyData()}
dateRange={dateRange}
filters={filters}
nodeRegions={nodeRegions}
columns={columns}
onFilterChange={noop}
onSortingChange={noop}
refreshData={noop}
resetSQLStats={noop}
search={""}
sortSetting={sortSetting}
onSortingChange={noop}
filters={filters}
onFilterChange={noop}
/>
);
})
Expand All @@ -75,58 +77,61 @@ storiesOf("Transactions Page", module)
return (
<TransactionsPage
{...routeProps}
columns={columns}
data={getEmptyData()}
dateRange={dateRange}
filters={filters}
history={history}
nodeRegions={nodeRegions}
columns={columns}
onFilterChange={noop}
onSortingChange={noop}
refreshData={noop}
history={history}
resetSQLStats={noop}
search={""}
sortSetting={sortSetting}
onSortingChange={noop}
filters={filters}
onFilterChange={noop}
/>
);
})
.add("with loading indicator", () => {
return (
<TransactionsPage
{...routeProps}
columns={columns}
data={undefined}
dateRange={dateRange}
filters={filters}
nodeRegions={nodeRegions}
columns={columns}
onFilterChange={noop}
onSortingChange={noop}
refreshData={noop}
resetSQLStats={noop}
search={""}
sortSetting={sortSetting}
onSortingChange={noop}
filters={filters}
onFilterChange={noop}
/>
);
})
.add("with error alert", () => {
return (
<TransactionsPage
{...routeProps}
columns={columns}
data={undefined}
dateRange={dateRange}
nodeRegions={nodeRegions}
columns={columns}
error={
new RequestError(
"Forbidden",
403,
"this operation requires admin privilege",
)
}
filters={filters}
nodeRegions={nodeRegions}
onFilterChange={noop}
onSortingChange={noop}
refreshData={noop}
resetSQLStats={noop}
search={""}
sortSetting={sortSetting}
onSortingChange={noop}
filters={filters}
onFilterChange={noop}
/>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ type Timestamp = protos.google.protobuf.ITimestamp;
const cx = classNames.bind(styles);

interface TState {
pagination: ISortedTablePagination;
search?: string;
aggregatedTs: Timestamp | null;
filters?: Filters;
pagination: ISortedTablePagination;
statementFingerprintIds: Long[] | null;
aggregatedTs: Timestamp | null;
transactionStats: TransactionStats | null;
transactionFingerprintId: Long | null;
transactionStats: TransactionStats | null;
}

export interface TransactionsPageStateProps {
columns: string[];
data: IStatementsResponse;
dateRange: [Moment, Moment];
nodeRegions: { [nodeId: string]: string };
error?: Error | null;
pageSize?: number;
filters: Filters;
isTenant?: UIConfigState["isTenant"];
columns: string[];
nodeRegions: { [nodeId: string]: string };
pageSize?: number;
search: string;
sortSetting: SortSetting;
filters: Filters;
}

export interface TransactionsPageDispatchProps {
Expand All @@ -101,6 +101,7 @@ export interface TransactionsPageDispatchProps {
onDateRangeChange?: (start: Moment, end: Moment) => void;
onColumnsChange?: (selectedColumns: string[]) => void;
onFilterChange?: (value: Filters) => void;
onSearchComplete?: (query: string) => void;
onSortingChange?: (
name: string,
columnTitle: string,
Expand Down Expand Up @@ -132,7 +133,6 @@ export class TransactionsPage extends React.Component<
pageSize: this.props.pageSize || 20,
current: 1,
},
search: "",
aggregatedTs: null,
statementFingerprintIds: null,
transactionStats: null,
Expand All @@ -143,29 +143,39 @@ export class TransactionsPage extends React.Component<
}

getStateFromHistory = (): Partial<TState> => {
const { history, sortSetting, filters } = this.props;
const {
history,
search,
sortSetting,
filters,
onSearchComplete,
onSortingChange,
onFilterChange,
} = this.props;
const searchParams = new URLSearchParams(history.location.search);

// Search query.
const searchQuery = searchParams.get("q") || undefined;
if (onSearchComplete && searchQuery && search != searchQuery) {
onSearchComplete(searchQuery);
}

// Sort Settings.
handleSortSettingFromQueryString(
"Transactions",
history.location.search,
sortSetting,
this.props.onSortingChange,
onSortingChange,
);

// Filters.
const latestFilter = handleFiltersFromQueryString(
history,
filters,
this.props.onFilterChange,
onFilterChange,
);

return {
search: searchQuery,
filters: latestFilter,
};
};
Expand All @@ -179,26 +189,40 @@ export class TransactionsPage extends React.Component<
this.refreshData();
}

updateQueryParamsOnTabSwitch(): void {
updateFiltersQueryParamsOnTab(
"Transactions",
this.state.filters,
this.props.history,
);
updateQueryParams(): void {
const { history, search, sortSetting } = this.props;
const tab = "Transactions";

// Search.
const searchParams = new URLSearchParams(history.location.search);
const currentTab = searchParams.get("tab") || "";
const searchQueryString = searchParams.get("q") || "";
if (currentTab === tab && search && search != searchQueryString) {
syncHistory(
{
q: search,
},
history,
);
}

// Filters.
updateFiltersQueryParamsOnTab(tab, this.state.filters, history);

// Sort Setting.
updateSortSettingQueryParamsOnTab(
"Transactions",
this.props.sortSetting,
tab,
sortSetting,
{
ascending: false,
columnTitle: "executionCount",
},
this.props.history,
history,
);
}

componentDidUpdate(): void {
this.updateQueryParamsOnTabSwitch();
this.updateQueryParams();
this.refreshData();
}

Expand Down Expand Up @@ -232,7 +256,9 @@ export class TransactionsPage extends React.Component<
};

onClearSearchField = (): void => {
this.setState({ search: "" });
if (this.props.onSearchComplete) {
this.props.onSearchComplete("");
}
syncHistory(
{
q: undefined,
Expand All @@ -242,7 +268,9 @@ export class TransactionsPage extends React.Component<
};

onSubmitSearchField = (search: string): void => {
this.setState({ search });
if (this.props.onSearchComplete) {
this.props.onSearchComplete(search);
}
this.resetPagination();
syncHistory(
{
Expand Down Expand Up @@ -340,8 +368,9 @@ export class TransactionsPage extends React.Component<
onColumnsChange,
columns: userSelectedColumnsToShow,
sortSetting,
search,
} = this.props;
const { pagination, search, filters } = this.state;
const { pagination, filters } = this.state;
const { statements, internal_app_name_prefix } = data;
const appNames = getTrxAppFilterOptions(
data.transactions,
Expand Down
Loading

0 comments on commit 68de9ab

Please sign in to comment.