feat(PBI-76): migration helper v2 — handle legacy cookies

Bumps marker version to 'v2'. buildMigrationPatch now also scans
document.cookie for `sp:*` (split-pane positions) and
`active_sprint_*` (active sprint per product) and lifts them into
layout.splitPanePositions / layout.activeSprints. clearLegacyStorage
replaces clearLegacyLocalStorage and clears both keys and cookies.
clearLegacyLocalStorage stays as a deprecated alias so the bridge
upgrade is a single rename.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-05-10 15:25:46 +02:00
parent 6212a3232c
commit 58dcb03420
3 changed files with 130 additions and 19 deletions

View file

@ -8,14 +8,28 @@
import type { UserSettings } from './user-settings'
const MIGRATION_MARKER = 'scrum4me:settings_migrated'
const CURRENT_VERSION = 'v1'
const CURRENT_VERSION = 'v2'
export interface MigrationResult {
patch: Partial<UserSettings>
legacyKeys: string[]
legacyCookies: string[]
hasData: boolean
}
function readCookies(): Record<string, string> {
if (typeof document === 'undefined') return {}
const out: Record<string, string> = {}
for (const part of document.cookie.split(';')) {
const eq = part.indexOf('=')
if (eq < 0) continue
const key = part.slice(0, eq).trim()
const val = part.slice(eq + 1).trim()
if (key) out[key] = val
}
return out
}
function readJsonArray(key: string): string[] | null {
const raw = localStorage.getItem(key)
if (!raw) return null
@ -54,13 +68,20 @@ function setIfNotNull<T>(target: Record<string, unknown>, key: string, value: T
}
export function buildMigrationPatch(): MigrationResult {
const empty: MigrationResult = { patch: {}, legacyKeys: [], hasData: false }
const empty: MigrationResult = {
patch: {},
legacyKeys: [],
legacyCookies: [],
hasData: false,
}
if (typeof window === 'undefined') return empty
if (localStorage.getItem(MIGRATION_MARKER) === CURRENT_VERSION) return empty
const patch: Partial<UserSettings> = {}
const views: NonNullable<UserSettings['views']> = {}
const layout: NonNullable<UserSettings['layout']> = {}
const legacyKeys: string[] = []
const legacyCookies: string[] = []
let hasData = false
// sprint_pb_*
@ -206,10 +227,48 @@ export function buildMigrationPatch(): MigrationResult {
hasData = true
}
return { patch, legacyKeys, hasData }
// layout from cookies (Phase 2)
const cookies = readCookies()
const splitPanePositions: Record<string, number[]> = {}
const activeSprints: Record<string, string> = {}
for (const [name, rawValue] of Object.entries(cookies)) {
if (name.startsWith('sp:')) {
const key = name.slice(3)
try {
const arr = JSON.parse(decodeURIComponent(rawValue))
if (
Array.isArray(arr) &&
arr.every((n) => typeof n === 'number') &&
Math.abs(arr.reduce((a, b) => a + b, 0) - 100) <= 1
) {
splitPanePositions[key] = arr as number[]
legacyCookies.push(name)
}
} catch {
// ignore malformed cookie
}
} else if (name.startsWith('active_sprint_') && rawValue) {
const productId = name.slice('active_sprint_'.length)
activeSprints[productId] = decodeURIComponent(rawValue)
legacyCookies.push(name)
}
}
if (Object.keys(splitPanePositions).length > 0) {
layout.splitPanePositions = splitPanePositions
hasData = true
}
if (Object.keys(activeSprints).length > 0) {
layout.activeSprints = activeSprints
hasData = true
}
if (Object.keys(layout).length > 0) {
patch.layout = layout
}
return { patch, legacyKeys, legacyCookies, hasData }
}
export function clearLegacyLocalStorage(keys: string[]): void {
export function clearLegacyStorage(keys: string[], cookies: string[] = []): void {
if (typeof window === 'undefined') return
for (const k of keys) {
try {
@ -218,9 +277,20 @@ export function clearLegacyLocalStorage(keys: string[]): void {
// storage quota exceeded or disabled — ignore
}
}
for (const c of cookies) {
try {
document.cookie = `${c}=; max-age=0; path=/; samesite=lax`
} catch {
// ignore
}
}
try {
localStorage.setItem(MIGRATION_MARKER, CURRENT_VERSION)
} catch {
// ignore
}
}
/** @deprecated use clearLegacyStorage */
export const clearLegacyLocalStorage = (keys: string[]) =>
clearLegacyStorage(keys, [])