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

Use static navigation #3126

Merged
merged 6 commits into from
Jan 25, 2024
Merged

Use static navigation #3126

merged 6 commits into from
Jan 25, 2024

Conversation

mlbrgl
Copy link
Member

@mlbrgl mlbrgl commented Jan 23, 2024

This replaces the dynamic Wordpress navigation, derived from the categories taxonomy, with a static navigation object.

Until we find a more permanent place and editing UI for the navigation, I opted for a simple JS object, which gives us type checking for free.

I don't expect this data structure to evolve regularly in the near future (it hasn't over the past few years), so it is probably not worth complexifying the setup with a JSON-based alternative and training authors to update it. This will likely remain a developer task.

Summary by CodeRabbit

  • New Features

    • Expanded the site navigation structure to include a detailed hierarchical representation of categories and subcategories.
  • Bug Fixes

    • Adjusted site navigation handling to account for cases where subcategories may be undefined or null.
  • Refactor

    • Streamlined site rendering and database interactions by utilizing a static navigation model.
    • Removed unnecessary properties from content graph interfaces to align with updated site structure.
  • Chores

    • Cleaned up unused functions and imports related to site navigation and content retrieval.

@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 23, 2024

@mlbrgl mlbrgl requested a review from danyx23 January 23, 2024 10:33
@mlbrgl mlbrgl marked this pull request as ready for review January 23, 2024 10:33
Copy link

coderabbitai bot commented Jan 23, 2024

Walkthrough

The codebase has undergone a significant transformation, shifting from dynamic content retrieval to a static site structure. This change involved removing dynamic data fetching methods and properties like excerpts and KPIs. Instead, a static site navigation system was integrated, providing a more organized and stable content structure. This transition aims to streamline content delivery and simplify the site's navigation mechanism.

Changes

File Path Summary
db/wpdb.ts Modified getEntryNode and isPostCitable functions, removed getEntriesByCategory, integrated SiteNavigationStatic.
site/SiteNavigation.tsx Expanded SiteNavigationStatic to include a detailed hierarchical structure.
site/SiteNavigationTopics.tsx Improved handling of null or undefined subcategories with a nullish coalescing operator.

"In the warren of the web, we've stilled the fetch,
🌐 Now static branches where dynamic ones stretched.
The site's paths are laid, with categories nested,
A rabbit's touch on the code, deftly tested." 🐇✨

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 18e6518 and 7fc5a2b.
Files selected for processing (5)
  • baker/siteRenderers.tsx (3 hunks)
  • db/wpdb.ts (4 hunks)
  • packages/@ourworldindata/types/src/domainTypes/ContentGraph.ts (1 hunks)
  • site/SiteNavigation.tsx (1 hunks)
  • site/SiteNavigationTopics.tsx (1 hunks)
Additional comments: 6
packages/@ourworldindata/types/src/domainTypes/ContentGraph.ts (1)
  • 3-17: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-11]

The removal of excerpt and kpi from EntryMeta and the change to make subcategories optional in CategoryWithEntries are consistent with the PR objectives and summary.

site/SiteNavigationTopics.tsx (1)
  • 99-101: The use of the nullish coalescing operator to provide an empty array as a fallback for category.subcategories is a good defensive programming practice.
baker/siteRenderers.tsx (2)
  • 207-207: The modification of renderMenuJson to return JSON.stringify(SiteNavigationStatic) aligns with the PR's goal of transitioning to a static navigation structure.
  • 207-207: Verify that the static navigation object SiteNavigationStatic is correctly structured and that all references to the old dynamic fetching method have been updated or removed.
db/wpdb.ts (2)
  • 52-52: Import of SiteNavigationStatic is added to replace dynamic fetching with static navigation.
  • 331-342: The isPostCitable function has been refactored to use the static navigation object instead of the removed dynamic fetching function.

