Skip to content

Commit

Permalink
POST migration redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
kognise committed Mar 21, 2023
1 parent 82f58e4 commit d99fdad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/lib/game-saving/legacy-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import { useEffect } from 'preact/hooks'
const helperUrl = 'https://editor.sprig.hackclub.com/migration-helper.html'

export const getPuzzleLabFromLocalStorage = (allowRedirect: boolean): Promise<string> => new Promise((resolve) => {
if (localStorage.getItem('puzzleLabHotfix') !== null)
return resolve(localStorage.getItem('puzzleLabHotfix')!)
if (sessionStorage.getItem('migratedPuzzleLab')) {
const puzzleLab = sessionStorage.getItem('migratedPuzzleLab')!
sessionStorage.removeItem('migratedPuzzleLab')
return resolve(puzzleLab)
}

// Query param is deprecated or something, we should maybe eventually remove it.
//
// (For context, before sessionStorage and a POST redirect was used, the value
// was passed as a query param.)
const params = new URLSearchParams(window.location.search)
if (params.get('puzzleLab') !== null) {
// Remove the query param, it's cleaner for the user and means
Expand Down
11 changes: 8 additions & 3 deletions src/pages/migration-redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import type { APIRoute } from 'astro'

export const post: APIRoute = async ({ request }) => {
let puzzleLab
let redirect
try {
const body = await request.formData()
puzzleLab = body.get('puzzle-lab')
puzzleLab = body.get('puzzleLab')
if (typeof puzzleLab !== 'string') throw 'Invalid form data'
redirect = body.get('redirect')
if (typeof redirect !== 'string' && redirect !== null) throw 'Invalid form data'
} catch (error) {
return new Response(typeof error === 'string' ? error : 'Bad request body', { status: 400 })
}

return new Response(`
<script>
const puzzleLab = ${JSON.stringify(puzzleLab)}
window.localStorage.setItem('puzzle-lab', puzzleLab)
window.location.replace('/migrate')
sessionStorage.setItem('migratedPuzzleLab', puzzleLab)
const search = new URLSearchParams(window.location.search)
window.location.replace(search.get('redirect') || '/migrate')
</script>
`, {
status: 200,
Expand Down

0 comments on commit d99fdad

Please sign in to comment.