Skip to content

Commit

Permalink
✨ rename 'breadcrumbs' -> 'manualBreadcrumbs'
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Jan 9, 2025
1 parent 3cc29e8 commit a41cde5
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const BreadcrumbLine = ({
)
}

export const GdocsBreadcrumbsInput = ({
export const GdocsManualBreadcrumbsInput = ({
gdoc,
setCurrentGdoc,
errors,
Expand All @@ -85,56 +85,63 @@ export const GdocsBreadcrumbsInput = ({
breadcrumbs[breadcrumbs.length - 1].href = undefined
} else breadcrumbs = undefined

setCurrentGdoc({ ...gdoc, breadcrumbs: breadcrumbs ?? null })
setCurrentGdoc({ ...gdoc, manualBreadcrumbs: breadcrumbs ?? null })
}

const setItemAtIndex = (item: BreadcrumbItem, i: number) => {
const breadcrumbs = [...(gdoc.breadcrumbs ?? [])]
const breadcrumbs = [...(gdoc.manualBreadcrumbs ?? [])]
breadcrumbs[i] = item
setBreadcrumbs(breadcrumbs)
}

const removeItemAtIndex = (i: number) => {
const breadcrumbs = [...(gdoc.breadcrumbs ?? [])]
const breadcrumbs = [...(gdoc.manualBreadcrumbs ?? [])]
breadcrumbs.splice(i, 1)
setBreadcrumbs(breadcrumbs)
}

return (
<div className="form-group">
<div className="d-flex justify-content-between">
Breadcrumbs
<Button
type="dashed"
onClick={() =>
setBreadcrumbs([
{ label: "" },
...(gdoc.breadcrumbs ?? []),
])
}
>
<FontAwesomeIcon icon={faPlus} className="mr-1" /> Add
breadcrumb
</Button>
</div>
{gdoc.breadcrumbs?.map((item, i) => (
<div className="d-flex justify-content-between">Breadcrumbs</div>
{!!gdoc.breadcrumbs?.length && !gdoc.manualBreadcrumbs?.length && (
<p>
The breadcrumbs for this article will be automatically
generated, based on this article's tags and the tag graph.
If you want to override these breadcrumbs, you can do so
here.
</p>
)}
<Button
type="dashed"
onClick={() =>
setBreadcrumbs([
{ label: "" },
...(gdoc.manualBreadcrumbs ?? []),
])
}
>
<FontAwesomeIcon icon={faPlus} className="mr-1" /> Add
breadcrumb
</Button>
{gdoc.manualBreadcrumbs?.map((item, i) => (
<BreadcrumbLine
item={item}
setItem={(item) => setItemAtIndex(item, i)}
removeItem={() => removeItemAtIndex(i)}
key={i}
labelError={getPropertyMostCriticalError(
`breadcrumbs[${i}].label`,
`manualBreadcrumbs[${i}].label`,
errors
)}
hrefError={getPropertyMostCriticalError(
`breadcrumbs[${i}].href`,
`manualBreadcrumbs[${i}].href`,
errors
)}
isLastBreadcrumbItem={i === gdoc.breadcrumbs!.length - 1}
isLastBreadcrumbItem={
i === gdoc.manualBreadcrumbs!.length - 1
}
/>
))}
{!gdoc.breadcrumbs?.length && <i>No breadcrumbs</i>}
</div>
)
}
4 changes: 2 additions & 2 deletions adminSiteClient/GdocsSettingsForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { GdocsPublishedAt } from "./GdocsDateline.js"
import { GdocsPublicationContext } from "./GdocsPublicationContext.js"
import { Alert } from "antd"
import { GdocsBreadcrumbsInput } from "./GdocsBreadcrumbsInput.js"
import { GdocsManualBreadcrumbsInput } from "./GdocsManualBreadcrumbsInput.js"

const GdocCommonErrors = ({
errors,
Expand Down Expand Up @@ -155,7 +155,7 @@ export const GdocPostSettings = ({
errors={errors}
description="An optional property to override the excerpt of this post in our atom feed, which is used for the newsletter"
/>
<GdocsBreadcrumbsInput
<GdocsManualBreadcrumbsInput
gdoc={gdoc}
errors={errors}
setCurrentGdoc={setCurrentGdoc}
Expand Down
3 changes: 2 additions & 1 deletion adminSiteClient/gdocsDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const checkIsLightningUpdate = (
Exclude<keyof OwidGdocBaseInterface, "content">,
boolean
> = {
breadcrumbs: true,
breadcrumbs: true, // automatically generated, not actually possible to change via the admin preview
manualBreadcrumbs: true,
errors: true,
linkedAuthors: false,
linkedCharts: true,
Expand Down
10 changes: 5 additions & 5 deletions adminSiteClient/gdocsValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ function validateExcerpt(
}
}

function validateBreadcrumbs(
function validateManualBreadcrumbs(
gdoc: OwidGdocPostInterface,
errors: OwidGdocErrorMessage[]
) {
if (gdoc.breadcrumbs) {
for (const [i, breadcrumb] of gdoc.breadcrumbs.entries()) {
if (gdoc.manualBreadcrumbs) {
for (const [i, breadcrumb] of gdoc.manualBreadcrumbs.entries()) {
if (!breadcrumb.label) {
errors.push({
property: `breadcrumbs[${i}].label`,
Expand All @@ -155,7 +155,7 @@ function validateBreadcrumbs(
}

// Last item can be missing a href
if (!breadcrumb.href && i !== gdoc.breadcrumbs.length - 1) {
if (!breadcrumb.href && i !== gdoc.manualBreadcrumbs.length - 1) {
errors.push({
property: `breadcrumbs[${i}].href`,
type: OwidGdocErrorMessageType.Error,
Expand Down Expand Up @@ -292,7 +292,7 @@ export const getErrors = (gdoc: OwidGdoc): OwidGdocErrorMessage[] => {
if (checkIsGdocPost(gdoc)) {
validateRefs(gdoc, errors)
validateExcerpt(gdoc, errors)
validateBreadcrumbs(gdoc, errors)
validateManualBreadcrumbs(gdoc, errors)
validateAtomFields(gdoc, errors)
} else if (checkIsDataInsight(gdoc)) {
validateApprovedBy(gdoc, errors)
Expand Down
2 changes: 1 addition & 1 deletion db/migrateWpPostsToArchieMl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const migrate = async (trx: db.KnexReadWriteTransaction): Promise<void> => {
updatedAt: post.updated_at_in_wordpress,
publicationContext: OwidGdocPublicationContext.listed, // TODO: not all articles are listed, take this from the DB
revisionId: null,
breadcrumbs: null,
manualBreadcrumbs: null,
markdown: null,
}
const archieMlStatsContent = {
Expand Down
15 changes: 15 additions & 0 deletions db/migration/1736455365750-RenameBreadcrumbsColumn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class RenameBreadcrumbsColumn1736455365750
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE posts_gdocs RENAME COLUMN breadcrumbs TO manualBreadcrumbs`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE posts_gdocs RENAME COLUMN manualBreadcrumbs TO breadcrumbs`)
}
}
1 change: 1 addition & 0 deletions db/model/Gdoc/GdocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class GdocBase implements OwidGdocBaseInterface {
publicationContext: OwidGdocPublicationContext =
OwidGdocPublicationContext.unlisted
breadcrumbs: BreadcrumbItem[] | null = null
manualBreadcrumbs: BreadcrumbItem[] | null = null
tags: DbPlainTag[] | null = null
errors: OwidGdocErrorMessage[] = []
donors: string[] = []
Expand Down
4 changes: 2 additions & 2 deletions db/model/Gdoc/GdocFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export async function getGdocBaseObjectById(
)
gdoc.tags = tags

if (!gdoc.breadcrumbs?.length && tags.length) {
if (tags.length) {
const parentTagArraysByChildName =
await getParentTagArraysByChildName(knex)
gdoc.breadcrumbs = getBestBreadcrumbs(
Expand Down Expand Up @@ -595,7 +595,7 @@ export function getDbEnrichedGdocFromOwidGdoc(
gdoc: OwidGdoc | GdocBase
): DbEnrichedPostGdoc {
const enrichedGdoc = {
breadcrumbs: gdoc.breadcrumbs,
manualBreadcrumbs: gdoc.manualBreadcrumbs,
content: gdoc.content,
createdAt: gdoc.createdAt,
id: gdoc.id,
Expand Down
15 changes: 10 additions & 5 deletions packages/@ourworldindata/types/src/dbTypes/PostsGdocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MinimalTag } from "./Tags.js"

export const PostsGdocsTableName = "posts_gdocs"
export interface DbInsertPostGdoc {
breadcrumbs?: JsonString | null
manualBreadcrumbs?: JsonString | null
content: JsonString
createdAt: Date
id: string
Expand All @@ -23,10 +23,10 @@ export interface DbInsertPostGdoc {
export type DbRawPostGdoc = Required<DbInsertPostGdoc>
export type DbEnrichedPostGdoc = Omit<
DbRawPostGdoc,
"content" | "breadcrumbs" | "published"
"content" | "manualBreadcrumbs" | "published"
> & {
content: OwidGdocContent
breadcrumbs: BreadcrumbItem[] | null
manualBreadcrumbs: BreadcrumbItem[] | null
published: boolean
}

Expand Down Expand Up @@ -62,7 +62,7 @@ export function parsePostsGdocsRow(row: DbRawPostGdoc): DbEnrichedPostGdoc {
return {
...row,
content: parsePostGdocContent(row.content),
breadcrumbs: parsePostsGdocsBreadcrumbs(row.breadcrumbs),
manualBreadcrumbs: parsePostsGdocsBreadcrumbs(row.manualBreadcrumbs),
published: !!row.published,
}
}
Expand All @@ -77,10 +77,15 @@ export function parsePostsGdocsWithTagsRow(
}

export function serializePostsGdocsRow(row: DbEnrichedPostGdoc): DbRawPostGdoc {
if ("breadcrumbs" in row) {
delete row.breadcrumbs
}
return {
...row,
content: serializePostGdocContent(row.content),
breadcrumbs: serializePostsGdocsBreadcrumbs(row.breadcrumbs),
manualBreadcrumbs: serializePostsGdocsBreadcrumbs(
row.manualBreadcrumbs
),
published: row.published ? 1 : 0,
}
}
3 changes: 2 additions & 1 deletion packages/@ourworldindata/types/src/gdocTypes/Gdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface OwidGdocBaseInterface {
updatedAt: Date | null
revisionId: string | null
publicationContext: OwidGdocPublicationContext
breadcrumbs: BreadcrumbItem[] | null
manualBreadcrumbs: BreadcrumbItem[] | null
linkedAuthors?: LinkedAuthor[]
linkedDocuments?: Record<string, OwidGdocMinimalPostInterface>
linkedCharts?: Record<string, LinkedChart>
Expand All @@ -96,6 +96,7 @@ export interface OwidGdocBaseInterface {
relatedCharts?: RelatedChart[]
tags?: MinimalTag[] | null
errors?: OwidGdocErrorMessage[]
breadcrumbs?: BreadcrumbItem[] | null
markdown: string | null
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,7 @@ export function isFiniteWithGuard(value: unknown): value is number {

// Use with getParentTagArraysByChildName to collapse all paths to the child into a single array of unique parent tag names
export function getUniqueNamesFromParentTagArrays(
parentTagArrays: DbPlainTag[][]
parentTagArrays: Pick<DbPlainTag, "id" | "name" | "slug">[][]
): string[] {
const tagNames = new Set<string>(
parentTagArrays.flatMap((parentTagArray) =>
Expand Down
3 changes: 2 additions & 1 deletion site/gdocs/pages/GdocPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function GdocPost({
publishedAt,
slug,
breadcrumbs,
manualBreadcrumbs,
}: OwidGdocPostInterface & {
isPreviewing?: boolean
}) {
Expand Down Expand Up @@ -90,7 +91,7 @@ export function GdocPost({
<OwidGdocHeader
content={content}
publishedAt={publishedAt}
breadcrumbs={breadcrumbs ?? undefined}
breadcrumbs={manualBreadcrumbs ?? breadcrumbs ?? undefined}
isDeprecated={isDeprecated}
/>
{isDeprecated && content["deprecation-notice"] && (
Expand Down

0 comments on commit a41cde5

Please sign in to comment.