A local-first personal finance app
[AI] Fix sidebar rename input closing on arrow keys (#9219)
Junyuan Zhengb26aef9ba2a7
diff --git a/packages/desktop-client/src/components/sidebar/redesign/AccountGroupHeader.tsx b/packages/desktop-client/src/components/sidebar/redesign/AccountGroupHeader.tsxindex 6787717d42..14c5fb82c7 100644--- a/packages/desktop-client/src/components/sidebar/redesign/AccountGroupHeader.tsx+++ b/packages/desktop-client/src/components/sidebar/redesign/AccountGroupHeader.tsx@@ -36,6 +36,7 @@ import { DragHandle } from './DragHandle';import { SidebarBalance } from './SidebarBalance';import { dropZoneStyle, groupLabelStyle } from './styles';import { SyncErrorRollup } from './SyncErrorRollup';+import { useTreeItemEditing } from './treeItemEditing';import { treeKeys } from './treeKeys';type AccountGroupHeaderProps = {@@ -57,7 +58,9 @@ export function AccountGroupHeader({}: AccountGroupHeaderProps) {const { t } = useTranslation();const dispatch = useDispatch();- const [isEditing, setIsEditing] = useState(false);+ const [isEditing, setIsEditing] = useTreeItemEditing(+ treeKeys.group(group.id),+ );const updateGroup = useUpdateAccountGroupMutation();const deleteGroup = useDeleteAccountGroupMutation();diff --git a/packages/desktop-client/src/components/sidebar/redesign/AccountRow.tsx b/packages/desktop-client/src/components/sidebar/redesign/AccountRow.tsxindex 6c4b5353cd..cb722de4dd 100644--- a/packages/desktop-client/src/components/sidebar/redesign/AccountRow.tsx+++ b/packages/desktop-client/src/components/sidebar/redesign/AccountRow.tsx@@ -27,6 +27,7 @@ import { DragHandle } from './DragHandle';import { SidebarBalance } from './SidebarBalance';import { SyncDot, useSyncDotLabel } from './SyncDot';import type { SyncDotStatus } from './SyncDot';+import { useTreeItemEditing } from './treeItemEditing';import { treeKeys } from './treeKeys';type AccountRowProps = {@@ -46,7 +47,9 @@ export function AccountRow({const syncingAccountIds = useSelector(state => state.account.accountsSyncing);const updatedAccounts = useUpdatedAccounts();const isUpdated = !isClosed && updatedAccounts.includes(account.id);- const [isEditing, setIsEditing] = useState(false);+ const [isEditing, setIsEditing] = useTreeItemEditing(+ treeKeys.account(account.id),+ );const href = `/accounts/${account.id}`;const isActive = location.pathname === href;diff --git a/packages/desktop-client/src/components/sidebar/redesign/AccountTree.tsx b/packages/desktop-client/src/components/sidebar/redesign/AccountTree.tsxindex 634c67820a..0f4481d018 100644--- a/packages/desktop-client/src/components/sidebar/redesign/AccountTree.tsx+++ b/packages/desktop-client/src/components/sidebar/redesign/AccountTree.tsx@@ -1,3 +1,4 @@+import { useState } from 'react';import { Tree } from 'react-aria-components';import type { Key } from 'react-aria-components';@@ -5,6 +6,7 @@ import { css } from '@emotion/css';import { AccountGroupHeader } from './AccountGroupHeader';import { AccountRow } from './AccountRow';+import { TreeItemEditingContext } from './treeItemEditing';import { treeKeys } from './treeKeys';import { useAccountTreeDragAndDrop } from './useAccountTreeDragAndDrop';import type { AccountTreeSide } from './useAccountTreeDragAndDrop';@@ -31,6 +33,7 @@ export function AccountTree({}: AccountTreeProps) {const { dragAndDropHooks, isGroupDropZoneActive, isDraggingGroup } =useAccountTreeDragAndDrop({ side, buckets, isDisabled: isDragDisabled });+ const [editingKey, setEditingKey] = useState<Key | null>(null);const groupBuckets = buckets.filter(bucket => bucket.group != null);const expandedKeys = new Set<Key>(@@ -49,36 +52,41 @@ export function AccountTree({};return (- <Tree- aria-label={label}- selectionMode="none"- expandedKeys={expandedKeys}- onExpandedChange={onExpandedChange}- dragAndDropHooks={dragAndDropHooks}- className={css({ outline: 'none' })}- >- {buckets.map(bucket =>- bucket.group == null ? (- bucket.accounts.map(account => (- <AccountRow- key={treeKeys.account(account.id)}- account={account}- isClosed={side === 'closed'}+ <TreeItemEditingContext.Provider value={{ editingKey, setEditingKey }}>+ <Tree+ aria-label={label}+ selectionMode="none"+ // In "arrow" mode, rows capture Left/Right to move focus between their+ // children, which pulls focus out of the rename input and ends editing.+ keyboardNavigationBehavior={editingKey == null ? 'arrow' : 'tab'}+ expandedKeys={expandedKeys}+ onExpandedChange={onExpandedChange}+ dragAndDropHooks={dragAndDropHooks}+ className={css({ outline: 'none' })}+ >+ {buckets.map(bucket =>+ bucket.group == null ? (+ bucket.accounts.map(account => (+ <AccountRow+ key={treeKeys.account(account.id)}+ account={account}+ isClosed={side === 'closed'}+ showSyncDot={showSyncDot}+ />+ ))+ ) : (+ <AccountGroupHeader+ key={treeKeys.group(bucket.group.id)}+ group={bucket.group}+ side={side === 'off' ? 'off' : 'on'}+ accounts={bucket.accounts}+ failedCount={bucket.failedCount}showSyncDot={showSyncDot}+ isDropZoneActive={isGroupDropZoneActive(bucket.group.id)}/>- ))- ) : (- <AccountGroupHeader- key={treeKeys.group(bucket.group.id)}- group={bucket.group}- side={side === 'off' ? 'off' : 'on'}- accounts={bucket.accounts}- failedCount={bucket.failedCount}- showSyncDot={showSyncDot}- isDropZoneActive={isGroupDropZoneActive(bucket.group.id)}- />- ),- )}- </Tree>+ ),+ )}+ </Tree>+ </TreeItemEditingContext.Provider>);}diff --git a/packages/desktop-client/src/components/sidebar/redesign/treeItemEditing.ts b/packages/desktop-client/src/components/sidebar/redesign/treeItemEditing.tsnew file mode 100644index 0000000000..f67ba97c4e--- /dev/null+++ b/packages/desktop-client/src/components/sidebar/redesign/treeItemEditing.ts@@ -0,0 +1,31 @@+import { createContext, useContext } from 'react';+import type { Dispatch, SetStateAction } from 'react';+import type { Key } from 'react-aria-components';++type TreeItemEditingContextValue = {+ editingKey: Key | null;+ setEditingKey: Dispatch<SetStateAction<Key | null>>;+};++export const TreeItemEditingContext =+ createContext<TreeItemEditingContextValue | null>(null);++export function useTreeItemEditing(key: Key) {+ const context = useContext(TreeItemEditingContext);+ if (!context) {+ throw new Error(+ 'useTreeItemEditing must be used within a TreeItemEditingContext',+ );+ }+ const { editingKey, setEditingKey } = context;++ const setIsEditing = (isEditing: boolean) =>+ setEditingKey(current => {+ if (isEditing) {+ return key;+ }+ return current === key ? null : current;+ });++ return [editingKey === key, setIsEditing] as const;+}diff --git a/upcoming-release-notes/fix-sidebar-rename-arrow-keys.md b/upcoming-release-notes/fix-sidebar-rename-arrow-keys.mdnew file mode 100644index 0000000000..5793894650--- /dev/null+++ b/upcoming-release-notes/fix-sidebar-rename-arrow-keys.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [junyuanz1]+---++Fix renaming an account or group in the redesigned sidebar closing when pressing the arrow keys