| 5 | } from "./InputPromptDraftStore"; |
| 6 | |
| 7 | export class InputPromptDraftHandler { |
| 8 | private readonly store = InputPromptDraftStore.getInstance(); |
| 9 | private readonly draftKey: string; |
| 10 | private initialValue = ""; |
| 11 | private didChange = false; |
| 12 | private readonly shouldPersist: () => boolean; |
| 13 | |
| 14 | constructor(key: InputPromptDraftKey, shouldPersist?: () => boolean) { |
| 15 | this.draftKey = this.store.makeKey(key); |
| 16 | this.shouldPersist = shouldPersist ?? |
| 17 | (() => settingsStore.getState().persistInputPromptDrafts); |
| 18 | } |
| 19 | |
| 20 | hydrate(initialValue: string): string { |
| 21 | this.initialValue = initialValue; |
| 22 | if (!this.shouldPersist()) return initialValue; |
| 23 | |
| 24 | const draft = this.store.get(this.draftKey); |
| 25 | return draft ?? initialValue; |
| 26 | } |
| 27 | |
| 28 | markChanged(): void { |
| 29 | this.didChange = true; |
| 30 | } |
| 31 | |
| 32 | persist(value: string, didSubmit: boolean): void { |
| 33 | if (!this.shouldPersist()) return; |
| 34 | |
| 35 | if (didSubmit) { |
| 36 | this.store.handleSubmittedDraft(this.draftKey, value); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if (!this.didChange || value === this.initialValue) return; |
| 41 | |
| 42 | if (!value.trim()) { |
| 43 | this.store.clear(this.draftKey); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | this.store.set(this.draftKey, value); |
| 48 | } |
| 49 | } |
nothing calls this directly
no test coverage detected