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
5 changes: 4 additions & 1 deletion src/frontend/shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export function parentPathForBack(pathname: string): string | null {
return null;
}

/** Redirect chapter preview paths to the story index before switching locale. */
/** Redirect unsafe paths to a locale-switchable parent before swapping locale. */
export function normalizePathForLanguageSwitch(pathname: string): string {
const segments = pathname.split('/').filter(Boolean);
if (
Expand All @@ -414,6 +414,9 @@ export function normalizePathForLanguageSwitch(pathname: string): string {
) {
return `/${segments.slice(0, 3).join('/')}`;
}
if (segments.length === 3 && segments[1] === 'story' && segments[2] === 'create') {
return `/${segments.slice(0, 2).join('/')}`;
}
return pathname;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/replaceLocaleInPath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,21 @@ test.describe('normalizePathForLanguageSwitch', () => {
expect(normalizePathForLanguageSwitch('/en/dashboard')).toBe('/en/dashboard');
});

test('redirects story create to story gallery', () => {
expect(normalizePathForLanguageSwitch('/en/story/create')).toBe('/en/story');
});

test('combined with replaceLocaleInPath redirects chapter to translated story index', () => {
const pathname = '/en/story/1/chapter/3';
const normalized = normalizePathForLanguageSwitch(pathname);
expect(replaceLocaleInPath(normalized, 'fr', locales)).toBe('/fr/story/1');
});

test('combined with replaceLocaleInPath redirects story create to translated story gallery', () => {
const pathname = '/en/story/create';
const normalized = normalizePathForLanguageSwitch(pathname);
expect(replaceLocaleInPath(normalized, 'fr', locales)).toBe('/fr/story');
});
});

test.describe('parentPathForBack', () => {
Expand Down
Loading