Skip to content

Commit

Permalink
Fixed Nodes focusing and highlighting to reflect JSON edits in Graph …
Browse files Browse the repository at this point in the history
…considering the SearchInput
  • Loading branch information
Shahidullah-Muffakir committed Feb 8, 2024
1 parent dbc198c commit 2dc2cac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/components/SearchInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { getHotkeyHandler } from "@mantine/hooks";
import ReactGA from "react-ga4";
import { AiOutlineSearch } from "react-icons/ai";
import { useFocusNode } from "src/hooks/useFocusNode";
import useGraph from "src/store/useGraph";

export const SearchInput: React.FC = () => {
const [searchValue, setValue, skip, nodeCount, currentNode] = useFocusNode();
const setSearchInputValue = useGraph(state => state.setSearchInputValue);

return (
<TextInput
Expand All @@ -15,7 +17,10 @@ export const SearchInput: React.FC = () => {
id="search-node"
w={180}
value={searchValue}
onChange={e => setValue(e.currentTarget.value)}
onChange={e => {
setValue(e.currentTarget.value);
setSearchInputValue(e.currentTarget.value);
}}
onFocus={() => ReactGA.event({ action: "focus_node_search", category: "User" })}
placeholder="Search Node"
onKeyDown={getHotkeyHandler([["Enter", skip]])}
Expand Down
9 changes: 7 additions & 2 deletions src/containers/Views/GraphView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ElkRoot } from "reaflow/dist/layout/useLayout";
import { useLongPress } from "use-long-press";
import { CustomNode } from "src/containers/Views/GraphView/CustomNode";
import { ViewMode } from "src/enums/viewMode.enum";
import { useFocusNode } from "src/hooks/useFocusNode";
import useToggleHide from "src/hooks/useToggleHide";
import { Loading } from "src/layout/Loading";
import useConfig from "src/store/useConfig";
Expand Down Expand Up @@ -163,7 +164,8 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
const gesturesEnabled = useConfig(state => state.gesturesEnabled);
const rulersEnabled = useConfig(state => state.rulersEnabled);
const setViewMode = useConfig(state => state.setViewMode);

const searchInputValue = useGraph(state => state.searchInputValue);
const [, setValue] = useFocusNode();
const callback = React.useCallback(() => {
const canvas = document.querySelector(".jsoncrack-canvas") as HTMLDivElement | null;
canvas?.classList.add("dragging");
Expand Down Expand Up @@ -209,7 +211,10 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
{...bindLongPress()}
>
<Space
onUpdated={() => debouncedOnZoomChangeHandler()}
onUpdated={() => {
setValue(searchInputValue);
debouncedOnZoomChangeHandler();
}}
onCreate={setViewPort}
onContextMenu={e => e.preventDefault()}
treatTwoFingerTrackPadGesturesLikeTouch={gesturesEnabled}
Expand Down
7 changes: 3 additions & 4 deletions src/hooks/useFocusNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const useFocusNode = () => {
const [debouncedValue] = useDebouncedValue(value, 600);

const skip = () => setSelectedNode(current => (current + 1) % nodeCount);

const matchedNodes: NodeListOf<Element> = searchQuery(`span[data-key*='${debouncedValue}' i]`);
const matchedNode: Element | null = matchedNodes[selectedNode] || null;
React.useEffect(() => {
if (!value) {
cleanupHighlight();
Expand All @@ -21,8 +22,6 @@ export const useFocusNode = () => {
}

if (!viewPort || !debouncedValue) return;
const matchedNodes: NodeListOf<Element> = searchQuery(`span[data-key*='${debouncedValue}' i]`);
const matchedNode: Element | null = matchedNodes[selectedNode] || null;

cleanupHighlight();

Expand All @@ -37,7 +36,7 @@ export const useFocusNode = () => {
setSelectedNode(0);
setNodeCount(0);
}
}, [selectedNode, debouncedValue, value, viewPort]);
}, [selectedNode, debouncedValue, value, viewPort, matchedNodes]);

Check warning on line 39 in src/hooks/useFocusNode.ts

View workflow job for this annotation

GitHub Actions / cache-and-install

React Hook React.useEffect has a missing dependency: 'matchedNode'. Either include it or remove the dependency array

return [value, setValue, skip, nodeCount, selectedNode] as const;
};
4 changes: 4 additions & 0 deletions src/store/useGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Graph {
collapsedParents: string[];
selectedNode: NodeData | null;
path: string;
searchInputValue: string;
}

const initialStates: Graph = {
Expand All @@ -37,6 +38,7 @@ const initialStates: Graph = {
collapsedParents: [],
selectedNode: null,
path: "",
searchInputValue:"",

Check failure on line 41 in src/store/useGraph.ts

View workflow job for this annotation

GitHub Actions / cache-and-install

Insert `·`
};

interface GraphActions {
Expand All @@ -59,6 +61,7 @@ interface GraphActions {
centerView: () => void;
clearGraph: () => void;
setZoomFactor: (zoomFactor: number) => void;
setSearchInputValue: (searchInputValue: string) => void;
}

const useGraph = create<Graph & GraphActions>((set, get) => ({
Expand Down Expand Up @@ -214,6 +217,7 @@ const useGraph = create<Graph & GraphActions>((set, get) => ({
},
toggleFullscreen: fullscreen => set({ fullscreen }),
setViewPort: viewPort => set({ viewPort }),
setSearchInputValue: searchInputValue => set({ searchInputValue }),
}));

export default useGraph;

0 comments on commit 2dc2cac

Please sign in to comment.