Skip to content

Commit

Permalink
Playground improvement (#41)
Browse files Browse the repository at this point in the history
* Improvements for contextless

* linter

* CloseActivity page

* fix import

* tabs -> spaces

* nvm
  • Loading branch information
yonilerner authored Dec 2, 2024
1 parent 107c444 commit f5012e0
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 21 deletions.
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"**/dist",
"**/build",
"**/coverage",
"**/lib"
"**/lib",
"**/.wrangler"
]
},
"organizeImports": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "MIT",
"scripts": {
"fix": "biome check --write .",
"lint": "biome check ."
"lint": "biome check ."
},
"devDependencies": {
"@biomejs/biome": "^1.8.3"
Expand Down
2 changes: 2 additions & 0 deletions sdk-playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.idea
1 change: 1 addition & 0 deletions sdk-playground/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ VITE_DISCORD_API_BASE=https://discord.com/api

CLIENT_SECRET=secret # This should be the oauth2 token for your application from the developer portal.
BOT_TOKEN=bottoken # This should be the bot token for your application from the developer portal.
WEBAPP_SERVE_PORT=3000
12 changes: 12 additions & 0 deletions sdk-playground/packages/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import SetActivity from './pages/SetActivity';
import UserSettingsGetLocale from './pages/UserSettingsGetLocale';
import Search from './components/Search';
import {useState} from 'react';
import GetActivityInstance from "./pages/GetActivityInstance";
import CloseActivity from "./pages/CloseActivity";

// Add contexts here
export default function App(): React.ReactElement {
Expand Down Expand Up @@ -71,6 +73,11 @@ const routes: Record<string, AppRoute> = {
name: 'Encourage Hardware Acceleration',
component: EncourageHardwareAcceleration,
},
closeActivity: {
path: '/close-activity',
name: 'Close Activity',
component: CloseActivity,
},
getChannel: {
path: '/get-channel',
name: 'Get Channel',
Expand All @@ -91,6 +98,11 @@ const routes: Record<string, AppRoute> = {
name: 'Get Instance Connected Participants',
component: GetInstanceConnectedParticipants,
},
getActivityInstance: {
path: '/get-activity-instance',
name: 'Get Activity Instance',
component: GetActivityInstance,
},
getPlatformBehaviors: {
path: '/get-platform-behaviors',
name: 'Get Platform Behaviors',
Expand Down
2 changes: 1 addition & 1 deletion sdk-playground/packages/client/src/actions/authActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const start = async () => {
'rpc.voice.write',
'rpc.voice.read',
// "webhook.incoming",
discordSdk.guildId == null ? 'dm_channels.read' : null, // This scope requires approval from Discord.
// discordSdk.guildId == null ? 'dm_channels.read' : null, // This scope requires approval from Discord.
].filter((scope) => scope != null) as Types.OAuthScopes[],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export function AuthProvider({children}: {children: React.ReactNode}) {
const auth = authStore();

React.useEffect(() => {
start();
start().catch(e => {
console.error('Error starting auth', e);
})
}, []);

if (auth.user == null) {
Expand Down
18 changes: 18 additions & 0 deletions sdk-playground/packages/client/src/pages/CloseActivity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';

import discordSdk from '../discordSdk';
import { RPCCloseCodes } from '@discord/embedded-app-sdk';

export default function CloseActivity() {
React.useEffect(() => {
discordSdk.close(RPCCloseCodes.CLOSE_NORMAL, 'Activity closed');
}, []);

return (
<div style={{padding: 32}}>
<div>
<h1>Close the Activity</h1>
</div>
</div>
);
}
31 changes: 31 additions & 0 deletions sdk-playground/packages/client/src/pages/GetActivityInstance.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import discordSdk from '../discordSdk';
import ReactJsonView from '../components/ReactJsonView';
import {authStore} from '../stores/authStore';

export default function GetActivityInstance() {
const [instance, setInstance] = React.useState<any | null>(null);
const auth = authStore();

React.useEffect(() => {
async function update() {
if (!auth) {
return;
}
const instanceResponse = await fetch(`/api/activity-instance/${discordSdk.instanceId}`);
setInstance(await instanceResponse.json());
}
update();
}, [auth]);

return (
<div style={{padding: 32}}>
<div>
<h1>Current Validated Instance</h1>
<br />
<br />
{instance ? <ReactJsonView src={instance} /> : null}
</div>
</div>
);
}
7 changes: 7 additions & 0 deletions sdk-playground/packages/client/src/pages/SetActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export default function SetActivity() {
small_image: fillerUrl,
large_image: fillerUrl,
},
secrets: {
join: discordSdk.instanceId,
},
party: {
id: 'foo',
size: [1, 10]
}
},
});
}, []);
Expand Down
37 changes: 20 additions & 17 deletions sdk-playground/packages/client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import { defineConfig, loadEnv } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
envDir: '../../',
server: {
port: 3000,
proxy: {
'/api': {
target: 'http://localhost:8787',
changeOrigin: true,
secure: false,
ws: true,
// rewrite: (path) => path.replace(/^\/api/, ""),
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, '../../', '');
return {
plugins: [react()],
envDir: '../../',
server: {
port: Number.parseInt(env.WEBAPP_SERVE_PORT),
proxy: {
'/api': {
target: 'http://localhost:8787',
changeOrigin: true,
secure: false,
ws: true,
// rewrite: (path) => path.replace(/^\/api/, ""),
},
},
hmr: {
clientPort: 443,
},
},
hmr: {
clientPort: 443,
},
},
};
});
3 changes: 3 additions & 0 deletions sdk-playground/packages/server/src/handleApiRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getActivityInstanceHandler from './handlers/getActivityInstanceHandler';
import iapHandler from './handlers/iapHandler';
import tokenHandler from './handlers/tokenHandler';
import type { Env } from './types';
Expand All @@ -9,6 +10,8 @@ export function handleApiRequest(path: string[], request: Request, env: Env) {
return tokenHandler(path, request, env);
case 'iap':
return iapHandler(path, request, env);
case 'activity-instance':
return getActivityInstanceHandler(path, request, env);
default:
return new Response('Not found', { status: 404 });
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Env, IGetOAuthToken } from '../types';
import { readRequestBody, requestHeaders } from '../utils';

export default async function getActivityInstanceHandler(
path: string[],
request: Request,
env: Env,
) {
try {
const instanceId = path[1];
return await fetch(
`${env.VITE_DISCORD_API_BASE}/applications/${env.VITE_CLIENT_ID}/activity-instances/${instanceId}`,
{
headers: requestHeaders(env),
},
);
} catch (ex) {
console.error(ex);
return new Response(`Internal Error: ${ex}`, { status: 500 });
}
}

0 comments on commit f5012e0

Please sign in to comment.