refactor: migrate the "Animation" story to TypeScript by tbouffard · Pull Request #635 · maxGraph/maxGraph · GitHub
Skip to content

refactor: migrate the "Animation" story to TypeScript#635

Merged
tbouffard merged 1 commit into
mainfrom
refactor/story_to_TS_Animation
Jan 13, 2025
Merged

refactor: migrate the "Animation" story to TypeScript#635
tbouffard merged 1 commit into
mainfrom
refactor/story_to_TS_Animation

Conversation

@tbouffard

@tbouffard tbouffard commented Jan 13, 2025

Copy link
Copy Markdown
Member

Rationale:

  • Ease the maintenance
  • Detect the errors earlier

Summary by CodeRabbit

  • Style
    • Enhanced TypeScript type safety for story template and vertex style definitions
    • Improved type declarations in animation stories
    • Updated import and type handling for CellStyle

Rationale:
  - Ease the maintenance
  - Detect the errors earlier
@tbouffard tbouffard added the refactor Code refactoring label Jan 13, 2025
@coderabbitai

coderabbitai Bot commented Jan 13, 2025

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/html/stories/Animation.stories.ts (1)

35-35: Consider using a more specific type for the template props.

Using Record<string, string> is too generic and might hide potential type errors. Consider creating a specific interface for the props.

interface TemplateProps extends Record<string, string> {
  label: string;
}

const Template = ({ label, ...args }: TemplateProps)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 49a4df1 and f59d3f9.

📒 Files selected for processing (1)
  • packages/html/stories/Animation.stories.ts (3 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/html/stories/Animation.stories.ts

[error] 82-82: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: build
  • GitHub Check: build (windows-2022)
  • GitHub Check: build (macos-14)
  • GitHub Check: build (ubuntu-22.04)
🔇 Additional comments (4)
packages/html/stories/Animation.stories.ts (4)

18-18: LGTM! Clean type import declaration.

The type-only import for CellStyle follows TypeScript best practices.


Line range hint 42-51: LGTM! Well-typed style object.

The CellStyle type annotation provides good type safety for the vertex styling properties.


77-79: LGTM! Proper boolean values for edge properties.

Converting numeric values to proper boolean types improves type safety and readability.


87-91: Consider refactoring repetitive DOM operations.

The code could be simplified to reduce repetition and improve null safety.

const path0 = state?.shape?.node.getElementsByTagName('path')[0];
const path1 = state?.shape?.node.getElementsByTagName('path')[1];

if (path0) {
  path0.removeAttribute('visibility');
  path0.setAttribute('stroke-width', '6');
  path0.setAttribute('stroke', 'lightGray');
}

path1?.setAttribute('class', 'flow');

Also, consider adding a comment explaining why the non-null assertion (!) is safe to use on e1.

✅ Verification successful

Refactoring suggestion is valid and safe to implement

The suggested refactoring will improve code readability while maintaining the same functionality. The non-null assertion on e1 is safe as the edge is created and configured immediately before use.

🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Find the context around this code
rg -B 10 -A 5 "state\?\.shape\?\.node\.getElementsByTagName\('path'\)" packages/html/stories/Animation.stories.ts

# Look for similar patterns of DOM operations on SVG paths
rg "getElementsByTagName\('path'\)" --type ts

# Find usage of graph.view.getState
rg "graph\.view\.getState" --type ts

Length of output: 5969

},
});
e1.geometry.points = [new Point(230, 50)];
e1.geometry && (e1.geometry.points = [new Point(230, 50)]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid assignment within expression.

The assignment within the condition makes the code harder to read and maintain.

-e1.geometry && (e1.geometry.points = [new Point(230, 50)]);
+if (e1.geometry) {
+  e1.geometry.points = [new Point(230, 50)];
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
🧰 Tools
🪛 Biome (1.9.4)

[error] 82-82: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Code refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant