|
1 | 1 | import type { InputRenderable, ScrollBoxRenderable } from "@opentui/core"; |
2 | 2 | import { useKeyboard } from "@opentui/react"; |
3 | | -import { useQuery } from "@tanstack/react-query"; |
| 3 | +import { useMutation, useQuery } from "@tanstack/react-query"; |
4 | 4 | import clipboard from "clipboardy"; |
5 | 5 | import { useEffect, useRef, useState } from "react"; |
6 | 6 | import { getConfig } from "../../../config"; |
@@ -149,6 +149,35 @@ export function Browser(props: Props) { |
149 | 149 | }, |
150 | 150 | }); |
151 | 151 |
|
| 152 | + const keyDeleteMutation = useMutation({ |
| 153 | + mutationKey: ["redis", "key-delete"], |
| 154 | + mutationFn: async (data: { fullKey: string }) => { |
| 155 | + await getRedis().del(data.fullKey); |
| 156 | + }, |
| 157 | + onSettled: () => { |
| 158 | + query.refetch(); |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const groupDeleteMutation = useMutation({ |
| 163 | + mutationKey: ["redis", "group-delete"], |
| 164 | + mutationFn: async (data: { groupPath: string }) => { |
| 165 | + const redisUtils = new RedisUtils(getRedis(), data.groupPath); |
| 166 | + const keysInGroup = await redisUtils.getRecursiveChildKeys(); |
| 167 | + if (keysInGroup.length === 0) { |
| 168 | + return; |
| 169 | + } |
| 170 | + const pipeline = getRedis().pipeline(); |
| 171 | + for (const keyEntry of keysInGroup) { |
| 172 | + pipeline.del(keyEntry.fullPath); |
| 173 | + } |
| 174 | + await pipeline.exec(); |
| 175 | + }, |
| 176 | + onSettled: () => { |
| 177 | + query.refetch(); |
| 178 | + }, |
| 179 | + }); |
| 180 | + |
152 | 181 | const [highlightedKeyFullPath, setHighlightedKeyFullPath] = useState< |
153 | 182 | string | undefined |
154 | 183 | >(); |
@@ -226,12 +255,12 @@ export function Browser(props: Props) { |
226 | 255 | } |
227 | 256 | }); |
228 | 257 |
|
229 | | - useRegisterKeyBind("c", "Copy key to clipboard"); |
| 258 | + useRegisterKeyBind("shift+c", "Copy key to clipboard"); |
230 | 259 | useKeyboard(async (key) => { |
231 | 260 | if (focus !== "key-list") { |
232 | 261 | return; |
233 | 262 | } |
234 | | - if (key.name === "c" && !key.ctrl && !key.meta) { |
| 263 | + if (key.name === "c" && key.shift && !key.ctrl && !key.meta) { |
235 | 264 | key.preventDefault(); |
236 | 265 | if (highlightedKeyFullPath != null) { |
237 | 266 | await clipboard.write(highlightedKeyFullPath); |
@@ -283,6 +312,30 @@ export function Browser(props: Props) { |
283 | 312 | } |
284 | 313 | }); |
285 | 314 |
|
| 315 | + const highlightedEntry = query.data?.find( |
| 316 | + (entry) => entry.fullPath === highlightedKeyFullPath, |
| 317 | + ); |
| 318 | + useRegisterKeyBind( |
| 319 | + "shift+d", |
| 320 | + ["Delete", highlightedEntry?.isGroup ? "group" : "key"].join(" "), |
| 321 | + ); |
| 322 | + useKeyboard((key) => { |
| 323 | + if (focus !== "key-list") { |
| 324 | + return; |
| 325 | + } |
| 326 | + if (key.name === "d" && key.shift && !key.ctrl && !key.meta) { |
| 327 | + key.preventDefault(); |
| 328 | + if (highlightedEntry == null) { |
| 329 | + return; |
| 330 | + } |
| 331 | + if (!highlightedEntry.isGroup) { |
| 332 | + keyDeleteMutation.mutate({ fullKey: highlightedEntry.fullPath }); |
| 333 | + } else { |
| 334 | + groupDeleteMutation.mutate({ groupPath: highlightedEntry.fullPath }); |
| 335 | + } |
| 336 | + } |
| 337 | + }); |
| 338 | + |
286 | 339 | return ( |
287 | 340 | <box flexDirection="column"> |
288 | 341 | <SearchBar |
|
0 commit comments