Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/current-user/update-mutation.ts
2501 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { AdditionalUserData, User as UserProtocol } from "@gitpod/gitpod-protocol";
8
import { useMutation } from "@tanstack/react-query";
9
import { trackEvent } from "../../Analytics";
10
import { getGitpodService } from "../../service/service";
11
import { useCurrentUser } from "../../user-context";
12
import { converter } from "../../service/public-api";
13
import deepmerge from "deepmerge";
14
import uniq from "lodash/uniq";
15
16
type UpdateCurrentUserArgs = Partial<UserProtocol>;
17
18
export const useUpdateCurrentUserMutation = () => {
19
return useMutation({
20
mutationFn: async (partialUser: UpdateCurrentUserArgs) => {
21
const current = await getGitpodService().server.getLoggedInUser();
22
const currentAdditionalData = { ...current.additionalData };
23
// workspaceAutostartOptions needs to be overriden
24
if (partialUser.additionalData?.workspaceAutostartOptions) {
25
currentAdditionalData.workspaceAutostartOptions = [];
26
}
27
const update: UpdateCurrentUserArgs = {
28
id: current.id,
29
fullName: partialUser.fullName || current.fullName,
30
additionalData: deepmerge<AdditionalUserData>(
31
currentAdditionalData || {},
32
partialUser.additionalData || {},
33
),
34
};
35
// deepmerge will try append array, so once data is defined, ignore previous value
36
if (partialUser.additionalData?.profile?.explorationReasons) {
37
update.additionalData!.profile!.explorationReasons = uniq(
38
partialUser.additionalData.profile.explorationReasons,
39
);
40
}
41
if (partialUser.additionalData?.profile?.signupGoals) {
42
update.additionalData!.profile!.signupGoals = uniq(partialUser.additionalData.profile.signupGoals);
43
}
44
const user = await getGitpodService().server.updateLoggedInUser(update);
45
return converter.toUser(user);
46
},
47
});
48
};
49
50
export const useUpdateCurrentUserDotfileRepoMutation = () => {
51
const user = useCurrentUser();
52
const updateUser = useUpdateCurrentUserMutation();
53
54
return useMutation({
55
mutationFn: async (dotfileRepo: string) => {
56
if (!user) {
57
throw new Error("No user present");
58
}
59
60
const additionalData = {
61
dotfileRepo,
62
};
63
const updatedUser = await updateUser.mutateAsync({ additionalData });
64
65
return updatedUser;
66
},
67
onMutate: async () => {
68
return {
69
previousDotfileRepo: user?.dotfileRepo || "",
70
};
71
},
72
onSuccess: (updatedUser, _, context) => {
73
if (updatedUser?.dotfileRepo !== context?.previousDotfileRepo) {
74
trackEvent("dotfile_repo_changed", {
75
previous: context?.previousDotfileRepo ?? "",
76
current: updatedUser?.dotfileRepo ?? "",
77
});
78
}
79
},
80
});
81
};
82
83