Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(settings): focus the name field when the API key and new tag dialogs open (#7804)

teo62e3e7838c4c
diff --git a/apps/web/src/features/property/tags/TagEditorDialog.test.tsx b/apps/web/src/features/property/tags/TagEditorDialog.test.tsx
new file mode 100644
index 0000000000..77044c93eb
--- /dev/null
+++ b/apps/web/src/features/property/tags/TagEditorDialog.test.tsx
@@ -0,0 +1,99 @@
+/**
+ * @vitest-environment jsdom
+ */
+
+import type { PropertyOptionResponse } from '@service-properties/generated/schemas/propertyOptionResponse';
+import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
+import { createSignal } from 'solid-js';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { TagEditorDialog, type TagEditorDialogMode } from './TagEditorDialog';
+
+vi.mock('@core/mobile/isMobile', () => ({ isMobile: () => false }));
+vi.mock('@queries/properties/tags', () => {
+ const mutation = () => ({ isPending: false, mutate: vi.fn() });
+ return {
+ useCreateTagMutation: mutation,
+ useDeletePropertyOptionMutation: mutation,
+ useUpdatePropertyOptionMutation: mutation,
+ };
+});
+
+let motionStyles: HTMLStyleElement;
+beforeEach(() => {
+ motionStyles = document.createElement('style');
+ // jsdom never fires animation events, so presence would keep closed dialogs mounted.
+ motionStyles.textContent =
+ '* { transition-duration: 0s; animation-name: none; }';
+ document.head.append(motionStyles);
+ vi.stubGlobal(
+ 'ResizeObserver',
+ class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ }
+ );
+});
+
+afterEach(() => {
+ cleanup();
+ motionStyles.remove();
+ vi.unstubAllGlobals();
+});
+
+function renderEditor(mode: TagEditorDialogMode) {
+ render(() => <TagEditorDialog open mode={mode} onClose={() => {}} />);
+ return screen.findByPlaceholderText('Tag name');
+}
+
+describe('TagEditorDialog', () => {
+ it('focuses the name field when creating a tag', async () => {
+ const name = await renderEditor({ type: 'create', initialScope: 'user' });
+ await vi.waitFor(() => expect(document.activeElement).toBe(name));
+ });
+
+ it('focuses the name field again when the create dialog reopens', async () => {
+ const [mode, setMode] = createSignal<TagEditorDialogMode | null>(null);
+ const create: TagEditorDialogMode = {
+ type: 'create',
+ initialScope: 'user',
+ };
+ render(() => (
+ <TagEditorDialog
+ open={mode() !== null}
+ mode={mode()}
+ onClose={() => setMode(null)}
+ />
+ ));
+
+ for (let attempt = 0; attempt < 2; attempt++) {
+ setMode(create);
+ const name = await screen.findByPlaceholderText('Tag name');
+ await vi.waitFor(() => expect(document.activeElement).toBe(name));
+ fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
+ await vi.waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
+ }
+ });
+
+ it('leaves the name field unfocused when editing a tag', async () => {
+ const name = await renderEditor({
+ type: 'edit',
+ tag: {
+ scope: 'user',
+ propertyDefinitionId: 'definition',
+ option: {
+ id: 'urgent',
+ propertyDefinitionId: 'definition',
+ displayOrder: 0,
+ value: { type: 'string', value: 'Urgent' },
+ } as PropertyOptionResponse,
+ },
+ });
+ await vi.waitFor(() =>
+ expect(screen.getByRole('dialog').contains(document.activeElement)).toBe(
+ true
+ )
+ );
+ expect(document.activeElement).not.toBe(name);
+ });
+});
diff --git a/apps/web/src/features/property/tags/TagEditorDialog.tsx b/apps/web/src/features/property/tags/TagEditorDialog.tsx
index 58b87b8324..5c25594fc1 100644
--- a/apps/web/src/features/property/tags/TagEditorDialog.tsx
+++ b/apps/web/src/features/property/tags/TagEditorDialog.tsx
@@ -91,12 +91,6 @@ export function TagEditorDialog(props: {
? 'user'
: initialScope(mode)
);
-
- if (mode.type === 'create') {
- requestAnimationFrame(() => {
- nameInputRef?.focus();
- });
- }
});
const title = () =>
@@ -183,6 +177,11 @@ export function TagEditorDialog(props: {
open={props.open}
onOpenChange={(open) => !open && close()}
onCloseAutoFocus={props.onCloseAutoFocus}
+ onOpenAutoFocus={(event) => {
+ if (props.mode?.type !== 'create') return;
+ event.preventDefault();
+ nameInputRef?.focus();
+ }}
>
<CommandMenuShell depth={2} class="text-sm" onKeyDown={handleKeyDown}>
<CommandMenuShell.Header class="my-0 gap-3 border-b-0">
@@ -210,7 +209,6 @@ export function TagEditorDialog(props: {
<EditorRow label="Name">
<input
ref={nameInputRef}
- autofocus={props.mode?.type === 'create'}
value={label()}
onInput={(event) => setLabel(event.currentTarget.value)}
onKeyDown={(event) => {
diff --git a/apps/web/src/features/settings/ApiKeys.test.tsx b/apps/web/src/features/settings/ApiKeys.test.tsx
new file mode 100644
index 0000000000..d3091d37d9
--- /dev/null
+++ b/apps/web/src/features/settings/ApiKeys.test.tsx
@@ -0,0 +1,71 @@
+/**
+ * @vitest-environment jsdom
+ */
+
+import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { ApiKeys } from './ApiKeys';
+
+vi.mock('@core/mobile/isMobile', () => ({ isMobile: () => false }));
+vi.mock('@core/component/Toast/Toast', () => ({
+ toast: { success: vi.fn(), failure: vi.fn() },
+}));
+vi.mock('@entity', () => ({ formatRelativeTimestamp: () => '' }));
+vi.mock('@queries/user-api-keys/user-api-keys', () => ({
+ useUserApiKeysQuery: () => ({ isLoading: false, isError: false, data: [] }),
+ useCreateUserApiKeyMutation: () => ({
+ isPending: false,
+ mutateAsync: vi.fn(),
+ }),
+ useDeleteUserApiKeyMutation: () => ({
+ isPending: false,
+ mutateAsync: vi.fn(),
+ }),
+}));
+
+let motionStyles: HTMLStyleElement;
+beforeEach(() => {
+ motionStyles = document.createElement('style');
+ // jsdom never fires animation events, so presence would keep closed dialogs mounted.
+ motionStyles.textContent =
+ '* { transition-duration: 0s; animation-name: none; }';
+ document.head.append(motionStyles);
+ vi.stubGlobal(
+ 'ResizeObserver',
+ class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ }
+ );
+});
+
+afterEach(() => {
+ cleanup();
+ motionStyles.remove();
+ vi.unstubAllGlobals();
+});
+
+describe('ApiKeys', () => {
+ it('focuses the name field each time the create dialog opens', async () => {
+ render(() => <ApiKeys />);
+
+ const [createButton] = screen.getAllByRole('button', {
+ name: 'Create key',
+ });
+ createButton.focus();
+ fireEvent.click(createButton);
+
+ const name = await screen.findByPlaceholderText('e.g. CI, local scripts');
+ await vi.waitFor(() => expect(document.activeElement).toBe(name));
+
+ fireEvent.click(screen.getByRole('button', { name: 'Close' }));
+ await vi.waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
+ fireEvent.click(createButton);
+
+ const reopenedName = await screen.findByPlaceholderText(
+ 'e.g. CI, local scripts'
+ );
+ await vi.waitFor(() => expect(document.activeElement).toBe(reopenedName));
+ });
+});
diff --git a/apps/web/src/features/settings/ApiKeys.tsx b/apps/web/src/features/settings/ApiKeys.tsx
index f4450533dc..d68e8094e0 100644
--- a/apps/web/src/features/settings/ApiKeys.tsx
+++ b/apps/web/src/features/settings/ApiKeys.tsx
@@ -227,6 +227,7 @@ function CreateApiKeyDialog(props: {
const [name, setName] = createSignal('');
const [created, setCreated] = createSignal<CreatedUserApiKey>();
const [nameError, setNameError] = createSignal<string>();
+ let nameInputRef: HTMLInputElement | undefined;
const reset = () => {
setName('');
@@ -257,6 +258,10 @@ function CreateApiKeyDialog(props: {
open={props.open}
onOpenChange={(open) => (open ? undefined : close())}
onEscapeKeyDown={(event) => props.pending && event.preventDefault()}
+ onOpenAutoFocus={(event) => {
+ event.preventDefault();
+ nameInputRef?.focus();
+ }}
position="center"
class="w-105"
>
@@ -286,7 +291,7 @@ function CreateApiKeyDialog(props: {
<label class="flex flex-col gap-1.5">
<span class="text-xs font-medium text-ink">Name</span>
<input
- autofocus
+ ref={nameInputRef}
value={name()}
placeholder="e.g. CI, local scripts"
class="settings-input w-full"