Spaces:
Paused
Paused
Feat: Delete branch #1210 (#1240)
Browse files* feat: delete branch #1210
* removed some files
* lint
* types & error handling
* Improve query efficiency & add auth check
* Icon styling
* make it hover only
---------
Co-authored-by: Nathan Sarrazin <[email protected]>
src/lib/components/chat/ChatMessage.svelte
CHANGED
|
@@ -10,6 +10,7 @@
|
|
| 10 |
import CopyToClipBoardBtn from "../CopyToClipBoardBtn.svelte";
|
| 11 |
import IconLoading from "../icons/IconLoading.svelte";
|
| 12 |
import CarbonRotate360 from "~icons/carbon/rotate-360";
|
|
|
|
| 13 |
import CarbonDownload from "~icons/carbon/download";
|
| 14 |
import CarbonThumbsUp from "~icons/carbon/thumbs-up";
|
| 15 |
import CarbonThumbsDown from "~icons/carbon/thumbs-down";
|
|
@@ -512,7 +513,7 @@
|
|
| 512 |
<svelte:fragment slot="childrenNav">
|
| 513 |
{#if nChildren > 1 && $convTreeStore.editing === null}
|
| 514 |
<div
|
| 515 |
-
class="font-white z-10 -mt-1 ml-3.5 mr-auto flex h-6 w-fit select-none flex-row items-center justify-center gap-1 text-sm"
|
| 516 |
>
|
| 517 |
<button
|
| 518 |
class="inline text-lg font-thin text-gray-400 disabled:pointer-events-none disabled:opacity-25 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-200"
|
|
@@ -535,6 +536,19 @@
|
|
| 535 |
>
|
| 536 |
<CarbonChevronRight class="text-sm" />
|
| 537 |
</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
</div>
|
| 539 |
{/if}
|
| 540 |
</svelte:fragment>
|
|
|
|
| 10 |
import CopyToClipBoardBtn from "../CopyToClipBoardBtn.svelte";
|
| 11 |
import IconLoading from "../icons/IconLoading.svelte";
|
| 12 |
import CarbonRotate360 from "~icons/carbon/rotate-360";
|
| 13 |
+
import CarbonTrashCan from "~icons/carbon/trash-can";
|
| 14 |
import CarbonDownload from "~icons/carbon/download";
|
| 15 |
import CarbonThumbsUp from "~icons/carbon/thumbs-up";
|
| 16 |
import CarbonThumbsDown from "~icons/carbon/thumbs-down";
|
|
|
|
| 513 |
<svelte:fragment slot="childrenNav">
|
| 514 |
{#if nChildren > 1 && $convTreeStore.editing === null}
|
| 515 |
<div
|
| 516 |
+
class="font-white group/navbranch z-10 -mt-1 ml-3.5 mr-auto flex h-6 w-fit select-none flex-row items-center justify-center gap-1 text-sm"
|
| 517 |
>
|
| 518 |
<button
|
| 519 |
class="inline text-lg font-thin text-gray-400 disabled:pointer-events-none disabled:opacity-25 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-200"
|
|
|
|
| 536 |
>
|
| 537 |
<CarbonChevronRight class="text-sm" />
|
| 538 |
</button>
|
| 539 |
+
{#if !loading && message.children}<form
|
| 540 |
+
method="POST"
|
| 541 |
+
action="?/deleteBranch"
|
| 542 |
+
class="hidden group-hover/navbranch:block"
|
| 543 |
+
>
|
| 544 |
+
<input name="messageId" value={message.children[childrenToRender]} type="hidden" />
|
| 545 |
+
<button
|
| 546 |
+
class="flex items-center justify-center text-xs text-gray-400 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-200"
|
| 547 |
+
type="submit"
|
| 548 |
+
><CarbonTrashCan />
|
| 549 |
+
</button>
|
| 550 |
+
</form>
|
| 551 |
+
{/if}
|
| 552 |
</div>
|
| 553 |
{/if}
|
| 554 |
</svelte:fragment>
|
src/routes/conversation/[id]/+page.server.ts
CHANGED
|
@@ -66,3 +66,44 @@ export const load = async ({ params, depends, locals }) => {
|
|
| 66 |
shared,
|
| 67 |
};
|
| 68 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
shared,
|
| 67 |
};
|
| 68 |
};
|
| 69 |
+
|
| 70 |
+
export const actions = {
|
| 71 |
+
deleteBranch: async ({ request, locals, params }) => {
|
| 72 |
+
const data = await request.formData();
|
| 73 |
+
const messageId = data.get("messageId");
|
| 74 |
+
|
| 75 |
+
if (!messageId || typeof messageId !== "string") {
|
| 76 |
+
throw error(400, "Invalid message id");
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
const conversation = await collections.conversations.findOne({
|
| 80 |
+
...authCondition(locals),
|
| 81 |
+
_id: new ObjectId(params.id),
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
+
if (!conversation) {
|
| 85 |
+
throw error(404, "Conversation not found");
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
const filteredMessages = conversation.messages
|
| 89 |
+
.filter(
|
| 90 |
+
(message) =>
|
| 91 |
+
// not the message AND the message is not in ancestors
|
| 92 |
+
!(message.id === messageId) && message.ancestors && !message.ancestors.includes(messageId)
|
| 93 |
+
)
|
| 94 |
+
.map((message) => {
|
| 95 |
+
// remove the message from children if it's there
|
| 96 |
+
if (message.children && message.children.includes(messageId)) {
|
| 97 |
+
message.children = message.children.filter((child) => child !== messageId);
|
| 98 |
+
}
|
| 99 |
+
return message;
|
| 100 |
+
});
|
| 101 |
+
|
| 102 |
+
await collections.conversations.updateOne(
|
| 103 |
+
{ _id: conversation._id, ...authCondition(locals) },
|
| 104 |
+
{ $set: { messages: filteredMessages } }
|
| 105 |
+
);
|
| 106 |
+
|
| 107 |
+
return { from: "deleteBranch", ok: true };
|
| 108 |
+
},
|
| 109 |
+
};
|