Skip to content
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
30 changes: 30 additions & 0 deletions src/static/js/pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,35 @@ 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;
const deleted = new Set([padName, pad.getPadId()]);
// Entries written by older versions may hold a URL-encoded name; normalize
// the same way the welcome screen does before comparing.
const decodeName = (name) => {
try {
return decodeURIComponent(name);
} catch {
return name;
}
};
localStorage.setItem('recentPads', JSON.stringify(recentPads.filter(
(p) => p == null || !(deleted.has(p.name) || deleted.has(decodeName(p.name))))));
} 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 +505,7 @@ const handshake = async () => {
}

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

Expand Down
86 changes: 86 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,86 @@
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('legacy URL-encoded recent pad entries are removed too', async ({page}) => {
const padId = `FRONTEND_TESTS_notes&ideas_${randomUUID().slice(0, 8)}`;
await page.goto(`http://localhost:9001/p/${encodeURIComponent(padId)}`);
await page.waitForSelector('#editorcontainer.initialized');
await page.locator('#deletiontoken-ack').click();
// Older versions stored the encoded name; seed such an entry alongside the
// decoded one the current client writes.
await page.evaluate((legacyName) => {
const pads = JSON.parse(window.localStorage.getItem('recentPads') || '[]');
pads.push({name: legacyName, timestamp: new Date(0).toISOString(), members: 1});
window.localStorage.setItem('recentPads', JSON.stringify(pads));
}, encodeURIComponent(padId));

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

const remaining = await recentPadNames(page);
expect(remaining).not.toContain(padId);
expect(remaining).not.toContain(encodeURIComponent(padId));
});

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