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 off-by-one error in importer error messages when first row is header #2339

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

niklasva82
Copy link
Member

@niklasva82 niklasva82 commented Nov 9, 2024

Description

This PR fixes the off-by-one error in the importer error messages when the first row of the imported document is a header.

Screenshots

image

Changes

  • Changes ImportRowProblem and ImportFieldProblem types to also include an array rows in addition to indices.
  • Changes usePreflight to map indices to rows, +1 or +2 depending on whether the first row is a header.
  • Renames the rowIndices property to rowNumbers in ImportMessage in order to reflect the fact that these are row numbers and not merely indices of the array.
  • Passes problem.rows instead of problem.indices to ImportMessage in ImportMessageItem
  • Removes mapping of indices to rows from ImportMessage

Related issues

Resolves #2325

Copy link
Member

@richardolsson richardolsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I can tell that you've explored the code to find the places to make these changes, so I'm curious about your reasoning. I can think of several places to fix this bug. Either in the UI, in the hook, or in the tested pure functions behind it. You've chosen the hook, and I would love to hear more about that. Some more concrete questions are in the comments below.

@@ -29,6 +30,7 @@ export type ImportRowProblem = {
| ImportProblemKind.UNEXPECTED_ERROR
| ImportProblemKind.UNKNOWN_PERSON
| ImportProblemKind.UNKNOWN_ERROR;
rows?: number[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benefit of adding this as a second property, over just rendering it +1 or +2 in the UI?

status,
title,
}) => {
const messages = useMessages(messageIds);

const rowNumbers = rowIndices?.map((rowIndex) => rowIndex + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the downsides of just adding logic to either add +1 or +2 depending on firstLineIsHeaders here?

Comment on lines +62 to +69
const rowModifier = sheet.firstRowIsHeaders ? 2 : 1;
problems.forEach((problem) => {
if ('indices' in problem) {
problem.rows = problem.indices.map((index) => index + rowModifier);
}
return problem;
});

Copy link
Member

@richardolsson richardolsson Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is quite "far back" in the stack of responsibilities, but still not so far back that it can be tested by any of our existing tests. It also relies on runtime type-checking logic which to me gives off just a little bit of code smell.

I'm curious about why you decided to put the logic here, and not further back (predictProblems(), problemsFromPreview()) where it can be tested, or in the UI (<ImportMessageItem>) where I think that it could probably be simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Imports: Wrong row indicated in error messages
2 participants