From 43e74fde8510c8c2125cebe8e04279b0ed604084 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Tue, 7 May 2024 13:56:30 +0100 Subject: [PATCH] Handle pasting join link --- client/src/HostOrJoinGameDialog.tsx | 21 +++++++++++++++++++++ e2e/online.spec.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/client/src/HostOrJoinGameDialog.tsx b/client/src/HostOrJoinGameDialog.tsx index 00e5675..5e2dfe5 100644 --- a/client/src/HostOrJoinGameDialog.tsx +++ b/client/src/HostOrJoinGameDialog.tsx @@ -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 }) => ( setPromise(alternativeAction(gameID).then(onClose))} @@ -196,6 +216,7 @@ const BaseHostOrJoinGameDialog = ({ onInput={event => setGameID(sanitiseGameID((event.target as any).value)) } + onPaste={handlePaste} />