|
| 1 | +export type JsonKey = Prettify<keyof State>; |
| 2 | + |
| 3 | +type State = { |
| 4 | + literature: { |
| 5 | + input?: Literature; |
| 6 | + originals?: Literature; |
| 7 | + translations?: Literature; |
| 8 | + }; |
| 9 | + outlines: { input?: Outlines; originals?: Outlines; translations?: Outlines }; |
| 10 | + songs: { input?: Songs; originals?: Songs; translations?: Songs }; |
| 11 | + tips: { input?: Tips; originals?: Tips; translations?: Tips }; |
| 12 | +}; |
| 13 | + |
| 14 | +export const useJsonStore = defineStore("json", { |
| 15 | + actions: { |
| 16 | + async fixInconsistentTips(heading: string, tips: { index: number }[]) { |
| 17 | + tips.forEach((t) => { |
| 18 | + if (!this.tips.translations?.[t.index]) return; |
| 19 | + this.tips.translations![t.index]!.heading = heading; |
| 20 | + }); |
| 21 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 22 | + }, |
| 23 | + setInput({ |
| 24 | + literature, |
| 25 | + outlines, |
| 26 | + songs, |
| 27 | + tips, |
| 28 | + }: { |
| 29 | + literature?: Literature; |
| 30 | + outlines?: Outlines; |
| 31 | + songs?: Songs; |
| 32 | + tips?: Tips; |
| 33 | + }) { |
| 34 | + this.literature = { ...this.literature, input: literature }; |
| 35 | + this.outlines = { ...this.outlines, input: outlines }; |
| 36 | + this.songs = { ...this.songs, input: songs }; |
| 37 | + this.tips = { ...this.tips, input: tips }; |
| 38 | + }, |
| 39 | + setOriginals({ |
| 40 | + literature, |
| 41 | + outlines, |
| 42 | + songs, |
| 43 | + tips, |
| 44 | + }: { |
| 45 | + literature?: Literature; |
| 46 | + outlines?: Outlines; |
| 47 | + songs?: Songs; |
| 48 | + tips?: Tips; |
| 49 | + }) { |
| 50 | + this.literature = { ...this.literature, originals: literature }; |
| 51 | + this.outlines = { ...this.outlines, originals: outlines }; |
| 52 | + this.songs = { ...this.songs, originals: songs }; |
| 53 | + this.tips = { ...this.tips, originals: tips }; |
| 54 | + }, |
| 55 | + setTranslations( |
| 56 | + { |
| 57 | + literature, |
| 58 | + outlines, |
| 59 | + songs, |
| 60 | + tips, |
| 61 | + }: { |
| 62 | + literature?: Literature; |
| 63 | + outlines?: Outlines; |
| 64 | + songs?: Songs; |
| 65 | + tips?: Tips; |
| 66 | + }, |
| 67 | + group?: JsonKey, |
| 68 | + ) { |
| 69 | + if (!group || group === "literature") { |
| 70 | + this.literature = { ...this.literature, translations: literature }; |
| 71 | + } |
| 72 | + if (!group || group === "outlines") { |
| 73 | + this.outlines = { ...this.outlines, translations: outlines }; |
| 74 | + } |
| 75 | + if (!group || group === "songs") { |
| 76 | + this.songs = { ...this.songs, translations: songs }; |
| 77 | + } |
| 78 | + if (!group || group === "tips") { |
| 79 | + this.tips = { ...this.tips, translations: tips }; |
| 80 | + } |
| 81 | + }, |
| 82 | + }, |
| 83 | + getters: { |
| 84 | + changedGroups(state) { |
| 85 | + const groups: JsonKey[] = []; |
| 86 | + typedKeys(state).forEach((group) => { |
| 87 | + if ( |
| 88 | + state[group] && |
| 89 | + JSON.stringify(state[group].input) !== |
| 90 | + JSON.stringify(state[group].translations) |
| 91 | + ) { |
| 92 | + groups.push(group); |
| 93 | + } |
| 94 | + }); |
| 95 | + return groups; |
| 96 | + }, |
| 97 | + inconsistentTips(state) { |
| 98 | + if (!state.tips.originals?.length) return []; |
| 99 | + const headings: Record<string, { index: number; translation: string }[]> = |
| 100 | + {}; |
| 101 | + |
| 102 | + state.tips.originals.forEach((tip, index) => { |
| 103 | + // If the translation is missing, skip it |
| 104 | + if (!state.tips.translations?.[index]?.heading) return; |
| 105 | + |
| 106 | + if (headings[tip.heading]) { |
| 107 | + headings[tip.heading]!.push({ |
| 108 | + index, |
| 109 | + translation: state.tips.translations[index].heading, |
| 110 | + }); |
| 111 | + } else { |
| 112 | + headings[tip.heading] = [ |
| 113 | + { |
| 114 | + index, |
| 115 | + translation: state.tips.translations[index].heading, |
| 116 | + }, |
| 117 | + ]; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + return Object.entries(headings) |
| 122 | + .filter( |
| 123 | + ([, tips]) => |
| 124 | + tips.length > 1 && |
| 125 | + tips.some((t) => t.translation !== tips[0]?.translation), |
| 126 | + ) |
| 127 | + .map(([heading, tips]) => ({ |
| 128 | + heading, |
| 129 | + tips: tips, |
| 130 | + translations: [...new Set(tips.map((t) => t.translation))], |
| 131 | + })); |
| 132 | + }, |
| 133 | + input(state) { |
| 134 | + return { |
| 135 | + literature: state.literature.input, |
| 136 | + outlines: state.outlines.input, |
| 137 | + songs: state.songs.input, |
| 138 | + tips: state.tips.input, |
| 139 | + }; |
| 140 | + }, |
| 141 | + missingLiterature(state) { |
| 142 | + return ( |
| 143 | + state.literature.originals?.filter( |
| 144 | + (o) => !state.literature.translations?.some((t) => t.id === o.id), |
| 145 | + ) ?? [] |
| 146 | + ); |
| 147 | + }, |
| 148 | + missingOutlines(state) { |
| 149 | + return ( |
| 150 | + state.outlines.originals?.filter( |
| 151 | + (o) => |
| 152 | + !!o.title && |
| 153 | + !state.outlines.translations?.some( |
| 154 | + (t) => t.number === o.number && !!t.title && !!t.updated, |
| 155 | + ), |
| 156 | + ) ?? [] |
| 157 | + ); |
| 158 | + }, |
| 159 | + missingSongs(state) { |
| 160 | + return ( |
| 161 | + state.songs.originals?.filter( |
| 162 | + (o) => !state.songs.translations?.some((t) => t.number === o.number), |
| 163 | + ) ?? [] |
| 164 | + ); |
| 165 | + }, |
| 166 | + missingTips(state) { |
| 167 | + return ( |
| 168 | + state.tips.originals?.filter( |
| 169 | + (t, i) => !state.tips.translations?.[i]?.heading, |
| 170 | + ) ?? [] |
| 171 | + ); |
| 172 | + }, |
| 173 | + originals(state) { |
| 174 | + return { |
| 175 | + literature: state.literature.originals, |
| 176 | + outlines: state.outlines.originals, |
| 177 | + songs: state.songs.originals, |
| 178 | + tips: state.tips.originals, |
| 179 | + }; |
| 180 | + }, |
| 181 | + translations(state) { |
| 182 | + return { |
| 183 | + literature: state.literature.translations, |
| 184 | + outlines: state.outlines.translations, |
| 185 | + songs: state.songs.translations, |
| 186 | + tips: state.tips.translations, |
| 187 | + }; |
| 188 | + }, |
| 189 | + }, |
| 190 | + persist: true, |
| 191 | + state: (): State => ({ |
| 192 | + literature: { |
| 193 | + input: undefined, |
| 194 | + originals: undefined, |
| 195 | + translations: undefined, |
| 196 | + }, |
| 197 | + outlines: { |
| 198 | + input: undefined, |
| 199 | + originals: undefined, |
| 200 | + translations: undefined, |
| 201 | + }, |
| 202 | + songs: { input: undefined, originals: undefined, translations: undefined }, |
| 203 | + tips: { input: undefined, originals: undefined, translations: undefined }, |
| 204 | + }), |
| 205 | +}); |
0 commit comments