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

fix(query): Restart log queries when case-sensitivity or regex button is toggled. #130

Merged
merged 12 commits into from
Dec 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
TAB_DISPLAY_NAMES,
TAB_NAME,
} from "../../../../../typings/tab";
import {QUERY_PROGRESS_DONE} from "../../../../../typings/worker";
import {
QUERY_PROGRESS_DONE,
QueryArgs,
} from "../../../../../typings/worker";
import {isDisabled} from "../../../../../utils/states";
import CustomTabPanel from "../CustomTabPanel";
import PanelTitleButton from "../PanelTitleButton";
Expand All @@ -35,6 +38,24 @@ enum QUERY_OPTION {
IS_REGEX = "isRegex"
}

/**
* Determines if the query is case-sensitive based on the provided query options.
*
* @param queryOptions
* @return True if the query is case-sensitive.
*/
const getIsCaseSensitive =
(queryOptions: QUERY_OPTION[]) => queryOptions.includes(QUERY_OPTION.IS_CASE_SENSITIVE);

/**
* Determines if the query is a regular expression based on the provided query options.
*
* @param queryOptions
* @return True if the query is a regular expression.
*/
const getIsRegex =
(queryOptions: QUERY_OPTION[]) => queryOptions.includes(QUERY_OPTION.IS_REGEX);

/**
* Displays a panel for submitting queries and viewing query results.
*
Expand All @@ -44,17 +65,31 @@ const SearchTabPanel = () => {
const {queryProgress, queryResults, startQuery, uiState} = useContext(StateContext);
const [isAllExpanded, setIsAllExpanded] = useState<boolean>(true);
const [queryOptions, setQueryOptions] = useState<QUERY_OPTION[]>([]);
const [queryString, setQueryString] = useState<string>("");

const handleQuerySubmit = (newArgs: Partial<QueryArgs>) => {
startQuery({
isCaseSensitive: getIsCaseSensitive(queryOptions),
isRegex: getIsRegex(queryOptions),
queryString: queryString,
...newArgs,
});
};

const handleQueryInputChange = (ev: React.ChangeEvent<HTMLTextAreaElement>) => {
const isCaseSensitive = queryOptions.includes(QUERY_OPTION.IS_CASE_SENSITIVE);
const isRegex = queryOptions.includes(QUERY_OPTION.IS_REGEX);
startQuery(ev.target.value, isRegex, isCaseSensitive);
setQueryString(ev.target.value);
handleQuerySubmit({queryString: ev.target.value});
};

const handleQueryOptionsChange = (
_: React.MouseEvent<HTMLElement>,
newOptions: QUERY_OPTION[]
) => {
setQueryOptions(newOptions);
handleQuerySubmit({
isCaseSensitive: getIsCaseSensitive(newOptions),
isRegex: getIsRegex(newOptions),
});
};

return (
Expand Down
10 changes: 4 additions & 6 deletions src/contexts/StateContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
FileSrcType,
MainWorkerRespMessage,
QUERY_PROGRESS_INIT,
QueryArgs,
QueryResults,
WORKER_REQ_CODE,
WORKER_RESP_CODE,
Expand Down Expand Up @@ -81,7 +82,7 @@ interface StateContextType {
loadFile: (fileSrc: FileSrcType, cursor: CursorType) => void,
loadPageByAction: (navAction: NavigationAction) => void,
setIsSettingsModalOpen: (isOpen: boolean) => void,
startQuery: (queryString: string, isRegex: boolean, isCaseSensitive: boolean) => void,
startQuery: (queryArgs: QueryArgs) => void,
}
const StateContext = createContext<StateContextType>({} as StateContextType);

Expand Down Expand Up @@ -361,11 +362,8 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
}
}, [postPopUp]);

const startQuery = useCallback((
queryString: string,
isRegex: boolean,
isCaseSensitive: boolean
) => {
const startQuery = useCallback((queryArgs: QueryArgs) => {
const {queryString, isRegex, isCaseSensitive} = queryArgs;
setQueryResults(STATE_DEFAULT.queryResults);
if (null === mainWorkerRef.current) {
console.error("Unexpected null mainWorkerRef.current");
Expand Down
7 changes: 7 additions & 0 deletions src/typings/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ type WorkerReqMap = {
},
};

interface QueryArgs {
queryString: string;
isCaseSensitive: boolean;
isRegex: boolean;
}

type TextRange = [number, number];

interface QueryResultsType {
Expand Down Expand Up @@ -192,6 +198,7 @@ export type {
FileSrcType,
MainWorkerReqMessage,
MainWorkerRespMessage,
QueryArgs,
QueryResults,
QueryResultsType,
WorkerReq,
Expand Down
Loading