Comment on lines +331 to +322
const entries = SiteNavigationStatic.categories
return entries.some((category) => {
return (
category.entries.some((entry) => entry.slug === post.slug) ||
category.subcategories.some((subcategory: CategoryWithEntries) => {
return subcategory.entries.some(
(subCategoryEntry) => subCategoryEntry.slug === post.slug
)
})
(category.subcategories ?? []).some(
(subcategory: CategoryWithEntries) => {
return subcategory.entries.some(
(subCategoryEntry) =>
subCategoryEntry.slug === post.slug
)
}
)
Copy link

@coderabbitai coderabbitai bot Jan 23, 2024

Choose a reason for hiding this comment

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

The logic within isPostCitable assumes that the subcategories property always exists, which may not be the case according to the changes in ContentGraph.ts. Consider adding a nullish coalescing operator or default value for subcategories.

- (category.subcategories ?? []).some(
+ (category.subcategories || []).some(

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.

Suggested change
const entries = SiteNavigationStatic.categories
return entries.some((category) => {
return (
category.entries.some((entry) => entry.slug === post.slug) ||
category.subcategories.some((subcategory: CategoryWithEntries) => {
return subcategory.entries.some(
(subCategoryEntry) => subCategoryEntry.slug === post.slug
)
})
(category.subcategories ?? []).some(
(subcategory: CategoryWithEntries) => {
return subcategory.entries.some(
(subCategoryEntry) =>
subCategoryEntry.slug === post.slug
)
}
)
const entries = SiteNavigationStatic.categories
return entries.some((category) => {
return (
category.entries.some((entry) => entry.slug === post.slug) ||
(category.subcategories || []).some(
(subcategory: CategoryWithEntries) => {
return subcategory.entries.some(
(subCategoryEntry) =>
subCategoryEntry.slug === post.slug
)
}
)

The isPostCitable function uses a deeply nested structure with multiple some and every calls. This could be refactored for better readability and potentially performance by flattening the structure or using a different approach to check for the post's citability.

Copy link
Member Author

Choose a reason for hiding this comment

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

@coderabbitai a nullish coalescing operator has already been added in 82bebf4

Copy link

Choose a reason for hiding this comment

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

@mlbrgl Thank you for the update. I've verified the changes in commit 82bebf4c1d05b7aee20cead971f37cd50f207633, and the nullish coalescing operator has indeed been added to the isPostCitable function. Your implementation now correctly handles potential null or undefined values for subcategories.

🎉


🐇✨

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Check the changes made in the specified commit for the `isPostCitable` function
git show 82bebf4c1d05b7aee20cead971f37cd50f207633 -- db/wpdb.ts

Length of output: 5661


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

Comment on lines 3 to 17
export interface EntryMeta {
slug: string
title: string
excerpt: string
kpi: string
}

export interface CategoryWithEntries {
name: string
slug: string
entries: EntryMeta[]
subcategories: CategoryWithEntries[]
subcategories?: CategoryWithEntries[]
}

export enum GraphDocumentType {
Topic = "topic",
Article = "article",
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The search for .excerpt yielded multiple results across various files in the codebase, indicating that the excerpt property is indeed used in several places. However, there were no results for .kpi, suggesting that this property might not be used or is less prevalent.

Given the results, it is clear that the removal of the excerpt property from the EntryMeta interface could potentially impact the functionality in the files where .excerpt is referenced. This needs to be addressed to ensure that the removal does not cause any unintended side effects.

The review comment about verifying the impact of removing excerpt and kpi from EntryMeta is therefore substantiated by the search results for .excerpt. The absence of .kpi in the search results does not necessarily mean it is not used, as it could be referenced in a way that was not captured by the search pattern.

Based on the findings, the appropriate response is to flag the potential issues with the removal of the excerpt property.

Analysis chain

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-11]

Verify that the removal of excerpt and kpi from EntryMeta does not negatively impact other parts of the codebase.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for usages of EntryMeta properties that have been removed.
ast-grep --lang typescript --pattern $'EntryMeta { $$$ .excerpt $$$ }'
ast-grep --lang typescript --pattern $'EntryMeta { $$$ .kpi $$$ }'

Length of output: 1041



Script:

#!/bin/bash
# Correct the pattern syntax and search for usages of EntryMeta properties that have been removed.
ast-grep --lang typescript --pattern $'interface EntryMeta { $$$ }' --exec 'rg "\.excerpt" {}'
ast-grep --lang typescript --pattern $'interface EntryMeta { $$$ }' --exec 'rg "\.kpi" {}'

Length of output: 575



Script:

#!/bin/bash
# Find files that contain the EntryMeta interface definition.
entry_meta_files=$(ast-grep --lang typescript --pattern $'interface EntryMeta { $$$ }' | rg -o '[^ ]+$')

# Search for usages of the removed properties within those files.
for file in $entry_meta_files; do
  rg "\.excerpt" $file
  rg "\.kpi" $file
done

Length of output: 1109



Script:

#!/bin/bash
# Directly search the entire codebase for references to the removed properties.
rg "\.excerpt"
rg "\.kpi"

Length of output: 1611

@mlbrgl mlbrgl force-pushed the static-navigation branch from 7fc5a2b to 3fd221f Compare January 23, 2024 13:00
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 18e6518 and 3fd221f.
Files selected for processing (5)
  • baker/siteRenderers.tsx (3 hunks)
  • db/wpdb.ts (4 hunks)
  • packages/@ourworldindata/types/src/domainTypes/ContentGraph.ts (1 hunks)
  • site/SiteNavigation.tsx (1 hunks)
  • site/SiteNavigationTopics.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • baker/siteRenderers.tsx
  • db/wpdb.ts
  • packages/@ourworldindata/types/src/domainTypes/ContentGraph.ts
  • site/SiteNavigationTopics.tsx

Comment on lines 251 to 1094
{
"name": "Human Rights and Democracy",
"slug": "politics",
"entries": [],
"subcategories": [
{
"name": "Human Rights",
"slug": "human-rights",
"entries": [
{
"slug": "state-capacity",
"title": "State Capacity"
},
{
"slug": "lgbt-rights",
"title": "LGBT+ Rights"
},
{
"slug": "women-rights",
"title": "Women’s Rights"
},
{
"slug": "child-labor",
"title": "Child Labor"
},
{
"slug": "human-rights",
"title": "Human Rights"
}
]
},
{
"name": "Democracy and Corruption",
"slug": "democracy",
"entries": [
{
"slug": "democracy",
"title": "Democracy"
},
{
"slug": "corruption",
"title": "Corruption"
}
]
}
]
},
{
"name": "Violence and War",
"slug": "violence-rights",
"entries": [],
"subcategories": [
{
"name": "War and Peace",
"slug": "war-peace",
"entries": [
{
"slug": "biological-and-chemical-weapons",
"title": "Biological and Chemical Weapons"
},
{
"slug": "war-and-peace",
"title": "War and Peace"
},
{
"slug": "military-personnel-spending",
"title": "Military Personnel and Spending"
},
{
"slug": "terrorism",
"title": "Terrorism"
},
{
"slug": "nuclear-weapons",
"title": "Nuclear Weapons"
}
]
},
{
"name": "Violence",
"slug": "violence",
"entries": [
{
"slug": "state-capacity",
"title": "State Capacity"
},
{
"slug": "violence-against-rights-for-children",
"title": "Violence Against Children and Children’s Rights"
},
{
"slug": "homicides",
"title": "Homicides"
}
]
}
]
}
]
}
Copy link

Choose a reason for hiding this comment

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

The SiteNavigationStatic object has been added to provide a static representation of site navigation. This is a large object literal with nested structures. Ensure that:

  • The object structure aligns with the intended navigation schema.
  • There are no duplicate slugs or titles within the same category level, which could cause navigation issues.
  • The object is properly formatted and maintains a consistent style for readability.
  • Consider breaking down this large object into smaller chunks or separate files if it becomes too unwieldy to manage within a single file.
// Example of breaking down the object into smaller parts for maintainability
export const PopulationCategory: CategoryWithEntries = {
  name: "Population and Demographic Change",
  slug: "population",
  entries: [],
  subcategories: [
    // ...subcategories
  ],
};

export const HealthCategory: CategoryWithEntries = {
  name: "Health",
  slug: "health",
  entries: [],
  subcategories: [
    // ...subcategories
  ],
};

// ... other categories

export const SiteNavigationStatic: { categories: CategoryWithEntries[] } = {
  categories: [
    PopulationCategory,
    HealthCategory,
    // ... other categories
  ],
};

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3fd221f and 1ea97dc.
Files selected for processing (3)
  • db/wpdb.ts (4 hunks)
  • site/SiteNavigation.tsx (1 hunks)
  • site/SiteNavigationTopics.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • db/wpdb.ts
  • site/SiteNavigationTopics.tsx

Comment on lines +251 to +1094
{
name: "Human Rights and Democracy",
slug: "politics",
entries: [],
subcategories: [
{
name: "Human Rights",
slug: "human-rights",
entries: [
{
slug: "state-capacity",
title: "State Capacity",
},
{
slug: "lgbt-rights",
title: "LGBT+ Rights",
},
{
slug: "women-rights",
title: "Women’s Rights",
},
{
slug: "child-labor",
title: "Child Labor",
},
{
slug: "human-rights",
title: "Human Rights",
},
],
},
{
name: "Democracy and Corruption",
slug: "democracy",
entries: [
{
slug: "democracy",
title: "Democracy",
},
{
slug: "corruption",
title: "Corruption",
},
],
},
],
},
{
name: "Violence and War",
slug: "violence-rights",
entries: [],
subcategories: [
{
name: "War and Peace",
slug: "war-peace",
entries: [
{
slug: "biological-and-chemical-weapons",
title: "Biological and Chemical Weapons",
},
{
slug: "war-and-peace",
title: "War and Peace",
},
{
slug: "military-personnel-spending",
title: "Military Personnel and Spending",
},
{
slug: "terrorism",
title: "Terrorism",
},
{
slug: "nuclear-weapons",
title: "Nuclear Weapons",
},
],
},
{
name: "Violence",
slug: "violence",
entries: [
{
slug: "state-capacity",
title: "State Capacity",
},
{
slug: "violence-against-rights-for-children",
title: "Violence Against Children and Children’s Rights",
},
{
slug: "homicides",
title: "Homicides",
},
],
},
],
},
],
}
Copy link

@coderabbitai coderabbitai bot Jan 23, 2024

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The verification process has identified multiple instances of non-unique slugs within the SiteNavigationStatic object, which aligns with the concern raised in the review comment about ensuring slug uniqueness. This could potentially lead to navigation issues.

Analysis chain

The SiteNavigationStatic object has been significantly expanded. Ensure that all slugs within this object are unique to prevent navigation issues. Consider adding a test or a build-time check to enforce the uniqueness of slugs.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all slugs in SiteNavigationStatic are unique
rg --type js "slug: \"[a-z0-9-]+\"" site/SiteNavigation.tsx | sort | uniq -c | grep -v " 1 "

Length of output: 1622


The static navigation structure is extensive and deeply nested. Ensure that the structure is well-documented and that there is a clear process for developers to update it when necessary. This is important for maintainability, especially since the navigation is expected to be managed by developers.

Copy link
Member Author

Choose a reason for hiding this comment

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

can you list the non-unique slugs identified?

Copy link

Choose a reason for hiding this comment

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

@mlbrgl The non-unique slugs identified in the SiteNavigationStatic object are as follows:

air-pollution
alcohol-consumption
child-labor
child-mortality
clean-water-sanitation
corruption
democracy
economic-inequality
energy
energy-access
environmental-impacts-of-food
financing-education
financing-healthcare
human-rights
indoor-air-pollution
internet
land-use
lead-pollution
life-expectancy
migration
military-personnel-spending
obesity
outdoor-air-pollution
research-and-development
sanitation
state-capacity
technological-change
time-use
tourism
water-access
working-hours

These slugs appear more than once in the navigation structure and should be made unique to prevent navigation issues.

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Extract slugs from SiteNavigationStatic, sort them, and identify duplicates
rg --type js "slug: \"[a-z0-9-]+\"" site/SiteNavigation.tsx | \
  sed -E 's/.*slug: "([^"]+)".*/\1/' | \
  sort | \
  uniq -d

Length of output: 598

Copy link
Member Author

Choose a reason for hiding this comment

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

it's fine for slugs to appear in multiple places in the navigation, since topics can appear under multiple top-level categories.

Copy link

Choose a reason for hiding this comment

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

@mlbrgl Understood, the reuse of slugs across different top-level categories is by design to accommodate topics appearing under multiple categories. No changes are required regarding slug uniqueness. Thank you for the clarification.

🐰✨


New Learnings Added
User: mlbrgl
PR: owid/owid-grapher#3126
File: site/SiteNavigation.tsx:251-1094
Timestamp: 2024-01-23T13:39:23.643Z
Learning: Slugs in the `SiteNavigationStatic` object can be non-unique across different top-level categories because topics can appear under multiple categories. This is intentional and acceptable within the navigation structure.

Copy link
Contributor

@danyx23 danyx23 left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link
Contributor

@danyx23 danyx23 left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1ea97dc and 5a2393a.
Files selected for processing (1)
  • db/wpdb.ts (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • db/wpdb.ts

@danyx23
Copy link
Contributor

danyx23 commented Jan 25, 2024

@mlbrgl just a quick heads up that I added one tiny commit to remove an unused import so you might want to rebase your later changes

@mlbrgl mlbrgl merged commit 3b5c212 into master Jan 25, 2024
16 checks passed
@mlbrgl mlbrgl deleted the static-navigation branch January 25, 2024 15:02
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.

2 participants