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 all 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
17 changes: 17 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,
useEndpointGroupListGlobalState,
} from '~/store';
import {
Endpoint,
Expand Down Expand Up @@ -922,3 +923,19 @@ export const useEndpoint = (): UseEndpointReturn => {
);
return ret;
};

export const useEndpointGroupToggle = (id: string) => {
const [endpointGroups, setEndpointGroups] = useEndpointGroupListGlobalState();
const endpointGroup = endpointGroups.find((group) => group.id === id);
const isOpen = endpointGroup?.isOpen ?? false;

const toggle = () => {
setEndpointGroups((groups) => {
return groups.map((group) => {
return group.id === id ? { ...group, isOpen: !isOpen } : group;
});
});
};

return { isOpen, toggle };
};
13 changes: 5 additions & 8 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,19 +143,16 @@ type GroupProps = {
list: Endpoint[];
};
const Group: React.FC<GroupProps> = ({ group, list }) => {
const [isOpened, setIsOpened] = useState<boolean>(!!list.length);
const handleToggleClick = () => {
setIsOpened((currVal) => !currVal);
};
const { isOpen, toggle } = useEndpointGroupToggle(group.id);

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

return (
<div>
{/* Head */}
<button
className="flex gap-1 w-full hover:bg-thm-on-background-faint py-2 rounded"
onClick={handleToggleClick}
onClick={toggle}
>
<span className="p-0.5">
<ToggleIcon className="w-4 h-4" />
Expand All @@ -177,7 +174,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: !isOpen,
}
)}
>
Expand Down
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;
isOpen?: boolean;
};

export type EndpointID = string;
Expand Down