Skip to content

Commit

Permalink
Abstract out SidebarCloseButton (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcginnes authored Jan 23, 2025
1 parent ca5680e commit 281ffa7
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { atom, DefaultValue, selectorFamily } from "recoil";
import { atom, DefaultValue, selectorFamily, useSetRecoilState } from "recoil";
import localForageEffect from "./localForageEffect";
import { useCallback } from "react";

export type ShapeStyle =
| "rectangle"
Expand Down Expand Up @@ -212,3 +213,13 @@ export const userLayoutAtom = atom<UserPreferences["layout"]>({
},
effects: [localForageEffect()],
});

export function useCloseSidebar() {
const setUserLayout = useSetRecoilState(userLayoutAtom);
return useCallback(() => {
setUserLayout(prev => ({
...prev,
activeSidebarItem: null,
}));
}, [setUserLayout]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@ import {
PanelContent,
PanelHeader,
PanelHeaderActions,
PanelHeaderCloseButton,
PanelHeaderCloseButtonProps,
PanelTitle,
} from "@/components";
import { useDisplayEdgeTypeConfigs } from "@/core";
import useTranslations from "@/hooks/useTranslations";
import SingleEdgeStyling from "./SingleEdgeStyling";
import { SidebarCloseButton } from "../SidebarCloseButton";

export type EdgesStylingProps = Pick<PanelHeaderCloseButtonProps, "onClose"> & {
export type EdgesStylingProps = {
onEdgeCustomize(edgeType?: string): void;
customizeEdgeType?: string;
};

const EdgesStyling = ({
function EdgesStyling({
customizeEdgeType,
onEdgeCustomize,
onClose,
}: EdgesStylingProps) => {
}: EdgesStylingProps) {
const etConfigs = useDisplayEdgeTypeConfigs().values().toArray();
const t = useTranslations();

Expand All @@ -31,7 +29,7 @@ const EdgesStyling = ({
<PanelHeader>
<PanelTitle>{t("edges-styling.title")}</PanelTitle>
<PanelHeaderActions>
<PanelHeaderCloseButton onClose={onClose} />
<SidebarCloseButton />
</PanelHeaderActions>
</PanelHeader>
<PanelContent className="flex flex-col gap-2">
Expand All @@ -53,6 +51,6 @@ const EdgesStyling = ({
</PanelContent>
</Panel>
);
};
}

export default EdgesStyling;
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import type { PanelHeaderCloseButtonProps } from "@/components";
import {
CheckboxList,
Divider,
Panel,
PanelContent,
PanelHeader,
PanelHeaderActions,
PanelHeaderCloseButton,
PanelTitle,
} from "@/components";
import useTranslations from "@/hooks/useTranslations";
import useFiltersConfig from "./useFiltersConfig";
import { PropsWithChildren } from "react";
import { SidebarCloseButton } from "../SidebarCloseButton";

export type EntitiesFilterProps = Pick<PanelHeaderCloseButtonProps, "onClose">;

const EntitiesFilter = ({ onClose }: EntitiesFilterProps) => {
function EntitiesFilter() {
const t = useTranslations();

const {
Expand All @@ -34,7 +31,7 @@ const EntitiesFilter = ({ onClose }: EntitiesFilterProps) => {
<PanelHeader>
<PanelTitle>Entities Filter</PanelTitle>
<PanelHeaderActions>
<PanelHeaderCloseButton onClose={onClose} />
<SidebarCloseButton />
</PanelHeaderActions>
</PanelHeader>
<PanelContent>
Expand Down Expand Up @@ -64,7 +61,7 @@ const EntitiesFilter = ({ onClose }: EntitiesFilterProps) => {
</PanelContent>
</Panel>
);
};
}

function CheckboxListContainer(props: PropsWithChildren) {
return <div className="w-full px-3 py-2">{props.children}</div>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default } from "./EntitiesFilter";
export type { EntitiesFilterProps } from "./EntitiesFilter";
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useRecoilState } from "recoil";
import type { PanelHeaderCloseButtonProps } from "@/components";
import {
AutoFitLeftIcon,
Panel,
PanelContent,
PanelHeader,
PanelHeaderActionButton,
PanelHeaderActions,
PanelHeaderCloseButton,
PanelHeaderDivider,
PanelTitle,
} from "@/components";
Expand All @@ -17,10 +15,9 @@ import { userLayoutAtom } from "@/core/StateProvider/userPreferences";
import EdgeDetail from "./EdgeDetail";
import NodeDetail from "./NodeDetail";
import { useSelectedDisplayEdges, useSelectedDisplayVertices } from "@/core";
import { SidebarCloseButton } from "../SidebarCloseButton";

export type EntityDetailsProps = Pick<PanelHeaderCloseButtonProps, "onClose">;

const EntityDetails = ({ onClose }: EntityDetailsProps) => {
function EntityDetails() {
const [userLayout, setUserLayout] = useRecoilState(userLayoutAtom);
const selectedNodes = useSelectedDisplayVertices();
const selectedNode = selectedNodes[0];
Expand All @@ -47,7 +44,7 @@ const EntityDetails = ({ onClose }: EntityDetailsProps) => {
}
/>
<PanelHeaderDivider />
<PanelHeaderCloseButton onClose={onClose} />
<SidebarCloseButton />
</PanelHeaderActions>
</PanelHeader>
<PanelContent>
Expand All @@ -74,6 +71,6 @@ const EntityDetails = ({ onClose }: EntityDetailsProps) => {
</PanelContent>
</Panel>
);
};
}

export default EntityDetails;
1 change: 0 additions & 1 deletion packages/graph-explorer/src/modules/EntityDetails/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default } from "./EntityDetails";
export type { EntityDetailsProps } from "./EntityDetails";
17 changes: 5 additions & 12 deletions packages/graph-explorer/src/modules/Namespaces/Namespaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
PanelContent,
PanelHeader,
PanelHeaderActions,
PanelHeaderCloseButton,
PanelHeaderCloseButtonProps,
PanelHeaderDivider,
PanelTitle,
Select,
Expand All @@ -14,10 +12,9 @@ import { useConfiguration } from "@/core";
import CommonPrefixes from "./CommonPrefixes";
import GeneratedPrefixes from "./GeneratedPrefixes";
import UserPrefixes from "./UserPrefixes";
import { SidebarCloseButton } from "../SidebarCloseButton";

export type EdgesStylingProps = Pick<PanelHeaderCloseButtonProps, "onClose">;

const Namespaces = ({ onClose }: EdgesStylingProps) => {
function Namespaces() {
const config = useConfiguration();
const [nsType, setNsType] = useState("auto");

Expand Down Expand Up @@ -54,12 +51,8 @@ const Namespaces = ({ onClose }: EdgesStylingProps) => {
noMargin={true}
size="sm"
/>
{onClose ? (
<>
<PanelHeaderDivider />
<PanelHeaderCloseButton onClose={onClose} />
</>
) : null}
<PanelHeaderDivider />
<SidebarCloseButton />
</PanelHeaderActions>
</PanelHeader>

Expand All @@ -70,6 +63,6 @@ const Namespaces = ({ onClose }: EdgesStylingProps) => {
</PanelContent>
</Panel>
);
};
}

export default Namespaces;
11 changes: 4 additions & 7 deletions packages/graph-explorer/src/modules/NodeExpand/NodeExpand.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useRecoilValue } from "recoil";
import type { PanelHeaderCloseButtonProps } from "@/components";
import {
Panel,
PanelContent,
PanelHeader,
PanelHeaderActions,
PanelHeaderCloseButton,
PanelTitle,
} from "@/components";
import GraphIcon from "@/components/icons/GraphIcon";
Expand All @@ -14,10 +12,9 @@ import { edgesSelectedIdsAtom } from "@/core/StateProvider/edges";
import useTranslations from "@/hooks/useTranslations";
import NodeExpandContent from "./NodeExpandContent";
import { useSelectedDisplayVertices } from "@/core";
import { SidebarCloseButton } from "../SidebarCloseButton";

export type NodeExpandProps = Pick<PanelHeaderCloseButtonProps, "onClose">;

const NodeExpand = ({ onClose }: NodeExpandProps) => {
function NodeExpand() {
const t = useTranslations();
const edgesSelectedIds = useRecoilValue(edgesSelectedIdsAtom);
const selectedNodes = useSelectedDisplayVertices();
Expand All @@ -28,7 +25,7 @@ const NodeExpand = ({ onClose }: NodeExpandProps) => {
<PanelHeader>
<PanelTitle>Expand</PanelTitle>
<PanelHeaderActions>
<PanelHeaderCloseButton onClose={onClose} />
<SidebarCloseButton />
</PanelHeaderActions>
</PanelHeader>
<PanelContent>
Expand Down Expand Up @@ -59,6 +56,6 @@ const NodeExpand = ({ onClose }: NodeExpandProps) => {
</PanelContent>
</Panel>
);
};
}

export default NodeExpand;
1 change: 0 additions & 1 deletion packages/graph-explorer/src/modules/NodeExpand/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default } from "./NodeExpand";
export type { NodeExpandProps } from "./NodeExpand";
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import {
PanelContent,
PanelHeader,
PanelHeaderActions,
PanelHeaderCloseButton,
PanelHeaderCloseButtonProps,
PanelTitle,
} from "@/components";
import { useDisplayVertexTypeConfigs } from "@/core";
import useTranslations from "@/hooks/useTranslations";
import SingleNodeStyling from "./SingleNodeStyling";
import { Fragment } from "react/jsx-runtime";
import { SidebarCloseButton } from "../SidebarCloseButton";

export type NodesStylingProps = Pick<PanelHeaderCloseButtonProps, "onClose"> & {
export type NodesStylingProps = {
onNodeCustomize(nodeType?: string): void;
customizeNodeType?: string;
};

const NodesStyling = ({
function NodesStyling({
customizeNodeType,
onNodeCustomize,
onClose,
}: NodesStylingProps) => {
}: NodesStylingProps) {
const vtConfigs = useDisplayVertexTypeConfigs().values().toArray();
const t = useTranslations();

Expand All @@ -31,7 +29,7 @@ const NodesStyling = ({
<PanelHeader>
<PanelTitle>{t("nodes-styling.title")}</PanelTitle>
<PanelHeaderActions>
<PanelHeaderCloseButton onClose={onClose} />
<SidebarCloseButton />
</PanelHeaderActions>
</PanelHeader>
<PanelContent className="gap-2">
Expand All @@ -53,6 +51,6 @@ const NodesStyling = ({
</PanelContent>
</Panel>
);
};
}

export default NodesStyling;
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ import {
PanelContent,
PanelHeader,
PanelHeaderActions,
PanelHeaderCloseButton,
PanelHeaderCloseButtonProps,
PanelTitle,
} from "@/components";
import { FilterSearchTabContent } from "./FilterSearchTabContent";
import { SidebarCloseButton } from "../SidebarCloseButton";

export type SearchSidebarPanelProps = Pick<
PanelHeaderCloseButtonProps,
"onClose"
>;

export function SearchSidebarPanel({ onClose }: SearchSidebarPanelProps) {
export function SearchSidebarPanel() {
return (
<Panel variant="sidebar">
<PanelHeader>
<PanelTitle>Search</PanelTitle>
<PanelHeaderActions>
<PanelHeaderCloseButton onClose={onClose} />
<SidebarCloseButton />
</PanelHeaderActions>
</PanelHeader>
<PanelContent>
Expand Down
8 changes: 8 additions & 0 deletions packages/graph-explorer/src/modules/SidebarCloseButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PanelHeaderCloseButton } from "@/components";
import { useCloseSidebar } from "@/core";

export function SidebarCloseButton() {
const closeSidebar = useCloseSidebar();

return <PanelHeaderCloseButton onClose={closeSidebar} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ const GraphExplorer = () => {

const filteredEntitiesCount = useRecoilValue(totalFilteredCount);

const closeSidebar = useCallback(() => {
setUserLayout(prev => ({
...prev,
activeSidebarItem: null,
}));
}, [setUserLayout]);

const toggleSidebar = useCallback(
(item: SidebarItems) => () => {
setUserLayout(prev => {
Expand Down Expand Up @@ -266,35 +259,23 @@ const GraphExplorer = () => {
: userLayout.activeSidebarItem !== null
}
>
{userLayout.activeSidebarItem === "search" && (
<SearchSidebarPanel onClose={closeSidebar} />
)}
{userLayout.activeSidebarItem === "details" && (
<EntityDetails onClose={closeSidebar} />
)}
{userLayout.activeSidebarItem === "expand" && (
<NodeExpand onClose={closeSidebar} />
)}
{userLayout.activeSidebarItem === "filters" && (
<EntitiesFilter onClose={closeSidebar} />
)}
{userLayout.activeSidebarItem === "search" && <SearchSidebarPanel />}
{userLayout.activeSidebarItem === "details" && <EntityDetails />}
{userLayout.activeSidebarItem === "expand" && <NodeExpand />}
{userLayout.activeSidebarItem === "filters" && <EntitiesFilter />}
{userLayout.activeSidebarItem === "nodes-styling" && (
<NodesStyling
onClose={closeSidebar}
customizeNodeType={customizeNodeType}
onNodeCustomize={setCustomizeNodeType}
/>
)}
{userLayout.activeSidebarItem === "edges-styling" && (
<EdgesStyling
onClose={closeSidebar}
customizeEdgeType={customizeEdgeType}
onEdgeCustomize={setCustomizeEdgeType}
/>
)}
{userLayout.activeSidebarItem === "namespaces" && (
<Namespaces onClose={closeSidebar} />
)}
{userLayout.activeSidebarItem === "namespaces" && <Namespaces />}
</Workspace.SideBar.Content>
</Workspace.SideBar>
</Workspace>
Expand Down

0 comments on commit 281ffa7

Please sign in to comment.