Skip to content

Commit

Permalink
refactor according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry8192 committed Dec 2, 2024
1 parent ed2d79a commit 698dd28
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, {
useContext,
useRef,
useState,
} from "react";

Expand All @@ -22,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 @@ -36,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 @@ -45,24 +65,31 @@ const SearchTabPanel = () => {
const {queryProgress, queryResults, startQuery, uiState} = useContext(StateContext);
const [isAllExpanded, setIsAllExpanded] = useState<boolean>(true);
const [queryOptions, setQueryOptions] = useState<QUERY_OPTION[]>([]);
const queryStringRef = useRef<string>("");
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>) => {
queryStringRef.current = ev.target.value;
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);
if ("" !== queryStringRef.current) {
const isCaseSensitive = newOptions.includes(QUERY_OPTION.IS_CASE_SENSITIVE);
const isRegex = newOptions.includes(QUERY_OPTION.IS_REGEX);
startQuery(queryStringRef.current, isRegex, isCaseSensitive);
}
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 @@ -26,6 +26,7 @@ import {
FileSrcType,
MainWorkerRespMessage,
QUERY_PROGRESS_INIT,
QueryArgs,
QueryResults,
WORKER_REQ_CODE,
WORKER_RESP_CODE,
Expand Down Expand Up @@ -71,7 +72,7 @@ interface StateContextType {
loadFile: (fileSrc: FileSrcType, cursor: CursorType) => void,
loadPageByAction: (navAction: NavigationAction) => void,
setLogLevelFilter: (newLogLevelFilter: LogLevelFilter) => void,
startQuery: (queryString: string, isRegex: boolean, isCaseSensitive: boolean) => void,
startQuery: (queryArgs: QueryArgs) => void,
}
const StateContext = createContext<StateContextType>({} as StateContextType);

Expand Down Expand Up @@ -333,11 +334,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 @@ -105,6 +105,12 @@ type WorkerReqMap = {
},
};

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

type TextRange = [number, number];

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

0 comments on commit 698dd28

Please sign in to comment.