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

enhance(apps/frontend-manage): configure slate editor correctly and introduce typing #4324

Merged
merged 2 commits into from
Oct 20, 2024

Conversation

sjschlapbach
Copy link
Member

@sjschlapbach sjschlapbach commented Oct 20, 2024

Define custom Slate types according to documentation: https://docs.slatejs.org/concepts/12-typescript

Summary by CodeRabbit

  • New Features

    • Enhanced editor functionality with improved type definitions for custom elements and text, ensuring a more structured content representation.
    • Updated hotkey handling to enforce valid formatting types.
  • Bug Fixes

    • Improved type safety in editor functions, reducing potential runtime errors.
  • Documentation

    • Clarified prop types for BlockButton and MarkButton components, enhancing usability for developers.

Copy link

aviator-app bot commented Oct 20, 2024

Current Aviator status

Aviator will automatically update this comment as the status of the PR changes.
Comment /aviator refresh to force Aviator to re-examine your PR (or learn about other /aviator commands).

This PR was merged manually (without Aviator). Merging manually can negatively impact the performance of the queue. Consider using Aviator next time.


See the real-time status of this PR on the Aviator webapp.
Use the Aviator Chrome Extension to see the status of your PR within GitHub.

Copy link

coderabbitai bot commented Oct 20, 2024

Caution

Review failed

Failed to post review comments.

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8c2dcd1 and 0e0fdf9.

📒 Files selected for processing (1)
  • apps/frontend-manage/src/components/common/ContentInput.tsx (13 hunks)
🧰 Additional context used
🔇 Additional comments (1)
apps/frontend-manage/src/components/common/ContentInput.tsx (1)

15-18: Validate import paths for 'convertToMd' and 'convertToSlate'

Ensure that the import path '@klicker-uzh/shared-components/src/utils/slateMdConversion' is correct and that these utilities are exported properly. Incorrect paths can lead to module resolution errors.

🛑 Comments failed to post (5)
apps/frontend-manage/src/components/common/ContentInput.tsx (5)

42-79: 🛠️ Refactor suggestion

Consider moving type definitions to a separate file for better maintainability

Defining the custom Slate type definitions within the component file can make the file lengthy and harder to maintain. Moving these type definitions to a separate file (e.g., ContentInput.types.ts) can improve code organization and reusability across the project.


438-442: ⚠️ Potential issue

Replace 'any' with specific types in 'ElementProps'

Using any reduces type safety. Consider replacing attributes: any with the appropriate type from slate-react.

Apply this diff to improve type safety:

+ import { RenderElementProps } from 'slate-react';

  interface ElementProps {
-   attributes: any
+   attributes: RenderElementProps['attributes']
    children: ReactNode
    element: CustomElement
  }
📝 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.

import { RenderElementProps } from 'slate-react';

interface ElementProps {
  attributes: RenderElementProps['attributes']
  children: ReactNode
  element: CustomElement
}

467-471: ⚠️ Potential issue

Replace 'any' with specific types in 'LeafProps'

Using any reduces type safety. Consider replacing attributes: any with the appropriate type from slate-react.

Apply this diff to improve type safety:

+ import { RenderLeafProps } from 'slate-react';

  interface LeafProps {
-   attributes: any
+   attributes: RenderLeafProps['attributes']
    children: ReactNode
    leaf: CustomText
  }
📝 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.

import { RenderLeafProps } from 'slate-react';

interface LeafProps {
  attributes: RenderLeafProps['attributes']
  children: ReactNode
  leaf: CustomText
}

550-569: 🛠️ Refactor suggestion

Consider using 'ComponentPropsWithoutRef' for 'SlateButton' props

To make the component more generic and type-safe, you can use ComponentPropsWithoutRef<'span'> to inherit all valid span attributes.

Apply this diff to simplify prop types:

+ import { ComponentPropsWithoutRef } from 'react';

  export const SlateButton = React.forwardRef<
    HTMLSpanElement,
-   PropsWithChildren<{
-     active: boolean
-     reversed?: boolean
-     className?: string
-     editor?: BaseEditor & ReactEditor & HistoryEditor
-     format?: string
-     onClick?: React.MouseEventHandler<HTMLSpanElement>
-   }>
+   PropsWithChildren<ComponentPropsWithoutRef<'span'> & {
+     active: boolean
+     reversed?: boolean
+   }>
  >(({ className, active, reversed, ...props }, ref) => (
    <span
      {...props}
      className={twMerge(
        className,
        'my-auto flex h-7 w-7 cursor-pointer items-center justify-center rounded',
        active && !reversed && 'bg-uzh-grey-40',
        !active && reversed && 'bg-uzh-grey-40'
      )}
      ref={ref}
    />
  ))

Committable suggestion was skipped due to low confidence.


550-569: ⚠️ Potential issue

Explicitly define all expected props in 'SlateButton'

