-
Notifications
You must be signed in to change notification settings - Fork 0
feat: [performance improvement] avoid flatmap in tags pages #340
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ import TalkCard from "@/components/layout/TalkCard"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import CTASection from "@/components/sections/CTASection"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getEditionConfig } from "@/config/editions"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getTagsFromTalk, getTalks } from "@/hooks/useTalks"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Talk } from "@/hooks/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { Metadata } from "next"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import Link from "next/link"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { notFound } from "next/navigation"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -39,9 +40,21 @@ export async function generateMetadata({ params }: { params: Promise<{ tag: stri | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const decodedTag = decodeURIComponent(tag); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const targetTagLower = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = { matchedTag: undefined as string | undefined }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionGroups.some((group) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group.sessions.some((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (found) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.matchedTag = found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+45
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using nested |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = state.matchedTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title: `Talks tagged "${displayTag}" - DevBcn ${year}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -56,17 +69,29 @@ export default async function Page({ params }: { params: Promise<{ tag: string } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const eventData = getEditionConfig(year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const targetTagLower = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks = allTalks.filter((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const talkTags = getTagsFromTalk(talk); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filteredTalks: [] as Talk[], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| displayTag: undefined as string | undefined, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionGroups.forEach((group) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group.sessions.forEach((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const matchedTag = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (matchedTag) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!state.displayTag) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.displayTag = matchedTag; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.filteredTalks.push(talk); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = state.displayTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks = state.filteredTalks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+75
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using nested
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (filteredTalks.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notFound(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ import TalkCard from "@/components/layout/TalkCard"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import CTASection from "@/components/sections/CTASection"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getArchivedEditions, getEditionConfig } from "@/config/editions"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getTagsFromTalk, getTalks } from "@/hooks/useTalks"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Talk } from "@/hooks/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { Metadata } from "next"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import Link from "next/link"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { notFound } from "next/navigation"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -46,9 +47,21 @@ export async function generateMetadata({ params }: Readonly<TagPageProps>): Prom | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const decodedTag = decodeURIComponent(tag); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const targetTagLower = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = { matchedTag: undefined as string | undefined }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionGroups.some((group) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group.sessions.some((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (found) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.matchedTag = found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using nested |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = state.matchedTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title: `Talks tagged "${displayTag}" - DevBcn ${year}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -62,17 +75,29 @@ export default async function TagPage({ params }: Readonly<TagPageProps>) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const eventData = getEditionConfig(year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const targetTagLower = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks = allTalks.filter((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const talkTags = getTagsFromTalk(talk); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filteredTalks: [] as Talk[], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| displayTag: undefined as string | undefined, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionGroups.forEach((group) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group.sessions.forEach((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const matchedTag = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (matchedTag) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!state.displayTag) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.displayTag = matchedTag; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.filteredTalks.push(talk); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = state.displayTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks = state.filteredTalks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+81
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using nested
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (filteredTalks.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notFound(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the recommended action to suggest nested
for...ofloops instead of.some()or.forEach()with state objects/arrays, asfor...ofloops are cleaner, more idiomatic, and avoid closure allocation overhead.