Skip to content

Commit

Permalink
Always add tab param to embed URL
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyi committed Apr 9, 2024
1 parent c55a1df commit a4eb9c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ describe("urls", () => {
expect(grapher.canonicalUrl?.includes("country")).toBeFalsy()
})

it("embedUrl includes the tab param even if it's the default value", () => {
const grapher = new Grapher({
isPublished: true,
slug: "foo",
bakedGrapherURL: "/grapher",
tab: GrapherTabOption.map,
})
expect(grapher.embedUrl).toEqual("/grapher/foo?tab=map")
})

it("can upgrade legacy urls", () => {
expect(legacyToCurrentGrapherQueryParams("?year=2000")).toEqual({
time: "2000",
Expand Down
12 changes: 11 additions & 1 deletion packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2999,7 +2999,17 @@ export class Grapher
}

@computed get embedUrl(): string | undefined {
return this.manager?.embedDialogUrl ?? this.canonicalUrl
const url = this.manager?.embedDialogUrl ?? this.canonicalUrl
if (!url) return

// We want to preserve the tab in the embed URL so that if we change the
// default view of the chart, it won't change existing embeds.
// See https://github.com/owid/owid-grapher/issues/2805
let urlObj = Url.fromURL(url)
if (!urlObj.queryParams.tab) {
urlObj = urlObj.updateQueryParams({ tab: this.allParams.tab })
}
return urlObj.fullUrl
}

@computed get embedDialogAdditionalElements():
Expand Down
2 changes: 1 addition & 1 deletion packages/@ourworldindata/grapher/src/modal/EmbedModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class EmbedModal extends React.Component<EmbedModalProps> {
}

@computed private get codeSnippet(): string {
const url = this.manager.embedUrl ?? this.manager.canonicalUrl
const url = this.manager.embedUrl
return `<iframe src="${url}" loading="lazy" style="width: 100%; height: 600px; border: 0px none;"></iframe>`
}

Expand Down

0 comments on commit a4eb9c2

Please sign in to comment.