|
| 1 | +import { Preset } from "@monkeytype/schemas/presets"; |
| 2 | +import { queryCollectionOptions } from "@tanstack/query-db-collection"; |
| 3 | +import { |
| 4 | + createCollection, |
| 5 | + createOptimisticAction, |
| 6 | + useLiveQuery, |
| 7 | +} from "@tanstack/solid-db"; |
| 8 | +import Ape from "../ape"; |
| 9 | +import { queryClient } from "../queries"; |
| 10 | +import { baseKey } from "../queries/utils/keys"; |
| 11 | +import { ConfigGroupName } from "@monkeytype/schemas/configs"; |
| 12 | +import { tempId } from "./utils/misc"; |
| 13 | + |
| 14 | +export type PresetItem = Preset; |
| 15 | + |
| 16 | +const queryKeys = { |
| 17 | + root: () => [...baseKey("presets", { isUserSpecific: true })], |
| 18 | +}; |
| 19 | + |
| 20 | +// oxlint-disable-next-line typescript/explicit-function-return-type |
| 21 | +export function usePresetsLiveQuery() { |
| 22 | + return useLiveQuery((q) => { |
| 23 | + return q |
| 24 | + .from({ preset: presetsCollection }) |
| 25 | + .orderBy(({ preset }) => preset.name, "asc"); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +const presetsCollection = createCollection( |
| 30 | + queryCollectionOptions({ |
| 31 | + staleTime: Infinity, |
| 32 | + startSync: true, |
| 33 | + queryKey: queryKeys.root(), |
| 34 | + |
| 35 | + queryClient, |
| 36 | + getKey: (it) => it._id, |
| 37 | + queryFn: async () => { |
| 38 | + return [] as PresetItem[]; |
| 39 | + }, |
| 40 | + }), |
| 41 | +); |
| 42 | + |
| 43 | +type ActionType = { |
| 44 | + addPreset: { |
| 45 | + name: string; |
| 46 | + config: Preset["config"]; |
| 47 | + settingGroups: ConfigGroupName[] | undefined; |
| 48 | + }; |
| 49 | + editPreset: { |
| 50 | + presetId: string; |
| 51 | + name: string; |
| 52 | + config?: Preset["config"]; |
| 53 | + settingGroups?: ConfigGroupName[] | null; |
| 54 | + }; |
| 55 | + deletePreset: { |
| 56 | + presetId: string; |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +const actions = { |
| 61 | + addPreset: createOptimisticAction<ActionType["addPreset"]>({ |
| 62 | + onMutate: ({ name, config, settingGroups }) => { |
| 63 | + presetsCollection.insert({ |
| 64 | + _id: tempId(), |
| 65 | + name: name.replace(/_/g, " "), |
| 66 | + config, |
| 67 | + settingGroups, |
| 68 | + }); |
| 69 | + }, |
| 70 | + mutationFn: async ({ name, config, settingGroups }) => { |
| 71 | + const response = await Ape.presets.add({ |
| 72 | + body: { |
| 73 | + name: name.replace(/ /g, "_"), |
| 74 | + config, |
| 75 | + ...(settingGroups !== undefined && { settingGroups }), |
| 76 | + }, |
| 77 | + }); |
| 78 | + if (response.status !== 200) { |
| 79 | + throw new Error(`Failed to add preset: ${response.body.message}`); |
| 80 | + } |
| 81 | + |
| 82 | + const newPreset = { |
| 83 | + _id: response.body.data.presetId, |
| 84 | + name: name.replace(/_/g, " "), |
| 85 | + config, |
| 86 | + settingGroups, |
| 87 | + }; |
| 88 | + |
| 89 | + presetsCollection.utils.writeInsert(newPreset); |
| 90 | + }, |
| 91 | + }), |
| 92 | + editPreset: createOptimisticAction<ActionType["editPreset"]>({ |
| 93 | + onMutate: ({ presetId, name, config, settingGroups }) => { |
| 94 | + presetsCollection.update(presetId, (preset) => { |
| 95 | + preset.name = name.replace(/_/g, " "); |
| 96 | + |
| 97 | + if (config !== undefined) { |
| 98 | + preset.config = config; |
| 99 | + } |
| 100 | + if (settingGroups !== undefined) { |
| 101 | + preset.settingGroups = settingGroups; |
| 102 | + } |
| 103 | + }); |
| 104 | + }, |
| 105 | + mutationFn: async ({ presetId, name, config, settingGroups }) => { |
| 106 | + const existing = presetsCollection.get(presetId); |
| 107 | + |
| 108 | + if (existing === undefined) { |
| 109 | + throw new Error("Preset not found"); |
| 110 | + } |
| 111 | + |
| 112 | + const response = await Ape.presets.save({ |
| 113 | + body: { |
| 114 | + _id: presetId, |
| 115 | + name: name.replace(/ /g, "_"), |
| 116 | + ...(config !== undefined && { |
| 117 | + config: config, |
| 118 | + settingGroups: settingGroups, |
| 119 | + }), |
| 120 | + }, |
| 121 | + }); |
| 122 | + if (response.status !== 200) { |
| 123 | + throw new Error(`Failed to edit preset: ${response.body.message}`); |
| 124 | + } |
| 125 | + |
| 126 | + // if this is missing getPreset is out of sync |
| 127 | + presetsCollection.utils.writeUpdate({ |
| 128 | + _id: presetId, |
| 129 | + name: name.replace(/_/g, " "), |
| 130 | + ...(config !== undefined && { config }), |
| 131 | + ...(settingGroups !== undefined && { settingGroups }), |
| 132 | + }); |
| 133 | + }, |
| 134 | + }), |
| 135 | + deletePreset: createOptimisticAction<ActionType["deletePreset"]>({ |
| 136 | + onMutate: ({ presetId }) => { |
| 137 | + presetsCollection.delete(presetId); |
| 138 | + }, |
| 139 | + mutationFn: async ({ presetId }) => { |
| 140 | + const response = await Ape.presets.delete({ |
| 141 | + params: { presetId }, |
| 142 | + }); |
| 143 | + if (response.status !== 200) { |
| 144 | + throw new Error(`Failed to delete preset: ${response.body.message}`); |
| 145 | + } |
| 146 | + presetsCollection.utils.writeDelete(presetId); |
| 147 | + }, |
| 148 | + }), |
| 149 | +}; |
| 150 | + |
| 151 | +// --- Public API --- |
| 152 | + |
| 153 | +function getPresets(): PresetItem[] { |
| 154 | + return [...presetsCollection.values()].sort((a, b) => |
| 155 | + a.name.localeCompare(b.name), |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +function getPreset(id: string): PresetItem | undefined { |
| 160 | + return presetsCollection.get(id); |
| 161 | +} |
| 162 | + |
| 163 | +export function fillPresetsCollection(presets: Preset[]): void { |
| 164 | + const presetItems = presets.map((preset) => ({ |
| 165 | + _id: preset._id, |
| 166 | + name: preset.name.replace(/_/g, " "), |
| 167 | + config: preset.config, |
| 168 | + settingGroups: preset.settingGroups, |
| 169 | + })); |
| 170 | + |
| 171 | + presetsCollection.utils.writeBatch(() => { |
| 172 | + presetItems.forEach((item) => { |
| 173 | + presetsCollection.utils.writeInsert(item); |
| 174 | + }); |
| 175 | + }); |
| 176 | +} |
| 177 | + |
| 178 | +export async function addPreset( |
| 179 | + params: ActionType["addPreset"], |
| 180 | +): Promise<void> { |
| 181 | + const transaction = actions.addPreset(params); |
| 182 | + await transaction.isPersisted.promise; |
| 183 | +} |
| 184 | + |
| 185 | +export async function editPreset( |
| 186 | + params: ActionType["editPreset"], |
| 187 | +): Promise<void> { |
| 188 | + const transaction = actions.editPreset(params); |
| 189 | + await transaction.isPersisted.promise; |
| 190 | +} |
| 191 | + |
| 192 | +export async function deletePreset( |
| 193 | + params: ActionType["deletePreset"], |
| 194 | +): Promise<void> { |
| 195 | + const transaction = actions.deletePreset(params); |
| 196 | + await transaction.isPersisted.promise; |
| 197 | +} |
| 198 | + |
| 199 | +/** |
| 200 | + * Used for non reactive access. Do not use in Solid components. |
| 201 | + */ |
| 202 | +export const __nonReactive = { |
| 203 | + getPresets, |
| 204 | + getPreset, |
| 205 | +}; |
0 commit comments