-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update OpenExternalUrl example (#37)
- Loading branch information
Showing
1 changed file
with
40 additions
and
9 deletions.
There are no files selected for viewing
49 changes: 40 additions & 9 deletions
49
sdk-playground/packages/client/src/pages/OpenExternalLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,52 @@ | ||
import discordSdk from '../discordSdk'; | ||
import * as React from "react"; | ||
import discordSdk from "../discordSdk"; | ||
|
||
enum LinkStatus { | ||
NOT_CLICKED = "NOT_CLICKED", | ||
CLICKED = "CLICKED", | ||
NOT_OPENED = "NOT_OPENED", | ||
OPENED = "OPENED", | ||
} | ||
|
||
// Note: we're still using the anchor tag, to ensure standard accessibility UX | ||
export default function OpenExternalLink() { | ||
const [linkStatus, setLinkStatus] = React.useState(LinkStatus.NOT_CLICKED); | ||
|
||
async function handleLinkClicked( | ||
e: React.MouseEvent<HTMLAnchorElement, MouseEvent> | ||
) { | ||
e.preventDefault(); | ||
setLinkStatus(LinkStatus.CLICKED); | ||
const { opened } = await discordSdk.commands.openExternalLink({ | ||
url: "https://google.com", | ||
}); | ||
if (opened) { | ||
setLinkStatus(LinkStatus.OPENED); | ||
} else { | ||
setLinkStatus(LinkStatus.NOT_OPENED); | ||
} | ||
} | ||
|
||
return ( | ||
<div style={{padding: 32}}> | ||
<div | ||
style={{ | ||
padding: 32, | ||
display: "grid", | ||
gridGap: 16, | ||
justifyItems: "start", | ||
}} | ||
> | ||
<h1>Open external link</h1> | ||
<p> | ||
Click here to go to google:{' '} | ||
<a | ||
href="https://google.com" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
discordSdk.commands.openExternalLink({url: 'https://google.com'}); | ||
}}> | ||
Click here to go to google:{" "} | ||
<a href="https://google.com" onClick={handleLinkClicked}> | ||
Google! | ||
</a> | ||
</p> | ||
<div>Link Status: {linkStatus}</div> | ||
<button onClick={() => setLinkStatus(LinkStatus.NOT_CLICKED)}> | ||
Reset Link Status | ||
</button> | ||
</div> | ||
); | ||
} |