Skip to content

Bug: selectedLines={null} not respected after line selection event #433

@jonatanklosko

Description

@jonatanklosko

Prerequisites

Describe the issue

Let's say we pass hard-coded <FileDiff selectedLines={null} ... />. Then we select lines (possibly using the gutter + utility). After the selection finishes, it stays in place, even if we keep passing selectedLines={null}.

Now, the selection stays in place until the component re-renders for any other reason (some other state changing), at that point the selection is cleared.

I believe the issue is that here

useIsometricEffect(() => {
const { current: instance } = instanceRef;
if (instance == null) return;
const newOptions = mergeFileDiffOptions(options, hasGutterRenderUtility);
const forceRender = !areOptionsEqual(instance.options, newOptions);
instance.setOptions(newOptions);
void instance.render({
forceRender,
fileDiff,
oldFile,
newFile,
lineAnnotations,
});
if (selectedLines !== undefined) {
instance.setSelectedLines(selectedLines);
}
});

instance.setSelectedLines is called on ever re-render. However, the selection even by itself does not cause a re-render. That also explains why a later re-render does clear the selection.

It probably makes sense to keep the selection if no selectedLines prop is passed at all, similar to uncontrolled React inputs. However, when selectedLines is passed, I would expect it to be the source of truth, similar to controlled React inputs.

Reproduction

As described above.

What browser(s) are you seeing the problem on?

No response

What version of @pierre/diffs are you using?

1.1.4

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions