Skip to content
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
38 changes: 38 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,16 @@
font-size: 0.9rem;
}

.import-row--actionable {
cursor: pointer;
}

.import-row--actionable:hover,
.import-row--actionable:focus-visible {
border-color: rgba(86, 132, 243, 0.32);
box-shadow: 0 0 0 2px rgba(86, 132, 243, 0.08);
}

.import-row--invalid {
background: rgba(229, 107, 85, 0.06);
border-color: rgba(229, 107, 85, 0.22);
Expand All @@ -2931,6 +2941,7 @@
display: flex;
flex-direction: column;
gap: 2px;
flex: 1;
min-width: 0;
word-break: break-all;
}
Expand All @@ -2946,6 +2957,33 @@
font-size: 0.82rem;
}

.import-row__remove {
width: 24px;
height: 24px;
border: 1px solid var(--panel-outline, rgba(0, 0, 0, 0.08));
border-radius: 999px;
background: var(--panel);
color: var(--muted);
cursor: pointer;
flex-shrink: 0;
font: inherit;
font-size: 0.8rem;
line-height: 1;
opacity: 0;
transition: opacity 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.import-row:hover .import-row__remove,
.import-row:focus-within .import-row__remove {
opacity: 1;
}

.import-row__remove:hover,
.import-row__remove:focus-visible {
border-color: rgba(229, 107, 85, 0.5);
color: #e56b55;
}

@keyframes vault-modal-in {
from {
opacity: 0;
Expand Down
137 changes: 108 additions & 29 deletions src/components/VaultImportModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ function rowStatusClass(row) {
return 'is-negative';
}

function lineBounds(text, lineNo) {
const lines = String(text || '').split('\n');
if (lineNo < 1 || lineNo > lines.length) return null;
let start = 0;
for (let i = 0; i < lineNo - 1; i += 1) {
start += lines[i].length + 1;
}
return { start, end: start + lines[lineNo - 1].length };
}

export default function VaultImportModal({ open, onClose }) {
const vault = useVault();
const { user } = useAuth();
Expand Down Expand Up @@ -117,6 +127,39 @@ export default function VaultImportModal({ open, onClose }) {
[vault.data]
);

const selectPasteLine = useCallback(
(lineNo) => {
const bounds = lineBounds(paste, lineNo);
if (!bounds || !pasteRef.current) return;
const textarea = pasteRef.current;
textarea.focus();
textarea.setSelectionRange(bounds.start, bounds.end);

const lineHeight =
parseFloat(window.getComputedStyle(textarea).lineHeight) || 20;
textarea.scrollTop = Math.max(
0,
(lineNo - 1) * lineHeight - textarea.clientHeight / 2
);
},
[paste]
);

const removePasteLine = useCallback(
(lineNo) => {
const lines = paste.split('\n');
if (lineNo < 1 || lineNo > lines.length) return;
lines.splice(lineNo - 1, 1);
const nextPaste = lines.join('\n');
applyPastePreview(nextPaste);
setPaste(nextPaste);
setTimeout(() => {
if (pasteRef.current) pasteRef.current.focus();
}, 0);
},
[applyPastePreview, paste]
);

const canSave =
!saving &&
!validating &&
Expand Down Expand Up @@ -343,36 +386,72 @@ export default function VaultImportModal({ open, onClose }) {
) : null}
</div>
<ul className="import-rows__list">
{rows.map((r) => (
<li
key={`${r.lineNo}:${r.wif}`}
className={`import-row import-row--${r.kind}`}
data-testid="vault-import-row"
>
<span
className={`status-chip ${rowStatusClass(r)} import-row__status`}
{rows.map((r) => {
const canEditSource =
r.kind === 'invalid' || r.kind === 'duplicate';
return (
<li
key={`${r.lineNo}:${r.wif}`}
className={`import-row import-row--${r.kind}${
canEditSource ? ' import-row--actionable' : ''
}`}
data-testid="vault-import-row"
onClick={
canEditSource ? () => selectPasteLine(r.lineNo) : undefined
}
onKeyDown={
canEditSource
? (e) => {
if (e.target !== e.currentTarget) return;
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
selectPasteLine(r.lineNo);
Comment thread
sidhujag marked this conversation as resolved.
}
}
: undefined
}
role={canEditSource ? 'button' : undefined}
tabIndex={canEditSource ? 0 : undefined}
>
{rowStatusLabel(r)}
</span>
<span className="import-row__detail">
{r.kind === 'valid' ? (
<>
<code className="import-row__addr">{r.address}</code>
{r.label ? (
<span className="import-row__label">
{r.label}
</span>
) : null}
</>
) : (
<code className="import-row__addr">
Line {r.lineNo}
{r.label ? ` — ${r.label}` : ''}
</code>
)}
</span>
</li>
))}
<span
className={`status-chip ${rowStatusClass(r)} import-row__status`}
>
{rowStatusLabel(r)}
</span>
<span className="import-row__detail">
{r.kind === 'valid' ? (
<>
<code className="import-row__addr">{r.address}</code>
{r.label ? (
<span className="import-row__label">
{r.label}
</span>
) : null}
</>
) : (
<code className="import-row__addr">
Line {r.lineNo}
{r.label ? ` — ${r.label}` : ''}
</code>
)}
</span>
{canEditSource ? (
<button
type="button"
className="import-row__remove"
aria-label={`Remove line ${r.lineNo}`}
title={`Remove line ${r.lineNo}`}
onClick={(e) => {
e.stopPropagation();
removePasteLine(r.lineNo);
}}
>
x
</button>
) : null}
</li>
);
})}
</ul>
</div>
) : null}
Expand Down
48 changes: 48 additions & 0 deletions src/components/VaultImportModal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,54 @@ describe('VaultImportModal — paste → validate', () => {
// Nothing is importable — Save remains disabled.
expect(screen.getByTestId('vault-import-save')).toBeDisabled();
});

test('clicking an invalid row selects the offending textarea line', async () => {
const vault = unlockedVault();
mount({ vault });
const textarea = screen.getByTestId('vault-import-paste');
const text = `${VALID_WIF_1},MN 1\n${INVALID_WIF},bad row`;
pasteInto(textarea, text);
await waitForValidationDone();

const invalidRow = screen.getAllByTestId('vault-import-row')[1];
await userEvent.click(invalidRow);

const lineStart = text.indexOf(INVALID_WIF);
expect(textarea.selectionStart).toBe(lineStart);
expect(textarea.selectionEnd).toBe(text.length);
});

test('remove line action deletes the offending row from the paste', async () => {
const vault = unlockedVault();
mount({ vault });
const textarea = screen.getByTestId('vault-import-paste');
pasteInto(textarea, `${VALID_WIF_1},MN 1\n${INVALID_WIF},bad row`);
await waitForValidationDone();

await userEvent.click(screen.getByLabelText('Remove line 2'));

expect(textarea).toHaveValue(`${VALID_WIF_1},MN 1`);
await waitForValidationDone();
expect(screen.getAllByTestId('vault-import-row')).toHaveLength(1);
expect(screen.getByTestId('vault-import-save')).not.toBeDisabled();
});

test('row keyboard shortcut ignores key events from the remove button', async () => {
const vault = unlockedVault();
mount({ vault });
const textarea = screen.getByTestId('vault-import-paste');
const text = `${VALID_WIF_1},MN 1\n${INVALID_WIF},bad row`;
pasteInto(textarea, text);
await waitForValidationDone();

const removeButton = screen.getByLabelText('Remove line 2');
removeButton.focus();
fireEvent.keyDown(removeButton, { key: ' ' });

expect(document.activeElement).toBe(removeButton);
expect(textarea.selectionStart).toBe(text.length);
expect(textarea.selectionEnd).toBe(text.length);
});
});

describe('VaultImportModal — save flow (UNLOCKED)', () => {
Expand Down
Loading