Skip to content

Commit ae22246

Browse files
anonrigzomars
andauthored
perf: improve application toggle flow (#8302)
* perf: improve application toggle flow * Reverses logic for enabled apps --------- Co-authored-by: zomars <[email protected]>
1 parent e2931d9 commit ae22246

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

packages/features/apps/AdminAppsList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const IntegrationContainer = ({
104104
} else if (app.keys) {
105105
showKeyModal(true);
106106
} else {
107-
enableAppMutation.mutate({ slug: app.slug, enabled: app.enabled });
107+
enableAppMutation.mutate({ slug: app.slug, enabled: !app.enabled });
108108
}
109109
}}
110110
/>
@@ -117,7 +117,7 @@ const IntegrationContainer = ({
117117
title={t("disable_app")}
118118
variety="danger"
119119
onConfirm={() => {
120-
enableAppMutation.mutate({ slug: app.slug, enabled: app.enabled });
120+
enableAppMutation.mutate({ slug: app.slug, enabled: !app.enabled });
121121
}}>
122122
{t("disable_app_description")}
123123
</ConfirmationDialogContent>

packages/trpc/server/routers/viewer/apps.tsx

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export const appsRouter = router({
9999
)
100100
.mutation(async ({ ctx, input }) => {
101101
const { prisma } = ctx;
102+
const { enabled } = input;
102103

103104
// Get app name from metadata
104105
const localApps = getLocalAppMetadata();
@@ -112,7 +113,7 @@ export const appsRouter = router({
112113
slug: input.slug,
113114
},
114115
update: {
115-
enabled: !input.enabled,
116+
enabled,
116117
dirName: appMetadata?.dirName || appMetadata?.slug || "",
117118
},
118119
create: {
@@ -123,12 +124,14 @@ export const appsRouter = router({
123124
([appMetadata?.category] as AppCategories[]) ||
124125
undefined,
125126
keys: undefined,
126-
enabled: !input.enabled,
127+
enabled,
127128
},
128129
});
129130

130131
// If disabling an app then we need to alert users based on the app type
131-
if (input.enabled) {
132+
if (!enabled) {
133+
const translations = new Map();
134+
132135
if (app.categories.some((category) => ["calendar", "video"].includes(category))) {
133136
// Find all users with the app credentials
134137
const appCredentials = await prisma.credential.findMany({
@@ -145,18 +148,26 @@ export const appsRouter = router({
145148
},
146149
});
147150

151+
// TODO: This should be done async probably using a queue.
148152
Promise.all(
149153
appCredentials.map(async (credential) => {
150-
const t = await getTranslation(credential.user?.locale || "en", "common");
151-
152-
if (credential.user?.email) {
153-
await sendDisabledAppEmail({
154-
email: credential.user.email,
155-
appName: appMetadata?.name || app.slug,
156-
appType: app.categories,
157-
t,
158-
});
154+
// No need to continue if credential does not have a user
155+
if (!credential.user || !credential.user.email) return;
156+
157+
const locale = credential.user.locale ?? "en";
158+
let t = translations.get(locale);
159+
160+
if (!t) {
161+
t = await getTranslation(locale, "common");
162+
translations.set(locale, t);
159163
}
164+
165+
await sendDisabledAppEmail({
166+
email: credential.user.email,
167+
appName: appMetadata?.name || app.slug,
168+
appType: app.categories,
169+
t,
170+
});
160171
})
161172
);
162173
} else {
@@ -180,8 +191,11 @@ export const appsRouter = router({
180191
},
181192
});
182193

194+
// TODO: This should be done async probably using a queue.
183195
Promise.all(
184196
eventTypesWithApp.map(async (eventType) => {
197+
// TODO: This update query can be removed by merging it with
198+
// the previous `findMany` query, if that query returns certain values.
185199
await prisma.eventType.update({
186200
where: {
187201
id: eventType.id,
@@ -202,18 +216,26 @@ export const appsRouter = router({
202216
},
203217
});
204218

205-
eventType.users.map(async (user) => {
206-
const t = await getTranslation(user.locale || "en", "common");
207-
208-
await sendDisabledAppEmail({
209-
email: user.email,
210-
appName: appMetadata?.name || app.slug,
211-
appType: app.categories,
212-
t,
213-
title: eventType.title,
214-
eventTypeId: eventType.id,
215-
});
216-
});
219+
return Promise.all(
220+
eventType.users.map(async (user) => {
221+
const locale = user.locale ?? "en";
222+
let t = translations.get(locale);
223+
224+
if (!t) {
225+
t = await getTranslation(locale, "common");
226+
translations.set(locale, t);
227+
}
228+
229+
await sendDisabledAppEmail({
230+
email: user.email,
231+
appName: appMetadata?.name || app.slug,
232+
appType: app.categories,
233+
t,
234+
title: eventType.title,
235+
eventTypeId: eventType.id,
236+
});
237+
})
238+
);
217239
})
218240
);
219241
}

0 commit comments

Comments
 (0)