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

feat: Make exact table search result auto show up #1106

Merged
merged 4 commits into from
Dec 20, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
max-height: 80%;
box-shadow: var(--box-shadow-sm);

.DataTableViewMini {
> .DataTableViewMini-wrapper {
border-top: var(--border);
height: 100%;
margin-left: 8px;
margin-left: 4px;
display: flex;
flex-direction: column;

.Message {
margin: 0;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import Resizable from 're-resizable';
import React from 'react';
import React, { useEffect } from 'react';

import { DataTableNavigator } from 'components/DataTableNavigator/DataTableNavigator';
import { DataTableViewMini } from 'components/DataTableViewMini/DataTableViewMini';
import { useExactMatchTableId } from 'hooks/table/useExactMatchTableId';
import { enableResizable } from 'lib/utils';
import { getCurrentEnv } from 'lib/utils/query-string';
import { FullHeight } from 'ui/FullHeight/FullHeight';
import { Message } from 'ui/Message/Message';

import './DataDocSchemaNavigator.scss';

export const DataDocSchemaNavigator: React.FunctionComponent = () => {
const [tableId, setTableId] = React.useState<number>(null);
const exactMatchId = useExactMatchTableId();

useEffect(() => {
if (exactMatchId) {
setTableId(exactMatchId);
}
}, [exactMatchId]);

const schemaPanel = (
<DataTableNavigator
Expand All @@ -24,28 +33,41 @@ export const DataDocSchemaNavigator: React.FunctionComponent = () => {
selectedTableId={tableId}
/>
);
const tableView = tableId && (

const tableViewDOM = tableId ? (
<div className="DataTableViewMini-wrapper">
{tableId === exactMatchId && (
<div className="p8">
<Message type="success" size="small">
Exact table match found.
</Message>
</div>
)}

<DataTableViewMini
tableId={tableId}
onHide={() => setTableId(null)}
/>
</div>
) : null;

const tableViewContainer = tableViewDOM && (
<Resizable
className="schema-info-panel"
defaultSize={{
width: '100%',
height: '600px',
}}
enable={enableResizable({ top: true, bottom: true })}
// minHeight={'200px'}
// maxHeight={'900px'}
>
<DataTableViewMini
tableId={tableId}
onHide={() => setTableId(null)}
/>
{tableViewDOM}
</Resizable>
);

const contentDOM = (
<FullHeight flex="column" className="DataDocSchemaNavigator">
{schemaPanel}
{tableView}
{tableViewContainer}
</FullHeight>
);

Expand Down
42 changes: 42 additions & 0 deletions querybook/webapp/hooks/table/useExactMatchTableId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useShallowSelector } from 'hooks/redux/useShallowSelector';
import { useMemo } from 'react';
import { IStoreState } from 'redux/store/types';

export function useExactMatchTableId() {
const { dataTables, searchString, searchFilters } = useShallowSelector(
(state: IStoreState) => ({
dataTables: state.dataTableSearch.results,
searchString: state.dataTableSearch.searchString,
searchFilters: state.dataTableSearch.searchFilters,
})
);

const [searchStringSchema, searchStringTable] = useMemo(() => {
const trimmedStr = searchString.trim();
const separatedStrs = trimmedStr.split('.');

if (separatedStrs.length > 2) {
return [null, null];
}

if (separatedStrs.length === 1) {
return [searchFilters.schema, separatedStrs[0]];
} else if (separatedStrs.length === 2) {
return separatedStrs;
}
}, [searchString, searchFilters]);

const exactMatchTableId = useMemo(() => {
if (!searchStringSchema || !searchStringTable) {
return null;
}

return dataTables.find(
(table) =>
table.name === searchStringTable &&
table.schema === searchStringSchema
)?.id;
Comment on lines +34 to +38
Copy link
Collaborator

Choose a reason for hiding this comment

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

would it be simpler to find by joining the schema and name?

dataTables.find(
            (table) =>
                `${table.schema}.${table.name}` === searchString.trim()
        )

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point, i also wanted to include filters as well. Updated the PR

}, [searchStringSchema, searchStringTable, dataTables]);

return exactMatchTableId;
}