Skip to content

Fix unmodeled methods always being marked as unsaved#2969

Merged
koesie10 merged 2 commits intomainfrom
koesie10/fix-unmodeled-unsaved
Oct 16, 2023
Merged

Fix unmodeled methods always being marked as unsaved#2969
koesie10 merged 2 commits intomainfrom
koesie10/fix-unmodeled-unsaved

Conversation

@koesie10
Copy link
Copy Markdown
Member

When opening a library group in the model editor, unmodeled methods would always be marked as unsaved, even if there were no changes. This was because the ModelKindDropdown component did not properly take into account that the kind for an unmodeled method should be an empty string. It would always try setting it to undefined, which would cause the method to be marked as unsaved. This fixes it by checking if there are valid kinds before setting the kind to the first one.

Checklist

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • [Maintainers only] If this pull request makes user-facing changes that require documentation changes, open a corresponding docs pull request in the github/codeql repo and add the ready-for-doc-review label there.

When opening a library group in the model editor, unmodeled methods
would always be marked as unsaved, even if there were no changes. This
was because the `ModelKindDropdown` component did not properly take into
account that the `kind` for an unmodeled method should be an empty
string. It would always try setting it to `undefined`, which would cause
the method to be marked as unsaved. This fixes it by checking if there
are valid kinds before setting the kind to the first one.
@koesie10 koesie10 marked this pull request as ready for review October 13, 2023 12:14
@koesie10 koesie10 requested a review from a team as a code owner October 13, 2023 12:14

if (kinds.length === 0) {
if (value !== "") {
onChangeKind("");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've tested it and this PR works. It took me a little while to understand why.

What confused me is that in onChangeKind it aborts and does nothing if modeledMethod === undefined, so in the case of there being no modeled method, this PR shouldn't be a change in behaviour. And in the case where there is a modeled method but type === "none", then the according the typing, kind should never be undefined.

But the issue isn't with value being undefined to start with, and instead it was in value !== undefined && !kinds.includes(value) where the kinds array would be empty so we'd call onChangeKind(undefined).

This PR fixes things by adding a check for kinds.length === 0 earlier.

Looking into this made me realise that we no longer call these inputs with modeledMethod: undefined in the model editor panel. In MethodRow we always construct a dummy modeled method with type: "none".

@@ -63,7 +63,16 @@ export const ModelKindDropdown = ({

useEffect(() => {
const value = modeledMethod?.kind;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I find the logic in this method a little confusing because of multiple if statements in sequence where some return early and some don't. I think we could also perhaps make value never be undefined and that could simplify the logic.

What do you think of:

const value = modeledMethod?.kind ?? "";

if (kinds.length === 0 && value !== "") {
  onChangeKind("");
} else if (kinds.length > 0 && !kinds.includes(value)) {
  onChangeKind(kinds[0]);
}

Copy link
Copy Markdown
Contributor

@robertbrignull robertbrignull left a comment

Choose a reason for hiding this comment

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

👍🏼

@koesie10 koesie10 merged commit fca68ed into main Oct 16, 2023
@koesie10 koesie10 deleted the koesie10/fix-unmodeled-unsaved branch October 16, 2023 13:07
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.

2 participants