Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / frontend/src/lib/settingsQueries.ts |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | frontend/src/lib/settingsQueries.ts |
| Original SHA-256 | d559bc4d0bfa780eca03a2e79a6715e4be3d29a06923931ae6200c08fee04379 |
| Git blob SHA | 9bab7e2b38a7079c55d0f4ff2897e8e495ecf8bd |
| Reading-copy SHA-256 | cec92867b779c3e8f1cb460c41a4aaf28c80e296dc8a5da8c50b03f925f3d320 |
Recovered source transcript
Download complete source text ↓116 lines · 4.3 KB
Credential-bearing and unrelated personal details were removed from this reading copy. The source identity and original checksum remain recorded.
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { settingsApi, type SettingsPatch } from './settings'
// The org settings key. Every mutation below invalidates it rather than writing the response into the cache
// by hand, because the server may adjust what was sent: switching a mail host CLEARS the verification and
// turns delivery off, so a hand written cache update would leave the screen claiming mail still works.
const SETTINGS_KEY = ['org', 'settings'] as const
const POLICY_KEY = ['org', 'policy'] as const
const TEAMS_KEY = ['org', 'teams'] as const
export function useOrgSettings() {
return useQuery({ queryKey: SETTINGS_KEY, queryFn: settingsApi.get })
}
// The policy is read by every screen that renders a rate or somebody else's activity, so it is cached for a
// few minutes. It is NOT cached for the session: an owner narrowing visibility should take effect while the
// people it applies to are still logged in, not after they next sign in.
export function useOrgPolicy() {
return useQuery({ queryKey: POLICY_KEY, queryFn: settingsApi.policy, staleTime: 2 * 60_000 })
}
export function useSaveSettings() {
const qc = useQueryClient()
return useMutation({
mutationFn: (patch: SettingsPatch) => settingsApi.patch(patch),
onSuccess: () => {
void qc.invalidateQueries({ queryKey: SETTINGS_KEY })
// The policy is derived from these settings, so it is stale the moment they change.
void qc.invalidateQueries({ queryKey: POLICY_KEY })
},
})
}
export function useSetMailPassword() {
const qc = useQueryClient()
return useMutation({
mutationFn: (password: [credential removed] => settingsApi.setMailPassword(password),
onSuccess: () => void qc.invalidateQueries({ queryKey: SETTINGS_KEY }),
})
}
export function useClearMailPassword() {
const qc = useQueryClient()
return useMutation({
mutationFn: () => settingsApi.clearMailPassword(),
onSuccess: () => void qc.invalidateQueries({ queryKey: SETTINGS_KEY }),
})
}
export function useTestMail() {
const qc = useQueryClient()
return useMutation({
mutationFn: () => settingsApi.testMail(),
// Both outcomes change the stored state: success stamps the verification, failure records the error and
// switches delivery off, so the settings are refetched either way.
onSettled: () => void qc.invalidateQueries({ queryKey: SETTINGS_KEY }),
})
}
export function useTeams() {
return useQuery({ queryKey: TEAMS_KEY, queryFn: settingsApi.teams })
}
export function useMyTeams() {
return useQuery({ queryKey: [...TEAMS_KEY, 'mine'], queryFn: settingsApi.myTeams })
}
export function useCreateTeam() {
const qc = useQueryClient()
return useMutation({
mutationFn: (v: { name: string; lead_user_id?: string }) =>
settingsApi.createTeam(v.name, v.lead_user_id),
onSuccess: () => void qc.invalidateQueries({ queryKey: TEAMS_KEY }),
})
}
// useUpdateTeam covers rename, lead, and archive as one mutation, because they are one endpoint and
// splitting them would mean three hooks that differ only in which field they send.
//
// The POLICY is invalidated alongside the teams, because a team change is an access change whenever record
// scope is narrowed: moving somebody onto a desk changes which records exist for them.
export function useUpdateTeam() {
const qc = useQueryClient()
return useMutation({
mutationFn: (v: { id: string; patch: { name?: string; status?: string; lead_user_id?: string | null } }) =>
settingsApi.updateTeam(v.id, v.patch),
onSuccess: () => {
void qc.invalidateQueries({ queryKey: TEAMS_KEY })
void qc.invalidateQueries({ queryKey: POLICY_KEY })
},
})
}
export function useAddTeamMember() {
const qc = useQueryClient()
return useMutation({
mutationFn: (v: { teamId: string; userId: string }) => settingsApi.addMember(v.teamId, v.userId),
onSuccess: () => {
void qc.invalidateQueries({ queryKey: TEAMS_KEY })
void qc.invalidateQueries({ queryKey: POLICY_KEY })
},
})
}
export function useRemoveTeamMember() {
const qc = useQueryClient()
return useMutation({
mutationFn: (v: { teamId: string; userId: string }) => settingsApi.removeMember(v.teamId, v.userId),
onSuccess: () => {
void qc.invalidateQueries({ queryKey: TEAMS_KEY })
void qc.invalidateQueries({ queryKey: POLICY_KEY })
},
})
}