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

Change WebArchiveLink component to simplify snapshot URL fetching #3334

Merged
Merged
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
8 changes: 6 additions & 2 deletions site/gatsby-site/playwright/e2e/discover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ test.describe('The Discover app', () => {
await expect(modal).not.toBeVisible();
});

test.skip('Opens an archive link', async ({ page, skipOnEmptyEnvironment }) => {
test('Opens an archive link', async ({ page, skipOnEmptyEnvironment }) => {

test.slow();

Expand Down Expand Up @@ -278,9 +278,13 @@ test.describe('The Discover app', () => {
await expect(waybackMachineLink).toBeVisible();
await waybackMachineLink.click();

const response = await (await fetch('https://archive.org/wayback/available?url=https://www.cbsnews.com/news/is-starbucks-shortchanging-its-baristas/')).json();

const expectedTimestamp = response.archived_snapshots.closest.timestamp;

await expect(async () => {
await expect(popup).toBeTruthy();
await expect(popup).toHaveURL('https://web.archive.org/web/20240404174436/https://www.cbsnews.com/news/is-starbucks-shortchanging-its-baristas/');
await expect(popup).toHaveURL(`https://web.archive.org/web/${expectedTimestamp}/https://www.cbsnews.com/news/is-starbucks-shortchanging-its-baristas/`);
}).toPass();
});

Expand Down
10 changes: 1 addition & 9 deletions site/gatsby-site/src/components/discover/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ export default function Actions({ item, toggleFilterByIncidentId = null }) {

return (
<div className="flex flex-wrap">
<WebArchiveLink
url={item.url}
date={item.date_submitted}
className="btn btn-link px-1"
title={t('Authors')}
datePublished={item.epoch_date_published}
incidentDate={item.epoch_incident_date}
dateSubmitted={item.epoch_date_submitted}
>
<WebArchiveLink url={item.url} className="btn btn-link px-1">
<FontAwesomeIcon
titleId="report-source"
icon={faNewspaper}
Expand Down
10 changes: 4 additions & 6 deletions site/gatsby-site/src/components/ui/WebArchiveLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ import React from 'react';
import { Trans } from 'react-i18next';
import { Dropdown } from 'flowbite-react';

async function getSnapshotURL(url, date) {
const timestamp = date ? date.replace('-', '') : '';

const waUrl = `https://archive.org/wayback/available?url=${url}&timestamp=${timestamp}`;
async function getSnapshotURL(url) {
const waUrl = `https://archive.org/wayback/available?url=${url}`;

const response = await (await fetch(waUrl)).json();

return response.archived_snapshots.closest.url.replace('http:', 'https:');
}

export default function WebArchiveLink({ url, date, children, className = '' }) {
export default function WebArchiveLink({ url, children, className = '' }) {
const onClick = async () => {
const win = window.open('', '_blank');

try {
const snapshotUrl = await getSnapshotURL(url, date);
const snapshotUrl = await getSnapshotURL(url);

win.location.href = snapshotUrl;
} catch (e) {
Expand Down
Loading