Skip to content

Commit e107d42

Browse files
committed
settings: accordion behavior: start closed, single open submenu
1 parent f5f0ddf commit e107d42

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/packages/frontend/account/account-page.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ import { UpgradesPage } from "./upgrades/upgrades-page";
8989

9090
export const ACCOUNT_SETTINGS_ICON_NAME: IconName = "settings";
9191

92+
function getParentMenuKey(key?: string): string | undefined {
93+
if (!key) return;
94+
const dashIndex = key.indexOf("-");
95+
if (dashIndex === -1) return;
96+
return key.slice(0, dashIndex);
97+
}
98+
9299
// Type for valid menu keys
93100
type MenuKey =
94101
| "settings"
@@ -119,7 +126,7 @@ const LOAD_ACCOUNT_INFO_TIMEOUT = 15_000;
119126
export const AccountPage: React.FC = () => {
120127
const intl = useIntl();
121128
const [hidden, setHidden] = useState(IS_MOBILE);
122-
const [openKeys, setOpenKeys] = useState<string[]>(["preferences"]);
129+
const [openKeys, setOpenKeys] = useState<string[]>([]);
123130

124131
const { width: windowWidth } = useWindowDimensions();
125132
const isWide = windowWidth > 800;
@@ -447,6 +454,33 @@ export const AccountPage: React.FC = () => {
447454

448455
const tabs = getTabs();
449456

457+
useEffect(() => {
458+
if (!active_sub_tab) return;
459+
const parentKey = getParentMenuKey(active_sub_tab);
460+
if (!parentKey) return;
461+
setOpenKeys((prevOpenKeys) =>
462+
prevOpenKeys.length === 1 && prevOpenKeys[0] === parentKey
463+
? prevOpenKeys
464+
: [parentKey],
465+
);
466+
}, [active_sub_tab]);
467+
468+
useEffect(() => {
469+
if (active_page !== "preferences") {
470+
if (active_sub_tab) {
471+
redux.getActions("account").setState({ active_sub_tab: undefined });
472+
}
473+
setOpenKeys([]);
474+
}
475+
}, [active_page, active_sub_tab]);
476+
477+
function handleOpenChange(keys: string[]) {
478+
setOpenKeys((prevOpenKeys) => {
479+
const newlyOpened = keys.find((key) => !prevOpenKeys.includes(key));
480+
return newlyOpened ? [newlyOpened] : [];
481+
});
482+
}
483+
450484
// Process tabs to handle nested children for sub-tabs
451485
const children = {};
452486
const titles = {}; // Always store full labels for renderTitle()
@@ -565,9 +599,8 @@ export const AccountPage: React.FC = () => {
565599
}}
566600
>
567601
<Menu
568-
defaultOpenKeys={["preferences"]}
569-
openKeys={hidden ? ["preferences"] : openKeys}
570-
onOpenChange={hidden ? undefined : setOpenKeys}
602+
openKeys={openKeys}
603+
onOpenChange={handleOpenChange}
571604
mode="inline"
572605
items={tabs}
573606
onClick={(e) => {

0 commit comments

Comments
 (0)