Skip to content

Commit

Permalink
Merge pull request #257 from Avasam/develop
Browse files Browse the repository at this point in the history
Support old view/register for now
  • Loading branch information
Avasam authored Jul 1, 2021
2 parents e39b145 + 8beba83 commit f51b522
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
13 changes: 13 additions & 0 deletions tournament-scheduler/src/Components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { apiGet } from 'src/fetchers/Api'
import type User from 'src/Models/User'
import darkTheme from 'src/styles/dark.theme'
import lightTheme from 'src/styles/light.theme'
import copyToClipboard from 'src/utils/Clipboard'

type Themes = 'dark' | 'light'
type StylesOverrides = { body: { background: string } }
Expand All @@ -32,6 +33,18 @@ const logout = (setCurrentUser: (user: null) => void) => {
}

const App = () => {
/* eslint-disable react-hooks/rules-of-hooks */
for (const param of ['register', 'view']) {
const oldParamValue = new URLSearchParams(window.location.search).get(param)
console.log(oldParamValue, typeof oldParamValue)
if (oldParamValue != null) {
const newLink = `${window.location.origin}${window.process.env.PUBLIC_URL}/${param}/${oldParamValue}`
console.warn(`Old ${param} param found in URL, redirecting to:`, newLink)
copyToClipboard(newLink).finally(() => window.location.href = newLink)
return <></>
}
}

const isMobileSize = useMediaQuery('(max-width:640px)', { noSsr: true })
const [currentUser, setCurrentUser] = useState<User | null | undefined>()
const location = useLocation()
Expand Down
47 changes: 25 additions & 22 deletions tournament-scheduler/src/utils/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,40 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
const MAX_SELECTION = 99_999

const oldCopyToClipboard = (text: string) => {
const textArea = document.createElement('textarea')
textArea.value = text
textArea.style.position = 'fixed' // Avoid scrolling to bottom
document.body.append(textArea)
textArea.focus()
textArea.select()
textArea.setSelectionRange(0, MAX_SELECTION) // For mobile devices
const oldCopyToClipboard = (text: string) =>
new Promise<void>((resolve, reject) => {
const textArea = document.createElement('textarea')
textArea.value = text
textArea.style.position = 'fixed' // Avoid scrolling to bottom
document.body.append(textArea)
textArea.focus()
textArea.select()
textArea.setSelectionRange(0, MAX_SELECTION) // For mobile devices

try {
const successful = document.execCommand('copy')
if (!successful) throw new Error('execCommand failed')
console.info('text copied to clipboard successfully using textarea')
// @ts-expect-error TypeScript should allow error type on catch https://github.com/Microsoft/TypeScript/issues/20024
} catch (err: Error) {
alert(`Could not copy text: ${err}`)
console.error('Could not copy text using textarea:', err)
}
try {
const successful = document.execCommand('copy')
if (!successful) throw new Error('execCommand failed')
console.info('text copied to clipboard successfully using textarea')
// https://github.com/Microsoft/TypeScript/issues/20024
// @ts-expect-error TypeScript should allow error type on catch
} catch (err: Error) {
alert(`Could not copy text: ${err}`)
console.error('Could not copy text using textarea:', err)
reject()
}

textArea.remove()
}
textArea.remove()
resolve()
})

const copyToClipboard = (text: string) => {
// Note: navigator.clipboard may not exist on some devices
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!navigator.clipboard) {
oldCopyToClipboard(text)
return
return oldCopyToClipboard(text)
}

navigator.clipboard.writeText(text).then(
return navigator.clipboard.writeText(text).then(
() => console.info('text copied to clipboard successfully'),
err => {
alert(`Could not copy text: ${err}`)
Expand Down

0 comments on commit f51b522

Please sign in to comment.