Skip to content

blog: code refactoring#45

Merged
gapry merged 1 commit intomainfrom
code-refactoring
Mar 6, 2026
Merged

blog: code refactoring#45
gapry merged 1 commit intomainfrom
code-refactoring

Conversation

@gapry
Copy link
Owner

@gapry gapry commented Mar 6, 2026

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on significant code refactoring to improve the modularity, readability, and maintainability of the blog application. Key changes include extracting the markdown rendering functionality into its own component and reorganizing the core application logic for routing and data fetching, leading to a cleaner and more structured codebase.

Highlights

  • Markdown Rendering Component: Extracted the ReactMarkdown and syntax highlighting logic into a new, reusable MarkdownRenderer component, improving modularity.
  • Routing and Data Fetching Refactor: Centralized and optimized routing and markdown fetching logic within App.jsx using useCallback hooks for better performance and readability.
  • Improved Content Rendering Structure: Streamlined the main content rendering in App.jsx by using a dedicated renderContent function and a switch statement, enhancing clarity and maintainability.
Changelog
  • src/App.jsx
    • Removed direct ReactMarkdown and syntax highlighter imports.
    • Introduced useCallback for memoizing fetchMarkdown and handleRouting functions.
    • Delegated markdown content rendering to the new MarkdownRenderer component.
    • Refactored the main rendering logic into a renderContent switch statement.
    • Updated routing to use siteConfig.siteName for document titles.
    • Implemented proper popstate event listener setup and cleanup.
  • src/components/MarkdownRenderer.jsx
    • Added a new component responsible for rendering markdown content, including syntax highlighting.
    • Provided an option to conditionally display a 'Back to Home' link.
Activity
  • No specific activity has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@amazon-q-developer amazon-q-developer bot left a comment

Choose a reason for hiding this comment

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

This refactoring successfully extracts markdown rendering into a reusable component and introduces proper memoization with useCallback hooks. The changes improve code organization and maintainability.

Critical Issue Found:

  • Missing error handling in the popstate event handler that could lead to unhandled promise rejections during browser navigation

Please address the critical issue before merging.


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

return <div className="app-shell">Loading...</div>;
}
const onPopState = () => {
fetch('/posts.json').then(res => res.json()).then(handleRouting);

Choose a reason for hiding this comment

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

🛑 Crash Risk: Missing error handling in the popstate event handler. If the fetch request fails, the promise chain will result in an unhandled rejection, potentially causing runtime errors and poor user experience during browser navigation.

Suggested change
fetch('/posts.json').then(res => res.json()).then(handleRouting);
fetch('/posts.json').then(res => res.json()).then(handleRouting).catch(() => setStatus('404'));

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces significant refactoring by extracting the Markdown rendering logic into a dedicated "MarkdownRenderer" component and streamlining the main "App" component's routing and content display. This improves modularity and readability. The use of "useCallback" for "fetchMarkdown" and "handleRouting" is a good practice for performance optimization in React. The semantic use of the "

" tag is also a positive change.


const pathClean = currentPath.replace(/\.html$/, '');
const parts = pathClean.split('/').filter(Boolean);
if (parts.length === 1 && parts[0] === 'about') {

Choose a reason for hiding this comment

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

medium

To allow for differentiated rendering in renderContent, such as conditionally hiding the back link, consider using a more specific status for the 'about' page. For example, setStatus('about') could be used here.

      setStatus('about');

if (parts.length === 4) {
const [year, month, day, slug] = parts;
if (found) {
fetchMarkdown(`/posts/${year}/${found.originalName}.md`, `${found.title} | ${siteConfig.siteName}`);

Choose a reason for hiding this comment

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

medium

To differentiate regular blog posts from other markdown content like the 'about' page, use a more specific status. This enables granular control over rendering options, such as the showBackLink prop in MarkdownRenderer. For example, setStatus('blogPost') could be used here.

        setStatus('blogPost');

return <div className="app-shell">Loading...</div>;
}
const onPopState = () => {
fetch('/posts.json').then(res => res.json()).then(handleRouting);

Choose a reason for hiding this comment

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

medium

The onPopState handler currently refetches posts.json every time the browser history changes. If the posts.json file is static and its content does not change during the application's runtime, this leads to redundant network requests. It would be more efficient to reuse the posts data that has already been fetched and stored in the component's state. This change also requires adding posts to the useEffect dependency array on line 81.

      handleRouting(posts);


window.addEventListener('popstate', onPopState);
return () => window.removeEventListener('popstate', onPopState);
}, [handleRouting]);

Choose a reason for hiding this comment

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

medium

To ensure the onPopState handler (line 76) can access the latest posts state when handleRouting(posts) is called, posts must be included in the useEffect's dependency array. This will cause the effect to re-run and re-attach the event listener if posts changes, which is acceptable since posts is expected to change only once after the initial fetch.

  }, [handleRouting, posts]);

case 'loading': return <div className="loading">Loading...</div>;
case '404': return <NotFound />;
case 'home': return <Home posts={posts} />;
case 'post': return <MarkdownRenderer content={content} />;

Choose a reason for hiding this comment

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

medium

With the introduction of more specific statuses (e.g., 'about' and 'blogPost') in handleRouting, the renderContent function can now conditionally render the MarkdownRenderer with different props. For instance, the 'about' page might not need a "Back to Home" link.

      case 'about':   return <MarkdownRenderer content={content} showBackLink={false} />;
      case 'blogPost':return <MarkdownRenderer content={content} />;

@gapry gapry merged commit fc70fae into main Mar 6, 2026
2 checks passed
@gapry gapry deleted the code-refactoring branch March 6, 2026 00:16
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.

1 participant