-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
refactor: solid presets (@miodec) #7825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import { Preset } from "@monkeytype/schemas/presets"; | ||
| import { queryCollectionOptions } from "@tanstack/query-db-collection"; | ||
| import { | ||
| createCollection, | ||
| createOptimisticAction, | ||
| useLiveQuery, | ||
| } from "@tanstack/solid-db"; | ||
| import Ape from "../ape"; | ||
| import { queryClient } from "../queries"; | ||
| import { baseKey } from "../queries/utils/keys"; | ||
| import { ConfigGroupName } from "@monkeytype/schemas/configs"; | ||
|
|
||
| export type PresetItem = Preset & { display: string }; | ||
|
|
||
| const queryKeys = { | ||
| root: () => [...baseKey("presets", { isUserSpecific: true })], | ||
| }; | ||
|
|
||
| function toPresetItem(preset: Preset): PresetItem { | ||
| return { | ||
| ...preset, | ||
| display: preset.name.replaceAll("_", " "), | ||
| }; | ||
| } | ||
|
|
||
| // oxlint-disable-next-line typescript/explicit-function-return-type | ||
| export function usePresetsLiveQuery() { | ||
| return useLiveQuery((q) => { | ||
| return q.from({ preset: presetsCollection }).select((p) => ({ ...p })); | ||
| }); | ||
| } | ||
|
|
||
| const presetsCollection = createCollection( | ||
| queryCollectionOptions({ | ||
| staleTime: Infinity, | ||
| startSync: true, | ||
| queryKey: queryKeys.root(), | ||
|
|
||
| queryClient, | ||
| getKey: (it) => it._id, | ||
| queryFn: async () => { | ||
| return [] as PresetItem[]; | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| type ActionType = { | ||
| addPreset: { | ||
| name: string; | ||
| config: Preset["config"]; | ||
| settingGroups: ConfigGroupName[] | undefined; | ||
| }; | ||
| editPreset: { | ||
| presetId: string; | ||
| name: string; | ||
| config?: Preset["config"]; | ||
| settingGroups?: ConfigGroupName[] | null; | ||
| }; | ||
| deletePreset: { | ||
| presetId: string; | ||
| }; | ||
| }; | ||
|
|
||
| const actions = { | ||
| addPreset: createOptimisticAction<ActionType["addPreset"]>({ | ||
| onMutate: ({ name, config, settingGroups }) => { | ||
| presetsCollection.insert({ | ||
| _id: "temp-" + Date.now(), | ||
| name, | ||
| display: name.replaceAll("_", " "), | ||
| config, | ||
| settingGroups, | ||
| }); | ||
| }, | ||
| mutationFn: async ({ name, config, settingGroups }) => { | ||
| const response = await Ape.presets.add({ | ||
| body: { | ||
| name, | ||
| config, | ||
| ...(settingGroups !== undefined && { settingGroups }), | ||
| }, | ||
| }); | ||
| if (response.status !== 200) { | ||
| throw new Error(`Failed to add preset: ${response.body.message}`); | ||
| } | ||
| presetsCollection.utils.writeInsert( | ||
| toPresetItem({ | ||
| _id: response.body.data.presetId, | ||
| name: name, | ||
| settingGroups: settingGroups, | ||
| config: config, | ||
| }), | ||
| ); | ||
| }, | ||
| }), | ||
| editPreset: createOptimisticAction<ActionType["editPreset"]>({ | ||
| onMutate: ({ presetId, name, config, settingGroups }) => { | ||
| presetsCollection.update(presetId, (preset) => { | ||
| preset.name = name; | ||
| preset.display = name.replaceAll("_", " "); | ||
|
|
||
| if (config !== undefined) { | ||
| preset.config = config; | ||
| } | ||
| if (settingGroups !== undefined) { | ||
| preset.settingGroups = settingGroups; | ||
| } | ||
| }); | ||
| }, | ||
| mutationFn: async ({ presetId, name, config, settingGroups }) => { | ||
| const existing = presetsCollection.get(presetId); | ||
|
|
||
| if (existing === undefined) { | ||
| throw new Error("Preset not found"); | ||
| } | ||
|
|
||
| const response = await Ape.presets.save({ | ||
| body: { | ||
| _id: presetId, | ||
| name: name, | ||
| ...(config !== undefined && { | ||
| config: config, | ||
| settingGroups: settingGroups, | ||
| }), | ||
| }, | ||
| }); | ||
| if (response.status !== 200) { | ||
| throw new Error(`Failed to edit preset: ${response.body.message}`); | ||
| } | ||
| }, | ||
| }), | ||
| deletePreset: createOptimisticAction<ActionType["deletePreset"]>({ | ||
| onMutate: ({ presetId }) => { | ||
| presetsCollection.delete(presetId); | ||
| }, | ||
| mutationFn: async ({ presetId }) => { | ||
| const response = await Ape.presets.delete({ | ||
| params: { presetId }, | ||
| }); | ||
| if (response.status !== 200) { | ||
| throw new Error(`Failed to delete preset: ${response.body.message}`); | ||
| } | ||
| presetsCollection.delete(presetId); | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| // --- Public API --- | ||
|
|
||
| // todo: this might not be reactive | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A new Todo was discovered. If it is not a priority right now,consider marking it for later attention. |
||
| export function getPresets(): PresetItem[] { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ig |
||
| return [...presetsCollection.values()]; | ||
| } | ||
|
|
||
| // todo: this might not be reactive | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A new Todo was discovered. If it is not a priority right now,consider marking it for later attention. |
||
| export function getPreset(id: string): PresetItem | undefined { | ||
| return presetsCollection.get(id); | ||
| } | ||
|
|
||
| export function fillPresetsCollection(presets: Preset[]): void { | ||
| const presetItems = presets | ||
| .map(toPresetItem) | ||
| .sort((a, b) => a.name.localeCompare(b.name)); | ||
|
|
||
| presetsCollection.utils.writeBatch(() => { | ||
| presetsCollection.forEach((preset) => { | ||
| presetsCollection.utils.writeDelete(preset._id); | ||
| }); | ||
| presetItems.forEach((item) => { | ||
| presetsCollection.utils.writeInsert(item); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| export async function addPreset( | ||
| params: ActionType["addPreset"], | ||
| ): Promise<void> { | ||
| const transaction = actions.addPreset(params); | ||
| await transaction.isPersisted.promise; | ||
| } | ||
|
|
||
| export async function editPreset( | ||
| params: ActionType["editPreset"], | ||
| ): Promise<void> { | ||
| const transaction = actions.editPreset(params); | ||
| await transaction.isPersisted.promise; | ||
| } | ||
|
|
||
| export async function deletePreset( | ||
| params: ActionType["deletePreset"], | ||
| ): Promise<void> { | ||
| const transaction = actions.deletePreset(params); | ||
| await transaction.isPersisted.promise; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets order by name here and in the
getPresetsfunction and remove from thefillPresetsCollection