| 24 | }; |
| 25 | |
| 26 | const updateCachedPlugin = ( |
| 27 | client: QueryClient, |
| 28 | id: string, |
| 29 | vote: UserVote, |
| 30 | ): UserVote | undefined => { |
| 31 | let previousVote: UserVote | undefined; |
| 32 | const update = (plugin: Plugin): Plugin => { |
| 33 | if (plugin.id !== id) { |
| 34 | return plugin; |
| 35 | } |
| 36 | |
| 37 | previousVote = plugin.userVote ?? UserVote.None; |
| 38 | |
| 39 | return applyPluginVote(plugin, vote); |
| 40 | }; |
| 41 | |
| 42 | client.setQueriesData<Plugin[]>( |
| 43 | { queryKey: [RequestKey.Plugins] }, |
| 44 | (plugins) => plugins?.map(update), |
| 45 | ); |
| 46 | client.setQueriesData<Plugin>({ queryKey: [RequestKey.Plugin] }, (plugin) => |
| 47 | plugin ? update(plugin) : plugin, |
| 48 | ); |
| 49 | |
| 50 | return previousVote; |
| 51 | }; |
| 52 | |
| 53 | interface UseVotePlugin { |
| 54 | toggleUpvote: (plugin: Plugin) => Promise<void>; |