The SlateButton component receives props like editor, format, and onClick, but these are not specified in its prop types. Using [key: string]: any can mask missing props and reduce type safety. Consider explicitly defining all expected props or extending the props from a base type.

Apply this diff to enhance type safety:

  export const SlateButton = React.forwardRef<
    HTMLSpanElement,
-   PropsWithChildren<{
+   React.HTMLAttributes<HTMLSpanElement> & {
      active: boolean
-     reversed: boolean
-     className: string
-     [key: string]: any
+     reversed?: boolean
+     className?: string
+     editor?: BaseEditor & ReactEditor & HistoryEditor
+     format?: string
+     onClick?: React.MouseEventHandler<HTMLSpanElement>
    }
- >
+ >
  >(({ className, active, reversed, ...props }, ref) => (
    <span
      {...props}
      className={twMerge(
        className,
        'my-auto flex h-7 w-7 cursor-pointer items-center justify-center rounded',
        active && !reversed && 'bg-uzh-grey-40',
        !active && reversed && 'bg-uzh-grey-40'
      )}
      ref={ref}
    />
  ))
📝 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.

export const SlateButton = React.forwardRef<
  HTMLSpanElement,
  React.HTMLAttributes<HTMLSpanElement> & {
    active: boolean
    reversed?: boolean
    className?: string
    editor?: BaseEditor & ReactEditor & HistoryEditor
    format?: string
    onClick?: React.MouseEventHandler<HTMLSpanElement>
  }
>(({ className, active, reversed, ...props }, ref) => (
  <span
    {...props}
    className={twMerge(
      className,
      'my-auto flex h-7 w-7 cursor-pointer items-center justify-center rounded',
      active && !reversed && 'bg-uzh-grey-40',
      !active && reversed && 'bg-uzh-grey-40'
    )}
    ref={ref}
  />
))
📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces significant enhancements to the ContentInput.tsx component, focusing on type definitions and the handling of editor functionality. New type definitions are added for various custom elements and text types, which are integrated into the slate module. The HOTKEYS constant's type is refined, and several functions, including toggleBlock and toggleMark, are updated to accept more specific types. Additionally, the props for BlockButton, MarkButton, and SlateButton components are clarified, ensuring better type enforcement throughout the editor's functionality.

Changes

File Path Change Summary
apps/frontend-manage/src/components/common/ContentInput.tsx - Added types: CustomEditor, ParagraphElement, ListItemElement, BlockElement, CustomText, CustomElement, CustomElementTypes
- Updated HOTKEYS type to Record<string, FormatType>
- Updated function signatures for toggleBlock, toggleMark, BlockButton, MarkButton, and SlateButton to use specific types instead of generic string or any

Possibly related PRs

  • chore: upgrade slate editor #4323: The changes in this PR involve upgrading the slate, slate-history, and slate-react packages, which are directly related to the enhancements made in the ContentInput.tsx component regarding type definitions and editor functionality.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

sonarcloud bot commented Oct 20, 2024

@sjschlapbach sjschlapbach changed the title enhance(apps/frontend-manage): introduce typing for slate editor enhance(apps/frontend-manage): configure slate editor correctly and introduce typing Oct 20, 2024
Copy link

cypress bot commented Oct 20, 2024

klicker-uzh    Run #3320

Run Properties:  status check failed Failed #3320  •  git commit 5135315c7f ℹ️: Merge 0e0fdf945c401d6dc773664d8ac1708542501bf4 into 864ad7978eef63371d60a0ef9293...
Project klicker-uzh
Run status status check failed Failed #3320
Run duration 12m 39s
Commit git commit 5135315c7f ℹ️: Merge 0e0fdf945c401d6dc773664d8ac1708542501bf4 into 864ad7978eef63371d60a0ef9293...
Committer Julius Schlapbach
View all properties for this run ↗︎

Test results
Tests that failed  Failures 2
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 138

Tests for review

Failed  cypress/e2e/E-course-workflow.cy.ts • 2 failed tests

View Output

Test Artifacts
Test course creation and editing functionalities > Have 2 students join the course and create groups by themselves Test Replay Screenshots
Test course creation and editing functionalities > Check from the student view that they have been assigned to groups successfully Test Replay Screenshots

@sjschlapbach sjschlapbach merged commit 53bf3cc into v3 Oct 20, 2024
8 of 11 checks passed
@sjschlapbach sjschlapbach deleted the slate-typing branch October 20, 2024 13:34
Copy link

cypress bot commented Oct 20, 2024

klicker-uzh    Run #3321

Run Properties:  status check passed Passed #3321  •  git commit 53bf3ccd8e: enhance(apps/frontend-manage): configure slate editor correctly and introduce ty...
Project klicker-uzh
Run status status check passed Passed #3321
Run duration 11m 00s
Commit git commit 53bf3ccd8e: enhance(apps/frontend-manage): configure slate editor correctly and introduce ty...
Committer Julius Schlapbach
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 140

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

Successfully merging this pull request may close these issues.

1 participant