|
| 1 | +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" |
| 2 | + |
| 3 | +import { api } from "#/lib/api-client" |
| 4 | +import type { |
| 5 | + ConfigListResponse, |
| 6 | + CreateConfigInput, |
| 7 | + CreateLevelInput, |
| 8 | + CreateStageInput, |
| 9 | + Level, |
| 10 | + LevelConfig, |
| 11 | + LevelListResponse, |
| 12 | + LevelStage, |
| 13 | + StageListResponse, |
| 14 | + UpdateConfigInput, |
| 15 | + UpdateLevelInput, |
| 16 | + UpdateStageInput, |
| 17 | +} from "#/lib/types/level" |
| 18 | + |
| 19 | +const CONFIGS_KEY = ["level-configs"] as const |
| 20 | +const configKey = (key: string) => ["level-config", key] as const |
| 21 | +const stagesKey = (configId: string) => |
| 22 | + ["level-config", configId, "stages"] as const |
| 23 | +const levelsKey = (configId: string) => |
| 24 | + ["level-config", configId, "levels"] as const |
| 25 | +const levelKey = (id: string) => ["level", id] as const |
| 26 | + |
| 27 | +// ─── Configs ───────────────────────────────────────────────────── |
| 28 | + |
| 29 | +export function useLevelConfigs() { |
| 30 | + return useQuery({ |
| 31 | + queryKey: CONFIGS_KEY, |
| 32 | + queryFn: () => api.get<ConfigListResponse>("/api/level/configs"), |
| 33 | + select: (data) => data.items, |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +export function useLevelConfig(key: string) { |
| 38 | + return useQuery({ |
| 39 | + queryKey: configKey(key), |
| 40 | + queryFn: () => api.get<LevelConfig>(`/api/level/configs/${key}`), |
| 41 | + enabled: !!key, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +export function useCreateLevelConfig() { |
| 46 | + const qc = useQueryClient() |
| 47 | + return useMutation({ |
| 48 | + mutationFn: (input: CreateConfigInput) => |
| 49 | + api.post<LevelConfig>("/api/level/configs", input), |
| 50 | + onSuccess: () => qc.invalidateQueries({ queryKey: CONFIGS_KEY }), |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +export function useUpdateLevelConfig() { |
| 55 | + const qc = useQueryClient() |
| 56 | + return useMutation({ |
| 57 | + mutationFn: ({ id, input }: { id: string; input: UpdateConfigInput }) => |
| 58 | + api.put<LevelConfig>(`/api/level/configs/${id}`, input), |
| 59 | + onSuccess: (_row, vars) => { |
| 60 | + qc.invalidateQueries({ queryKey: CONFIGS_KEY }) |
| 61 | + qc.invalidateQueries({ queryKey: configKey(vars.id) }) |
| 62 | + }, |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +export function useDeleteLevelConfig() { |
| 67 | + const qc = useQueryClient() |
| 68 | + return useMutation({ |
| 69 | + mutationFn: (id: string) => api.delete(`/api/level/configs/${id}`), |
| 70 | + onSuccess: () => qc.invalidateQueries({ queryKey: CONFIGS_KEY }), |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +// ─── Stages ────────────────────────────────────────────────────── |
| 75 | + |
| 76 | +export function useLevelStages(configId: string) { |
| 77 | + return useQuery({ |
| 78 | + queryKey: stagesKey(configId), |
| 79 | + queryFn: () => |
| 80 | + api.get<StageListResponse>( |
| 81 | + `/api/level/configs/${configId}/stages`, |
| 82 | + ), |
| 83 | + select: (data) => data.items, |
| 84 | + enabled: !!configId, |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +export function useCreateLevelStage() { |
| 89 | + const qc = useQueryClient() |
| 90 | + return useMutation({ |
| 91 | + mutationFn: ({ |
| 92 | + configId, |
| 93 | + input, |
| 94 | + }: { |
| 95 | + configId: string |
| 96 | + input: CreateStageInput |
| 97 | + }) => |
| 98 | + api.post<LevelStage>( |
| 99 | + `/api/level/configs/${configId}/stages`, |
| 100 | + input, |
| 101 | + ), |
| 102 | + onSuccess: (_row, vars) => { |
| 103 | + qc.invalidateQueries({ queryKey: stagesKey(vars.configId) }) |
| 104 | + }, |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +export function useUpdateLevelStage() { |
| 109 | + const qc = useQueryClient() |
| 110 | + return useMutation({ |
| 111 | + mutationFn: ({ |
| 112 | + id, |
| 113 | + input, |
| 114 | + }: { |
| 115 | + id: string |
| 116 | + configId: string |
| 117 | + input: UpdateStageInput |
| 118 | + }) => api.put<LevelStage>(`/api/level/stages/${id}`, input), |
| 119 | + onSuccess: (_row, vars) => { |
| 120 | + qc.invalidateQueries({ queryKey: stagesKey(vars.configId) }) |
| 121 | + }, |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +export function useDeleteLevelStage() { |
| 126 | + const qc = useQueryClient() |
| 127 | + return useMutation({ |
| 128 | + mutationFn: ({ id }: { id: string; configId: string }) => |
| 129 | + api.delete(`/api/level/stages/${id}`), |
| 130 | + onSuccess: (_row, vars) => { |
| 131 | + qc.invalidateQueries({ queryKey: stagesKey(vars.configId) }) |
| 132 | + qc.invalidateQueries({ queryKey: levelsKey(vars.configId) }) |
| 133 | + }, |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +// ─── Levels ────────────────────────────────────────────────────── |
| 138 | + |
| 139 | +export function useLevels(configId: string, stageId?: string) { |
| 140 | + return useQuery({ |
| 141 | + queryKey: [...levelsKey(configId), stageId ?? "all"], |
| 142 | + queryFn: () => |
| 143 | + api.get<LevelListResponse>( |
| 144 | + `/api/level/configs/${configId}/levels${stageId ? `?stageId=${stageId}` : ""}`, |
| 145 | + ), |
| 146 | + select: (data) => data.items, |
| 147 | + enabled: !!configId, |
| 148 | + }) |
| 149 | +} |
| 150 | + |
| 151 | +export function useLevel(id: string) { |
| 152 | + return useQuery({ |
| 153 | + queryKey: levelKey(id), |
| 154 | + queryFn: () => api.get<Level>(`/api/level/levels/${id}`), |
| 155 | + enabled: !!id, |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +export function useCreateLevel() { |
| 160 | + const qc = useQueryClient() |
| 161 | + return useMutation({ |
| 162 | + mutationFn: ({ |
| 163 | + configId, |
| 164 | + input, |
| 165 | + }: { |
| 166 | + configId: string |
| 167 | + input: CreateLevelInput |
| 168 | + }) => |
| 169 | + api.post<Level>( |
| 170 | + `/api/level/configs/${configId}/levels`, |
| 171 | + input, |
| 172 | + ), |
| 173 | + onSuccess: (_row, vars) => { |
| 174 | + qc.invalidateQueries({ queryKey: levelsKey(vars.configId) }) |
| 175 | + }, |
| 176 | + }) |
| 177 | +} |
| 178 | + |
| 179 | +export function useUpdateLevel() { |
| 180 | + const qc = useQueryClient() |
| 181 | + return useMutation({ |
| 182 | + mutationFn: ({ |
| 183 | + id, |
| 184 | + input, |
| 185 | + }: { |
| 186 | + id: string |
| 187 | + configId: string |
| 188 | + input: UpdateLevelInput |
| 189 | + }) => api.put<Level>(`/api/level/levels/${id}`, input), |
| 190 | + onSuccess: (_row, vars) => { |
| 191 | + qc.invalidateQueries({ queryKey: levelsKey(vars.configId) }) |
| 192 | + qc.invalidateQueries({ queryKey: levelKey(vars.id) }) |
| 193 | + }, |
| 194 | + }) |
| 195 | +} |
| 196 | + |
| 197 | +export function useDeleteLevel() { |
| 198 | + const qc = useQueryClient() |
| 199 | + return useMutation({ |
| 200 | + mutationFn: ({ id }: { id: string; configId: string }) => |
| 201 | + api.delete(`/api/level/levels/${id}`), |
| 202 | + onSuccess: (_row, vars) => { |
| 203 | + qc.invalidateQueries({ queryKey: levelsKey(vars.configId) }) |
| 204 | + }, |
| 205 | + }) |
| 206 | +} |
0 commit comments