Skip to content

Commit

Permalink
Handle pasting join link
Browse files Browse the repository at this point in the history
  • Loading branch information
12joan committed May 7, 2024
1 parent 3929241 commit 43e74fd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
21 changes: 21 additions & 0 deletions client/src/HostOrJoinGameDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ const BaseHostOrJoinGameDialog = ({
setPromise(primaryAction(gameID).then(onClose))
}

const handlePaste = (event: ClipboardEvent) => {
const text = event.clipboardData?.getData('text/plain')
if (!text) return

let pathname: string

try {
pathname = new URL(text).pathname
} catch {
console.warn('Pasted text is not a URL')
return
}

const match = pathname.match(/\/game\/([^/]+)/)
if (!match) return

event.preventDefault()
setGameID(sanitiseGameID(decodeURIComponent(match[1])))
}

const AlternativeActionButton = ({ children }: { children: string }) => (
<ButtonLink
onClick={() => setPromise(alternativeAction(gameID).then(onClose))}
Expand Down Expand Up @@ -196,6 +216,7 @@ const BaseHostOrJoinGameDialog = ({
onInput={event =>
setGameID(sanitiseGameID((event.target as any).value))
}
onPaste={handlePaste}
/>

<Button type="submit" disabled={gameExists !== expectedExists}>
Expand Down
27 changes: 26 additions & 1 deletion e2e/online.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, Page } from '@playwright/test'
import { test, expect, Page } from '@playwright/test'
import { makeRandomIdentifier } from '../client/src/randomIdentifier'
import {
expectCellToBe,
Expand Down Expand Up @@ -157,4 +157,29 @@ test.describe('Online', () => {
await expectCurrentTurn(page, 'cross')
await expectCellToBe(page, 3, 3, 'triangle')
})

test('Pasting a join link', async ({ page }) => {
await pressJoinGame(page)

const input = page.getByLabel('Game ID').locator('visible=true')

const pasteIntoInput = async (text: string) =>
input.evaluate((input, text) => {
const event = new ClipboardEvent('paste', {
clipboardData: new DataTransfer(),
})

event.clipboardData!.setData('text/plain', text)

input.dispatchEvent(event)
}, text)

await input.fill('before 1')
await pasteIntoInput('https://anything.com:1234/game/my-game-id')
await expect(input).toHaveValue('my-game-id')

await input.fill('before 2')
await pasteIntoInput('https://anything.com:1234/not-game/my-game-id')
await expect(input).toHaveValue('before-2')
})
})

0 comments on commit 43e74fd

Please sign in to comment.