Show share buttons on challenge; use navigator.share

pull/63/head
Lynn 2022-01-24 16:27:10 +01:00
rodzic c7b8a745ed
commit 853671b4eb
1 zmienionych plików z 29 dodań i 26 usunięć

Wyświetl plik

@ -98,18 +98,21 @@ function Game(props: GameProps) {
setGameNumber((x) => x + 1); setGameNumber((x) => x + 1);
}; };
function copyToClipboard(text: string, successHint: string) { async function share(url: string, copiedHint: string, text?: string) {
if (!navigator.clipboard) { try {
setHint(text); await navigator.share({ url, text });
} else { return;
navigator.clipboard } catch (e) {
.writeText(text) console.warn("navigator.share failed:", e);
.then(() => { try {
setHint(successHint); const body = url + (text ? "\n\n" + text : "");
}) await navigator.clipboard.writeText(body);
.catch(() => { setHint(copiedHint);
setHint(text); return;
}); } catch (e2) {
console.warn("navigator.clipboard.writeText failed:", e2);
setHint(url);
}
} }
} }
@ -273,11 +276,11 @@ function Game(props: GameProps) {
{hint || `\u00a0`} {hint || `\u00a0`}
</p> </p>
<Keyboard letterInfo={letterInfo} onKey={onKey} /> <Keyboard letterInfo={letterInfo} onKey={onKey} />
{gameState !== GameState.Playing && !challenge && ( {gameState !== GameState.Playing && (
<p> <p>
<button <button
onClick={() => { onClick={() => {
copyToClipboard( share(
getChallengeUrl(target), getChallengeUrl(target),
"Challenge link copied to clipboard!" "Challenge link copied to clipboard!"
); );
@ -285,23 +288,23 @@ function Game(props: GameProps) {
> >
Challenge a friend to this word Challenge a friend to this word
</button> </button>
{" "}
<button <button
onClick={() => { onClick={() => {
copyToClipboard( share(
getChallengeUrl(target) + getChallengeUrl(target),
"\n\n" + "Result copied to clipboard!",
guesses guesses
.map((guess) => .map((guess) =>
clue(guess, target) clue(guess, target)
.map((c) => ["⬛", "🟨", "🟩"][c.clue ?? 0]) .map((c) => ["⬛", "🟨", "🟩"][c.clue ?? 0])
.join("") .join("")
) )
.join("\n"), .join("\n")
"Result copied to clipboard!"
); );
}} }}
> >
Copy result as emoji Challenge with emoji results
</button> </button>
</p> </p>
)} )}