Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Refactor node sorting

Hakan Shehub0bafb01d7d8
diff --git a/desktop/src/components/workspaces/sidebar-node-children.tsx b/desktop/src/components/workspaces/sidebar-node-children.tsx
index cda192d8..9cc1f1af 100644
--- a/desktop/src/components/workspaces/sidebar-node-children.tsx
+++ b/desktop/src/components/workspaces/sidebar-node-children.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { LocalNode } from '@/types/nodes';
import { SidebarNode } from '@/components/workspaces/sidebar-node';
import { useSidebar } from '@/contexts/sidebar';
+import { compareNodeIndex } from '@/lib/nodes';
interface SidebarNodeChildrenProps {
node: LocalNode;
@@ -11,15 +12,7 @@ export const SidebarNodeChildren = ({ node }: SidebarNodeChildrenProps) => {
const sidebar = useSidebar();
const children: LocalNode[] = sidebar.nodes
.filter((n) => n.parentId === node.id)
- .sort((a, b) => {
- if (a.index < b.index) {
- return -1;
- } else if (a.index > b.index) {
- return 1;
- }
-
- return 0;
- });
+ .sort((a, b) => compareNodeIndex(a, b));
return (
<React.Fragment>
diff --git a/desktop/src/hooks/use-conversation.tsx b/desktop/src/hooks/use-conversation.tsx
index 1697d2bd..9b9439b7 100644
--- a/desktop/src/hooks/use-conversation.tsx
+++ b/desktop/src/hooks/use-conversation.tsx
@@ -3,11 +3,15 @@ import { useWorkspace } from '@/contexts/workspace';
import { JSONContent } from '@tiptap/core';
import { NeuronId } from '@/lib/id';
import { LeafNodeTypes, NodeTypes } from '@/lib/constants';
-import { buildNodeWithChildren, generateNodeIndex, mapNode } from '@/lib/nodes';
+import {
+ buildNodeWithChildren,
+ compareNodeId,
+ generateNodeIndex,
+ mapNode,
+} from '@/lib/nodes';
import { useQuery } from '@tanstack/react-query';
import { sql } from 'kysely';
import { CreateNode, SelectNode } from '@/data/schemas/workspace';
-import { hashCode } from '@/lib/utils';
import { MessageNode } from '@/types/messages';
interface useConversationResult {
@@ -130,15 +134,7 @@ const buildMessages = (
messages.push(message);
}
- return messages.sort((a, b) => {
- if (a.id > b.id) {
- return 1;
- } else if (a.id < b.id) {
- return -1;
- } else {
- return 0;
- }
- });
+ return messages.sort((a, b) => compareNodeId(a, b));
};
const buildMessageCreateNodes = (
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index c8d05337..91608f9e 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -54,3 +54,13 @@ export const compareNodeIndex = (a: LocalNode, b: LocalNode): number => {
return 0;
};
+
+export const compareNodeId = (a: LocalNode, b: LocalNode): number => {
+ if (a.id < b.id) {
+ return -1;
+ } else if (a.id > b.id) {
+ return 1;
+ }
+
+ return 0;
+};