Skip to content

Commit

Permalink
add support for discord activity build
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinfuchs committed Mar 24, 2024
1 parent 9013e89 commit a7ac510
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 91 deletions.
3 changes: 2 additions & 1 deletion embedg-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port 3000",
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@codemirror/lang-json": "^6.0.1",
"@codemirror/lint": "^6.2.0",
"@discord/embedded-app-sdk": "^1.0.2",
"@emoji-mart/react": "^1.1.1",
"@formkit/auto-animate": "^1.0.0-beta.6",
"@heroicons/react": "^2.0.17",
Expand Down
19 changes: 15 additions & 4 deletions embedg-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import RequestLoadingIndicator from "./components/RequestLoadingIndicator";
import ClearView from "./views/editor/clear";
import ShareView from "./views/editor/share";
import EditorSideNav from "./components/SideNav";
import ColoredTextToolView from "./views/tools/coloredText";
import ToolsView from "./views/tools";
import WebhookInfoToolView from "./views/tools/webhookInfo";
import ActivityLoadingScreen from "./components/ActivityLoadingScreen";
import "./util/activity";

const LazyJsonView = lazy(() => import("./views/editor/json"));
const LazyAssistantView = lazy(() => import("./views/editor/assisstant"));
Expand All @@ -30,6 +29,7 @@ function App() {
return (
<div className="h-[100dvh] w-[100dvw] overflow-y-auto">
<RequestLoadingIndicator />
<ActivityLoadingScreen />
<div className="flex h-full">
<EditorSideNav />
<Routes>
Expand Down Expand Up @@ -127,7 +127,18 @@ function App() {
}
/>

<Route path="*" element={<Navigate replace to="/editor" />} />
<Route
path="*"
element={
<Navigate
replace
to={{
pathname: "/editor",
search: location.search,
}}
/>
}
/>
</Routes>
</div>
<ToastContainer />
Expand Down
23 changes: 23 additions & 0 deletions embedg-app/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ const queryClient = new QueryClient({
});

export default queryClient;

// This is only used in Discord Activities to work around the lack of cookies
// We don't need to persist the token at all because we re-authenticate for every Activity session
let localSessionToken: string;

export function setLocalSessionToken(token: string) {
localSessionToken = token;
}

export function fetchApi(
input: RequestInfo,
init?: RequestInit
): Promise<Response> {
const headers = (init?.headers || {}) as Record<string, string>;
if (localSessionToken) {
headers.Authorization = localSessionToken;
}

return fetch(input, {
...init,
headers,
});
}
71 changes: 39 additions & 32 deletions embedg-app/src/api/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
UploadImageResponseWire,
} from "./wire";
import { handleApiResponse } from "./queries";
import { fetchApi } from "./client";

export function useAssistantGenerateMessageMutation() {
return useMutation(
Expand All @@ -45,7 +46,7 @@ export function useAssistantGenerateMessageMutation() {
req: AssistantGenerateMessageRequestWire;
guildId: string;
}) => {
return fetch(`/api/assistant/message?guild_id=${guildId}`, {
return fetchApi(`/api/assistant/message?guild_id=${guildId}`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -60,7 +61,7 @@ export function useAssistantGenerateMessageMutation() {

export function useSendMessageToChannelMutation() {
return useMutation((req: MessageSendToChannelRequestWire) => {
return fetch(`/api/send-message/channel`, {
return fetchApi(`/api/send-message/channel`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -72,7 +73,7 @@ export function useSendMessageToChannelMutation() {

export function useSendMessageToWebhookMutation() {
return useMutation((req: MessageSendToWebhookRequestWire) => {
return fetch(`/api/send-message/webhook`, {
return fetchApi(`/api/send-message/webhook`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -84,7 +85,7 @@ export function useSendMessageToWebhookMutation() {

export function useRestoreMessageFromWebhookMutation() {
return useMutation((req: MessageRestoreFromWebhookRequestWire) => {
return fetch(`/api/restore-message/webhook`, {
return fetchApi(`/api/restore-message/webhook`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -96,7 +97,7 @@ export function useRestoreMessageFromWebhookMutation() {

export function useRestoreMessageFromChannelMutation() {
return useMutation((req: MessageRestoreFromChannelRequestWire) => {
return fetch(`/api/restore-message/channel`, {
return fetchApi(`/api/restore-message/channel`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -120,7 +121,7 @@ export function useCreatedSavedMessageMutation() {
url += `?guild_id=${guildId}`;
}

return fetch(url, {
return fetchApi(url, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand Down Expand Up @@ -149,7 +150,7 @@ export function useUpdateSavedMessageMutation() {
url += `?guild_id=${guildId}`;
}

return fetch(url, {
return fetchApi(url, {
method: "PUT",
body: JSON.stringify(req),
headers: {
Expand All @@ -170,7 +171,7 @@ export function useDeleteSavedMessageMutation() {
url += `?guild_id=${guildId}`;
}

return fetch(url, {
return fetchApi(url, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Expand All @@ -196,7 +197,7 @@ export function useImportSavedMessagesMutation() {
url += `?guild_id=${guildId}`;
}

return fetch(url, {
return fetchApi(url, {
method: "PATCH",
body: JSON.stringify(req),
headers: {
Expand All @@ -211,7 +212,7 @@ export function useImportSavedMessagesMutation() {

export function useSharedMessageCreateMutation() {
return useMutation((req: SharedMessageCreateRequestWire) => {
return fetch("/api/shared-messages", {
return fetchApi("/api/shared-messages", {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -232,7 +233,7 @@ export function useCustomBotConfigureMutation() {
guildId: string;
req: CustomBotConfigureRequestWire;
}) => {
return fetch(`/api/custom-bot?guild_id=${guildId}`, {
return fetchApi(`/api/custom-bot?guild_id=${guildId}`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -247,7 +248,7 @@ export function useCustomBotConfigureMutation() {

export function useCustomBotDisableMutation() {
return useMutation(({ guildId }: { guildId: string }) => {
return fetch(`/api/custom-bot?guild_id=${guildId}`, {
return fetchApi(`/api/custom-bot?guild_id=${guildId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Expand All @@ -267,7 +268,7 @@ export function useCustomBotUpdatePresenceMutation() {
guildId: string;
req: CustomBotUpdatePresenceRequestWire;
}) => {
return fetch(`/api/custom-bot/presence?guild_id=${guildId}`, {
return fetchApi(`/api/custom-bot/presence?guild_id=${guildId}`, {
method: "PUT",
body: JSON.stringify(req),
headers: {
Expand All @@ -289,7 +290,7 @@ export function useCustomCommandCreateMutation() {
guildId: string;
req: CustomCommandCreateRequestWire;
}) => {
return fetch(`/api/custom-bot/commands?guild_id=${guildId}`, {
return fetchApi(`/api/custom-bot/commands?guild_id=${guildId}`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -313,7 +314,7 @@ export function useCustomCommandUpdateMutation() {
guildId: string;
req: CustomCommandUpdateRequestWire;
}) => {
return fetch(
return fetchApi(
`/api/custom-bot/commands/${commandId}?guild_id=${guildId}`,
{
method: "PUT",
Expand All @@ -331,7 +332,7 @@ export function useCustomCommandUpdateMutation() {

export function useCustomCommandsDeployMutation() {
return useMutation(({ guildId }: { guildId: string }) => {
return fetch(`/api/custom-bot/commands/deploy?guild_id=${guildId}`, {
return fetchApi(`/api/custom-bot/commands/deploy?guild_id=${guildId}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -345,7 +346,7 @@ export function useCustomCommandsDeployMutation() {
export function useCustomCommandDeleteMutation() {
return useMutation(
({ commandId, guildId }: { commandId: string; guildId: string }) => {
return fetch(
return fetchApi(
`/api/custom-bot/commands/${commandId}?guild_id=${guildId}`,
{
method: "DELETE",
Expand All @@ -371,7 +372,7 @@ export function useUploadImageMutation() {
const body = new FormData();
body.append("file", file);

return fetch(url, {
return fetchApi(url, {
method: "POST",
body,
}).then((res) => handleApiResponse<UploadImageResponseWire>(res.json()));
Expand All @@ -388,7 +389,7 @@ export function useScheduledMessageCreateMutation() {
guildId: string;
req: ScheduledMessageCreateRequestWire;
}) => {
return fetch(`/api/scheduled-messages?guild_id=${guildId}`, {
return fetchApi(`/api/scheduled-messages?guild_id=${guildId}`, {
method: "POST",
body: JSON.stringify(req),
headers: {
Expand All @@ -412,13 +413,16 @@ export function useScheduledMessageUpdateMutation() {
guildId: string;
req: ScheduledMessageUpdateRequestWire;
}) => {
return fetch(`/api/scheduled-messages/${messageId}?guild_id=${guildId}`, {
method: "PUT",
body: JSON.stringify(req),
headers: {
"Content-Type": "application/json",
},
}).then((res) =>
return fetchApi(
`/api/scheduled-messages/${messageId}?guild_id=${guildId}`,
{
method: "PUT",
body: JSON.stringify(req),
headers: {
"Content-Type": "application/json",
},
}
).then((res) =>
handleApiResponse<ScheduledMessageUpdateResponseWire>(res.json())
);
}
Expand All @@ -428,12 +432,15 @@ export function useScheduledMessageUpdateMutation() {
export function useScheduledMessageDeleteMutation() {
return useMutation(
({ messageId, guildId }: { messageId: string; guildId: string }) => {
return fetch(`/api/scheduled-messages/${messageId}?guild_id=${guildId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
}).then((res) =>
return fetchApi(
`/api/scheduled-messages/${messageId}?guild_id=${guildId}`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
}
).then((res) =>
handleApiResponse<ScheduledMessageDeleteResponseWire>(res.json())
);
}
Expand Down
Loading

0 comments on commit a7ac510

Please sign in to comment.