This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathuseSignalTeamConfigMutations.ts
More file actions
108 lines (99 loc) · 3.14 KB
/
Copy pathuseSignalTeamConfigMutations.ts
File metadata and controls
108 lines (99 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { signalsConfigKeys } from "@posthog/core/inbox/inboxQuery";
import type { SignalTeamConfig } from "@posthog/shared/types";
import { useAuthenticatedClient } from "@posthog/ui/features/auth/authClient";
import { useQueryClient } from "@tanstack/react-query";
import { useCallback } from "react";
import { toast } from "sonner";
const TEAM_CONFIG_QUERY_KEY = signalsConfigKeys.teamConfig;
/**
* Mutations that write to the per-team Self-driving config:
* default autostart priority, default Slack channel, and the per-repo
* autostart base-branch map. Reads come from `useSignalTeamConfig`.
*/
export function useSignalTeamConfigMutations() {
const client = useAuthenticatedClient();
const queryClient = useQueryClient();
const handleUpdateAutostartPriority = useCallback(
async (priority: string) => {
if (!client) return;
try {
await client.updateSignalTeamConfig({
default_autostart_priority: priority,
});
await queryClient.invalidateQueries({
queryKey: TEAM_CONFIG_QUERY_KEY,
});
} catch (error: unknown) {
const message =
error instanceof Error
? error.message
: "Failed to update autostart priority";
toast.error(message);
}
},
[client, queryClient],
);
const handleUpdateTeamSlackChannel = useCallback(
async (channel: string | null) => {
if (!client) return;
try {
await client.updateSignalTeamConfig({
default_slack_notification_channel: channel,
});
await queryClient.invalidateQueries({
queryKey: TEAM_CONFIG_QUERY_KEY,
});
} catch (error: unknown) {
const message =
error instanceof Error
? error.message
: "Failed to update default notification channel";
toast.error(message);
}
},
[client, queryClient],
);
const handleUpdateAutostartBaseBranches = useCallback(
async (branches: Record<string, string>) => {
if (!client) return;
const previous = queryClient.getQueryData<SignalTeamConfig | null>(
TEAM_CONFIG_QUERY_KEY,
);
if (previous) {
const optimistic: SignalTeamConfig = {
...previous,
autostart_base_branches: branches,
};
queryClient.setQueryData<SignalTeamConfig | null>(
TEAM_CONFIG_QUERY_KEY,
optimistic,
);
}
try {
const fresh = await client.updateSignalTeamConfig({
autostart_base_branches: branches,
});
queryClient.setQueryData<SignalTeamConfig | null>(
TEAM_CONFIG_QUERY_KEY,
fresh,
);
} catch (error: unknown) {
queryClient.setQueryData<SignalTeamConfig | null>(
TEAM_CONFIG_QUERY_KEY,
previous ?? null,
);
const message =
error instanceof Error
? error.message
: "Failed to update base branch setting";
toast.error(message);
}
},
[client, queryClient],
);
return {
handleUpdateAutostartPriority,
handleUpdateTeamSlackChannel,
handleUpdateAutostartBaseBranches,
};
}