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

fix: rendering issue with few components using next-i18n on community pages #3384

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions components/CommunityEvents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { useState } from 'react';

import EventFilter from '@/components/navigation/EventFilter';
import EventPostItem from '@/components/navigation/EventPostItem';
import Heading from '@/components/typography/Heading';
import Paragraph from '@/components/typography/Paragraph';
import meetings from '@/config/meetings.json';
import type { Event } from '@/types/pages/community/Community';
import { HeadingLevel, HeadingTypeStyle } from '@/types/typography/Heading';
import { ParagraphTypeStyle } from '@/types/typography/Paragraph';
import { getEvents } from '@/utils/staticHelpers';

const CommunityEvents = () => {
const [events, setEvents] = useState(getEvents(meetings));

Comment on lines +13 to +15
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Optimize state initialization to prevent unnecessary calculations.

The getEvents(meetings) call in useState will run on every render. Consider optimizing this by either:

  1. Moving the initialization outside the component
  2. Using useMemo to memoize the result
+const initialEvents = getEvents(meetings);
+
 const CommunityEvents = () => {
-  const [events, setEvents] = useState(getEvents(meetings));
+  const [events, setEvents] = useState(initialEvents);

Or with useMemo:

 const CommunityEvents = () => {
-  const [events, setEvents] = useState(getEvents(meetings));
+  const initialEvents = useMemo(() => getEvents(meetings), []);
+  const [events, setEvents] = useState(initialEvents);

Committable suggestion skipped: line range outside the PR's diff.

return (
<div className='mt-20'>
<div className='items-center justify-between sm:flex'>
<Heading level={HeadingLevel.h2} typeStyle={HeadingTypeStyle.md}>
All Events
</Heading>
<div className='mt-5 sm:mt-0'>
<EventFilter data={meetings} setData={setEvents} />
</div>
</div>
<div className='mt-10'>
{!events || events.length === 0 ? (
<div className='flex content-center justify-center'>
<Paragraph typeStyle={ParagraphTypeStyle.md} className='mx-auto mt-5 max-w-2xl'>
No Events. Check back later!
</Paragraph>
</div>
) : (
<ul className='grid grid-cols-1 gap-5 md:grid-cols-2 lg:grid-cols-3'>
{events.map((event: Event, index: number) => {
return <EventPostItem key={index} id={event.title} post={event} />;
})}
Comment on lines +35 to +37
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Avoid using array indices as React keys.

Using array indices as keys can lead to issues with component state and performance when the list items can change (reordering, filtering, etc.). Consider using a unique identifier from the event data.

-{events.map((event: Event, index: number) => {
-  return <EventPostItem key={index} id={event.title} post={event} />;
+{events.map((event: Event) => {
+  return <EventPostItem key={event.title} id={event.title} post={event} />;

Committable suggestion skipped: line range outside the PR's diff.

</ul>
)}
</div>
</div>
);
};

export default CommunityEvents;
39 changes: 7 additions & 32 deletions pages/community/events/index.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
/* eslint-disable react/no-unescaped-entities */
import { ArrowRightIcon } from '@heroicons/react/outline';
import React, { useState } from 'react';

import type { Event } from '@/types/pages/community/Community';
import CommunityEvents from '@/components/CommunityEvents';
import { HeadingLevel, HeadingTypeStyle } from '@/types/typography/Heading';
import { ParagraphTypeStyle } from '@/types/typography/Paragraph';
import { makeStaticProps } from '@/utils/getStatic';

import GoogleCalendarButton from '../../../components/buttons/GoogleCalendarButton';
import ICSFileButton from '../../../components/buttons/ICSFileButton';
import GenericLayout from '../../../components/layout/GenericLayout';
import Meeting from '../../../components/Meeting';
import EventFilter from '../../../components/navigation/EventFilter';
import EventPostItem from '../../../components/navigation/EventPostItem';
import NewsletterSubscribe from '../../../components/NewsletterSubscribe';
import Heading from '../../../components/typography/Heading';
import Paragraph from '../../../components/typography/Paragraph';
import TextLink from '../../../components/typography/TextLink';
import meetings from '../../../config/meetings.json';
import { getEvents } from '../../../utils/staticHelpers';

const getStaticProps = makeStaticProps(['landing-page', 'footer', 'common']);

export { getStaticProps };

/**
* @description This is the events page which displays all the events and meetings.
*/
export default function EventIndex() {
const image = '/img/social/community-events.webp';
const [events, setEvents] = useState(getEvents(meetings));

return (
<GenericLayout title='AsyncAPI events' description='Our catalogs of events and meetups' image={image} wide>
Expand Down Expand Up @@ -89,31 +88,7 @@ export default function EventIndex() {
</div>
</div>
</div>
<div className='mt-20'>
<div className='items-center justify-between sm:flex'>
<Heading level={HeadingLevel.h2} typeStyle={HeadingTypeStyle.md}>
All Events
</Heading>
<div className='mt-5 sm:mt-0'>
<EventFilter data={meetings} setData={setEvents} />
</div>
</div>
<div className='mt-10'>
{!events || events.length === 0 ? (
<div className='flex content-center justify-center'>
<Paragraph typeStyle={ParagraphTypeStyle.md} className='mx-auto mt-5 max-w-2xl'>
No Events. Check back later!
</Paragraph>
</div>
) : (
<ul className='grid grid-cols-1 gap-5 md:grid-cols-2 lg:grid-cols-3'>
{events.map((event: Event, index: number) => {
return <EventPostItem key={index} id={event.title} post={event} />;
})}
</ul>
)}
</div>
</div>
<CommunityEvents />
<div className='mt-24'>
<div className='lg:flex lg:justify-between' data-testid='EventTypesCard'>
<div className='lg:w-[30%]'>
Expand Down
5 changes: 5 additions & 0 deletions pages/community/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';

import { CardType } from '@/types/components/community/CardPropsType';
import { HeadingLevel, HeadingTypeStyle } from '@/types/typography/Heading';
import { makeStaticProps } from '@/utils/getStatic';

import Card from '../../components/community/Card';
import Header from '../../components/community/Header';
Expand All @@ -14,6 +15,10 @@ import Heading from '../../components/typography/Heading';
import eventsData from '../../config/meetings.json';
import { getEvents } from '../../utils/staticHelpers';

const getStaticProps = makeStaticProps(['common']);

export { getStaticProps };

interface Event {
title: string;
date: moment.Moment;
Expand Down
24 changes: 11 additions & 13 deletions pages/community/tsc.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sortBy } from 'lodash';
import React, { useState } from 'react';

import type { Tsc } from '@/types/pages/community/Community';
import { makeStaticProps } from '@/utils/getStatic';

import IconGithub from '../../components/icons/Github';
import IconLinkedIn from '../../components/icons/LinkedIn';
Expand All @@ -11,6 +11,10 @@ import NewsletterSubscribe from '../../components/NewsletterSubscribe';
import TextLink from '../../components/typography/TextLink';
import TSCMembersList from '../../config/MAINTAINERS.json';

const getStaticProps = makeStaticProps(['common']);

export { getStaticProps };

interface SocialLinkProps {
href: string;
social: string;
Expand Down Expand Up @@ -66,11 +70,9 @@ function addAdditionalUserInfo(user: Tsc) {
* @returns The Twitter SVG component.
*/
function TwitterSVG() {
const [isHovered, setIsHovered] = useState(false);

return (
<div className='size-5' onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)}>
<IconTwitter className={isHovered ? 'hover:fill-black' : ''} />
<div className='size-5'>
<IconTwitter className={'hover:fill-black'} />
</div>
);
}
Expand All @@ -81,11 +83,9 @@ function TwitterSVG() {
* @returns The GitHub SVG component.
*/
function GitHubSVG() {
const [isHovered, setIsHovered] = useState(false);

return (
<div className='size-5' onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)}>
<IconGithub className={isHovered ? 'hover:fill-black' : ''} />
<div className='size-5'>
<IconGithub className={'hover:fill-black'} />
</div>
);
}
Expand All @@ -96,12 +96,10 @@ function GitHubSVG() {
* @returns The LinkedIn SVG component.
*/
function LinkedInSVG() {
const [isHovered, setIsHovered] = useState(false);

return (
<div className='size-5' onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)}>
<div className='size-5'>
{/* Use the imported SVG icon component */}
<IconLinkedIn className={isHovered ? 'hover:fill-linkedin' : ''} />
<IconLinkedIn className={'hover:fill-linkedin'} />
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion utils/getStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const getStaticPaths = () => ({
* @returns An object containing the internationalization props.
*/
export async function getI18nProps(ctx: any, ns = ['common']) {
const locale = ctx?.params?.lang;
const locale = ctx?.params?.lang ? ctx?.params?.lang : 'en';
const props = {
...(await serverSideTranslations(locale, ns))
};
Expand Down
Loading