Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions src/static/js/pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ const showDeletionTokenModalIfPresent = () => {
});
};

// Drop the current pad from the "Recent pads" list shown on the welcome
// screen once the server reports it deleted, so the homepage no longer offers
// a link that would silently recreate it (issue #8201). The name is derived
// from the URL path the same way the entry was stored.
const forgetRecentPad = () => {
try {
const stored = localStorage.getItem('recentPads');
if (stored == null) return;
const pathSegments = window.location.pathname.split('/');
const padName = decodeURIComponent(pathSegments[pathSegments.length - 1]);
const recentPads = JSON.parse(stored);
if (!Array.isArray(recentPads)) return;
localStorage.setItem('recentPads', JSON.stringify(
recentPads.filter((p) => p == null || (p.name !== padName && p.name !== pad.getPadId()))));
Comment thread
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
Outdated
} catch (err) {
console.warn('Unable to update recent pads after pad deletion', err);
}
};

const sendClientReady = (isReconnect) => {
let padId = document.location.pathname.substring(document.location.pathname.lastIndexOf('/') + 1);
// unescape necessary due to Safari and Opera interpretation of spaces
Expand Down Expand Up @@ -476,6 +495,7 @@ const handshake = async () => {
}

} else if (obj.disconnect) {
if (obj.disconnect === 'deleted') forgetRecentPad();
padconnectionstatus.disconnected(obj.disconnect);
socket.disconnect();

Expand Down
63 changes: 63 additions & 0 deletions src/tests/frontend-new/specs/recent_pads_delete.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {expect, test, Page} from '@playwright/test';
import {randomUUID} from 'node:crypto';
import {goToNewPad, goToPad} from '../helper/padHelper';
import {showSettings} from '../helper/settingsHelper';

// Issue #8201: after deleting a pad, the welcome screen kept listing it under
// "Recent pads" because the entry in localStorage was never removed.

const recentPadNames = async (page: Page): Promise<string[]> => page.evaluate(
() => JSON.parse(window.localStorage.getItem('recentPads') || '[]')
.map((p: {name: string}) => p.name));

const waitForHome = async (page: Page) => page.waitForURL(
(url) => url.pathname === '/' || url.pathname.endsWith('/index.html'), {timeout: 10000});

test.describe('recent pads after pad deletion', () => {
test.beforeEach(async ({context}) => {
await context.clearCookies();
});

test('creator deleting a pad removes it from the welcome screen', async ({page}) => {
const padId = await goToNewPad(page);
expect(await recentPadNames(page)).toContain(padId);

await showSettings(page);
page.once('dialog', (d) => d.accept());
await page.locator('#delete-pad').click();
await waitForHome(page);

expect(await recentPadNames(page)).not.toContain(padId);
await expect(page.locator('.recent-pad a', {hasText: padId})).toHaveCount(0);
});

test('deleting with a token on a second device removes it from that device', async ({
page, browser,
}) => {
const padId = `FRONTEND_TESTS${randomUUID()}`;
await page.goto(`http://localhost:9001/p/${padId}`);
await page.waitForSelector('#editorcontainer.initialized');
const token = await page.locator('#deletiontoken-value').inputValue();
await page.locator('#deletiontoken-ack').click();

const context2 = await browser.newContext();
const page2 = await context2.newPage();
await goToPad(page2, padId);
expect(await recentPadNames(page2)).toContain(padId);

await showSettings(page2);
await page2.locator('#delete-pad-with-token > summary').click();
await page2.locator('#delete-pad-token-input').fill(token);
page2.once('dialog', (d) => d.accept());
await page2.locator('#delete-pad-token-submit').click();
await waitForHome(page2);

expect(await recentPadNames(page2)).not.toContain(padId);
await expect(page2.locator('.recent-pad a', {hasText: padId})).toHaveCount(0);
// The creator's still-open tab was disconnected with reason "deleted" and
// must also forget the pad.
await expect.poll(() => recentPadNames(page)).not.toContain(padId);

await context2.close();
});
});
Loading