Skip to content

Commit f5012e0

Browse files
authored
Playground improvement (#41)
* Improvements for contextless * linter * CloseActivity page * fix import * tabs -> spaces * nvm
1 parent 107c444 commit f5012e0

13 files changed

+122
-21
lines changed

biome.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"**/dist",
88
"**/build",
99
"**/coverage",
10-
"**/lib"
10+
"**/lib",
11+
"**/.wrangler"
1112
]
1213
},
1314
"organizeImports": {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"license": "MIT",
1212
"scripts": {
1313
"fix": "biome check --write .",
14-
"lint": "biome check ."
14+
"lint": "biome check ."
1515
},
1616
"devDependencies": {
1717
"@biomejs/biome": "^1.8.3"

sdk-playground/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@
2424
npm-debug.log*
2525
yarn-debug.log*
2626
yarn-error.log*
27+
28+
.idea

sdk-playground/example.env

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ VITE_DISCORD_API_BASE=https://discord.com/api
99

1010
CLIENT_SECRET=secret # This should be the oauth2 token for your application from the developer portal.
1111
BOT_TOKEN=bottoken # This should be the bot token for your application from the developer portal.
12+
WEBAPP_SERVE_PORT=3000

sdk-playground/packages/client/src/App.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import SetActivity from './pages/SetActivity';
4040
import UserSettingsGetLocale from './pages/UserSettingsGetLocale';
4141
import Search from './components/Search';
4242
import {useState} from 'react';
43+
import GetActivityInstance from "./pages/GetActivityInstance";
44+
import CloseActivity from "./pages/CloseActivity";
4345

4446
// Add contexts here
4547
export default function App(): React.ReactElement {
@@ -71,6 +73,11 @@ const routes: Record<string, AppRoute> = {
7173
name: 'Encourage Hardware Acceleration',
7274
component: EncourageHardwareAcceleration,
7375
},
76+
closeActivity: {
77+
path: '/close-activity',
78+
name: 'Close Activity',
79+
component: CloseActivity,
80+
},
7481
getChannel: {
7582
path: '/get-channel',
7683
name: 'Get Channel',
@@ -91,6 +98,11 @@ const routes: Record<string, AppRoute> = {
9198
name: 'Get Instance Connected Participants',
9299
component: GetInstanceConnectedParticipants,
93100
},
101+
getActivityInstance: {
102+
path: '/get-activity-instance',
103+
name: 'Get Activity Instance',
104+
component: GetActivityInstance,
105+
},
94106
getPlatformBehaviors: {
95107
path: '/get-platform-behaviors',
96108
name: 'Get Platform Behaviors',

sdk-playground/packages/client/src/actions/authActions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const start = async () => {
4343
'rpc.voice.write',
4444
'rpc.voice.read',
4545
// "webhook.incoming",
46-
discordSdk.guildId == null ? 'dm_channels.read' : null, // This scope requires approval from Discord.
46+
// discordSdk.guildId == null ? 'dm_channels.read' : null, // This scope requires approval from Discord.
4747
].filter((scope) => scope != null) as Types.OAuthScopes[],
4848
});
4949

sdk-playground/packages/client/src/components/AuthProvider.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export function AuthProvider({children}: {children: React.ReactNode}) {
77
const auth = authStore();
88

99
React.useEffect(() => {
10-
start();
10+
start().catch(e => {
11+
console.error('Error starting auth', e);
12+
})
1113
}, []);
1214

1315
if (auth.user == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
3+
import discordSdk from '../discordSdk';
4+
import { RPCCloseCodes } from '@discord/embedded-app-sdk';
5+
6+
export default function CloseActivity() {
7+
React.useEffect(() => {
8+
discordSdk.close(RPCCloseCodes.CLOSE_NORMAL, 'Activity closed');
9+
}, []);
10+
11+
return (
12+
<div style={{padding: 32}}>
13+
<div>
14+
<h1>Close the Activity</h1>
15+
</div>
16+
</div>
17+
);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import discordSdk from '../discordSdk';
3+
import ReactJsonView from '../components/ReactJsonView';
4+
import {authStore} from '../stores/authStore';
5+
6+
export default function GetActivityInstance() {
7+
const [instance, setInstance] = React.useState<any | null>(null);
8+
const auth = authStore();
9+
10+
React.useEffect(() => {
11+
async function update() {
12+
if (!auth) {
13+
return;
14+
}
15+
const instanceResponse = await fetch(`/api/activity-instance/${discordSdk.instanceId}`);
16+
setInstance(await instanceResponse.json());
17+
}
18+
update();
19+
}, [auth]);
20+
21+
return (
22+
<div style={{padding: 32}}>
23+
<div>
24+
<h1>Current Validated Instance</h1>
25+
<br />
26+
<br />
27+
{instance ? <ReactJsonView src={instance} /> : null}
28+
</div>
29+
</div>
30+
);
31+
}

sdk-playground/packages/client/src/pages/SetActivity.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export default function SetActivity() {
1515
small_image: fillerUrl,
1616
large_image: fillerUrl,
1717
},
18+
secrets: {
19+
join: discordSdk.instanceId,
20+
},
21+
party: {
22+
id: 'foo',
23+
size: [1, 10]
24+
}
1825
},
1926
});
2027
}, []);
+20-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import react from '@vitejs/plugin-react';
2-
import { defineConfig } from 'vite';
2+
import { defineConfig, loadEnv } from 'vite';
33

44
// https://vitejs.dev/config/
5-
export default defineConfig({
6-
plugins: [react()],
7-
envDir: '../../',
8-
server: {
9-
port: 3000,
10-
proxy: {
11-
'/api': {
12-
target: 'http://localhost:8787',
13-
changeOrigin: true,
14-
secure: false,
15-
ws: true,
16-
// rewrite: (path) => path.replace(/^\/api/, ""),
5+
export default defineConfig(({ mode }) => {
6+
const env = loadEnv(mode, '../../', '');
7+
return {
8+
plugins: [react()],
9+
envDir: '../../',
10+
server: {
11+
port: Number.parseInt(env.WEBAPP_SERVE_PORT),
12+
proxy: {
13+
'/api': {
14+
target: 'http://localhost:8787',
15+
changeOrigin: true,
16+
secure: false,
17+
ws: true,
18+
// rewrite: (path) => path.replace(/^\/api/, ""),
19+
},
20+
},
21+
hmr: {
22+
clientPort: 443,
1723
},
1824
},
19-
hmr: {
20-
clientPort: 443,
21-
},
22-
},
25+
};
2326
});

sdk-playground/packages/server/src/handleApiRequest.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import getActivityInstanceHandler from './handlers/getActivityInstanceHandler';
12
import iapHandler from './handlers/iapHandler';
23
import tokenHandler from './handlers/tokenHandler';
34
import type { Env } from './types';
@@ -9,6 +10,8 @@ export function handleApiRequest(path: string[], request: Request, env: Env) {
910
return tokenHandler(path, request, env);
1011
case 'iap':
1112
return iapHandler(path, request, env);
13+
case 'activity-instance':
14+
return getActivityInstanceHandler(path, request, env);
1215
default:
1316
return new Response('Not found', { status: 404 });
1417
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Env, IGetOAuthToken } from '../types';
2+
import { readRequestBody, requestHeaders } from '../utils';
3+
4+
export default async function getActivityInstanceHandler(
5+
path: string[],
6+
request: Request,
7+
env: Env,
8+
) {
9+
try {
10+
const instanceId = path[1];
11+
return await fetch(
12+
`${env.VITE_DISCORD_API_BASE}/applications/${env.VITE_CLIENT_ID}/activity-instances/${instanceId}`,
13+
{
14+
headers: requestHeaders(env),
15+
},
16+
);
17+
} catch (ex) {
18+
console.error(ex);
19+
return new Response(`Internal Error: ${ex}`, { status: 500 });
20+
}
21+
}

0 commit comments

Comments
 (0)