Skip to content

Commit bd5db0a

Browse files
feat: ✨ add keybind to delete keys and groups from browser view
1 parent ff5f116 commit bd5db0a

1 file changed

Lines changed: 56 additions & 3 deletions

File tree

src/components/functional/browser/browser.tsx

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { InputRenderable, ScrollBoxRenderable } from "@opentui/core";
22
import { useKeyboard } from "@opentui/react";
3-
import { useQuery } from "@tanstack/react-query";
3+
import { useMutation, useQuery } from "@tanstack/react-query";
44
import clipboard from "clipboardy";
55
import { useEffect, useRef, useState } from "react";
66
import { getConfig } from "../../../config";
@@ -149,6 +149,35 @@ export function Browser(props: Props) {
149149
},
150150
});
151151

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+
152181
const [highlightedKeyFullPath, setHighlightedKeyFullPath] = useState<
153182
string | undefined
154183
>();
@@ -226,12 +255,12 @@ export function Browser(props: Props) {
226255
}
227256
});
228257

229-
useRegisterKeyBind("c", "Copy key to clipboard");
258+
useRegisterKeyBind("shift+c", "Copy key to clipboard");
230259
useKeyboard(async (key) => {
231260
if (focus !== "key-list") {
232261
return;
233262
}
234-
if (key.name === "c" && !key.ctrl && !key.meta) {
263+
if (key.name === "c" && key.shift && !key.ctrl && !key.meta) {
235264
key.preventDefault();
236265
if (highlightedKeyFullPath != null) {
237266
await clipboard.write(highlightedKeyFullPath);
@@ -283,6 +312,30 @@ export function Browser(props: Props) {
283312
}
284313
});
285314

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+
286339
return (
287340
<box flexDirection="column">
288341
<SearchBar

0 commit comments

Comments
 (0)