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(app): retains group toggle open status #790

Merged
merged 7 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/twelve-ducks-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@viron/app": minor
---

retains group toggle open status
26 changes: 26 additions & 0 deletions packages/app/src/hooks/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
useEndpointListUngroupedGlobalStateValue,
useEndpointGroupListGlobalStateSet,
useEndpointGroupListSortedGlobalStateValue,
useEndpointGroupToggleState,
} from '~/store';
import {
Endpoint,
Expand Down Expand Up @@ -922,3 +923,28 @@ export const useEndpoint = (): UseEndpointReturn => {
);
return ret;
};

export const useEndpointGroupToggle = (id: string) => {
const [endpointGroups, setEndpointGroups] = useEndpointGroupToggleState();

const isOpenToggle =
endpointGroups.find((group) => group.id === id)?.isOpenToggle ?? false;

const setIsOpenToggle = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Functions that start with "set" are typically considered as setters for private properties. Nonetheless, the absence of arguments in such a function could make its behavior unpredictable.

setEndpointGroups((groups) => {
const index = groups.findIndex((group) => group.id === id);

if (index === -1) {
return groups;
}

return groups.map((group, i) =>
i === index
? { ...group, isOpenToggle: group.isOpenToggle !== true }
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps just using "!" operator would be sufficient to evaluate it.

isOpenToggle: !group.isOpenToggle

: group
);
});
};

return { isOpenToggle, setIsOpenToggle };
};
10 changes: 5 additions & 5 deletions packages/app/src/pages/dashboard/endpoints/_/body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Head from '~/components/head';
import ChevronDownIcon from '~/components/icon/chevronDown/outline';
import ChevronRightIcon from '~/components/icon/chevronRight/outline';
import PlusIcon from '~/components/icon/plus/outline';
import { useEndpoint } from '~/hooks/endpoint';
import { useEndpoint, useEndpointGroupToggle } from '~/hooks/endpoint';
import { Trans, useTranslation } from '~/hooks/i18n';
import { Props as LayoutProps } from '~/layouts/';
import Modal, { useModal } from '~/portals/modal';
Expand Down Expand Up @@ -143,12 +143,12 @@ type GroupProps = {
list: Endpoint[];
};
const Group: React.FC<GroupProps> = ({ group, list }) => {
const [isOpened, setIsOpened] = useState<boolean>(!!list.length);
const { isOpenToggle, setIsOpenToggle } = useEndpointGroupToggle(group.id);
const handleToggleClick = () => {
setIsOpened((currVal) => !currVal);
setIsOpenToggle();
};

const ToggleIcon = isOpened ? ChevronDownIcon : ChevronRightIcon;
const ToggleIcon = isOpenToggle ? ChevronDownIcon : ChevronRightIcon;

return (
<div>
Expand Down Expand Up @@ -177,7 +177,7 @@ const Group: React.FC<GroupProps> = ({ group, list }) => {
className={classnames(
'grid grid-cols-1 @[740px]:grid-cols-2 @[995px]:grid-cols-3 gap-6 mt-2 py-2',
{
hidden: !isOpened,
hidden: !isOpenToggle,
}
)}
>
Expand Down
3 changes: 3 additions & 0 deletions packages/app/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ export const useEndpointGroupListItemGlobalStateValue = (
export const useEndpointGroupListItemGlobalStateSet = (
params: Parameters<typeof endpointGroupListItemSelector>[0]
) => useGlobalStateSet(endpointGroupListItemSelector(params));

export const useEndpointGroupToggleState = () =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you change this to useEndpointGroupListGlobalState?

useGlobalState(endpointGroupListAtom);
1 change: 1 addition & 0 deletions packages/app/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type EndpointGroup = {
name: string;
description?: string;
priority?: number;
isOpenToggle?: boolean;
};

export type EndpointID = string;
Expand Down