Skip to content

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

Implement field reordering in table view

Hakan Shehud1be8322bd56
diff --git a/desktop/src/components/databases/fields/field-create-popover.tsx b/desktop/src/components/databases/fields/field-create-popover.tsx
index bb790a41..bfe25052 100644
--- a/desktop/src/components/databases/fields/field-create-popover.tsx
+++ b/desktop/src/components/databases/fields/field-create-popover.tsx
@@ -68,20 +68,23 @@ export const FieldCreatePopover = () => {
const { mutate, isPending } = useMutation({
mutationFn: async (values: z.infer<typeof formSchema>) => {
- const siblingsQuery = workspace.schema
+ const lastFieldQuery = workspace.schema
.selectFrom('nodes')
- .where('parent_id', '=', database.id)
+ .where((eb) =>
+ eb.and({
+ parent_id: database.id,
+ type: NodeTypes.Field,
+ }),
+ )
.selectAll()
+ .orderBy('index', 'desc')
+ .limit(1)
.compile();
- const result = await workspace.query(siblingsQuery);
- const siblings = result.rows;
- const maxIndex =
- siblings.length > 0
- ? siblings.sort((a, b) => compareString(a.index, b.index))[
- siblings.length - 1
- ].index
- : null;
+ const result = await workspace.query(lastFieldQuery);
+ const lastChild =
+ result.rows && result.rows.length > 0 ? result.rows[0] : null;
+ const maxIndex = lastChild?.index ? lastChild.index : null;
const index = generateNodeIndex(maxIndex, null);
const query = workspace.schema
@@ -123,7 +126,7 @@ export const FieldCreatePopover = () => {
return (
<Popover open={open} onOpenChange={setOpen} modal={true}>
<PopoverTrigger asChild>
- <Icon name="add-line" className="mr-2 h-3 w-3 cursor-pointer" />
+ <Icon name="add-line" className="ml-2 h-3 w-3 cursor-pointer" />
</PopoverTrigger>
<PopoverContent className="mr-5 w-128" side="bottom">
<Form {...form}>
diff --git a/desktop/src/components/databases/tables/table-view-field-header.tsx b/desktop/src/components/databases/tables/table-view-field-header.tsx
index 3a72f93e..a463ef0f 100644
--- a/desktop/src/components/databases/tables/table-view-field-header.tsx
+++ b/desktop/src/components/databases/tables/table-view-field-header.tsx
@@ -98,10 +98,9 @@ export const TableViewFieldHeader = ({ field }: TableViewFieldHeaderProps) => {
<div
className={cn(
'flex h-8 w-full cursor-pointer flex-row items-center gap-1 p-1 text-sm hover:bg-gray-50',
- {
- 'border-r-2 border-blue-300':
- dropMonitor.isOver && dropMonitor.canDrop,
- },
+ dropMonitor.isOver && dropMonitor.canDrop
+ ? 'border-r-2 border-blue-300'
+ : 'border-r',
)}
ref={dragDropRef as any}
>
diff --git a/desktop/src/components/databases/tables/table-view-header.tsx b/desktop/src/components/databases/tables/table-view-header.tsx
index 3614c6fe..64db6763 100644
--- a/desktop/src/components/databases/tables/table-view-header.tsx
+++ b/desktop/src/components/databases/tables/table-view-header.tsx
@@ -11,10 +11,8 @@ export const TableViewHeader = () => {
<div className="flex flex-row items-center gap-0.5">
<div style={{ width: '30px', minWidth: '30px' }} />
<TableViewNameHeader />
- {tableView.fields.map((field, index) => {
- return (
- <TableViewFieldHeader field={field} key={field.id} index={index} />
- );
+ {tableView.fields.map((field) => {
+ return <TableViewFieldHeader field={field} key={field.id} />;
})}
<FieldCreatePopover />
</div>
diff --git a/desktop/src/components/databases/tables/table-view-name-header.tsx b/desktop/src/components/databases/tables/table-view-name-header.tsx
index 6b4b833a..0aba1c24 100644
--- a/desktop/src/components/databases/tables/table-view-name-header.tsx
+++ b/desktop/src/components/databases/tables/table-view-name-header.tsx
@@ -66,10 +66,9 @@ export const TableViewNameHeader = () => {
<div
className={cn(
'flex h-8 w-full cursor-pointer flex-row items-center gap-1 p-1 text-sm hover:bg-gray-50',
- {
- 'border-r-2 border-blue-300':
- dropMonitor.isOver && dropMonitor.canDrop,
- },
+ dropMonitor.isOver && dropMonitor.canDrop
+ ? 'border-r-2 border-blue-300'
+ : 'border-r',
)}
ref={dropRef}
>
diff --git a/desktop/src/components/databases/tables/table-view.tsx b/desktop/src/components/databases/tables/table-view.tsx
index 485d3c8f..c5fb4dd5 100644
--- a/desktop/src/components/databases/tables/table-view.tsx
+++ b/desktop/src/components/databases/tables/table-view.tsx
@@ -12,6 +12,7 @@ import { useMutation } from '@tanstack/react-query';
import { sql } from 'kysely';
import { useWorkspace } from '@/contexts/workspace';
import { NeuronId } from '@/lib/id';
+import { generateNodeIndex } from '@/lib/nodes';
interface TableViewProps {
node: LocalNode;
@@ -137,18 +138,22 @@ export const TableView = ({ node }: TableViewProps) => {
setNameWidth(attrs.nameWidth ?? getDefaultNameWidth());
}, [node.versionId]);
+ const fields = React.useMemo(() => {
+ return database.fields
+ .filter((field) => !hiddenFields[field.id])
+ .sort((a, b) =>
+ compareString(
+ fieldIndexes[a.id] ?? a.index,
+ fieldIndexes[b.id] ?? b.index,
+ ),
+ );
+ }, [database.fields, hiddenFields, fieldIndexes]);
+
return (
<TableViewContext.Provider
value={{
id: node.id,
- fields: database.fields
- .filter((field) => !hiddenFields[field.id])
- .sort((a, b) =>
- compareString(
- fieldIndexes[a.id] ?? a.index,
- fieldIndexes[b.id] ?? b.index,
- ),
- ),
+ fields,
hideField: (id: string) => {
if (hiddenFields[id]) {
return;
@@ -180,46 +185,74 @@ export const TableView = ({ node }: TableViewProps) => {
updateFieldWidthsMutation.mutate({ fieldId: id, width });
},
moveField: (id, after) => {
- // const field = database.fields.find((f) => f.id === id);
- // if (!field) {
- // return;
- // }
- // const sortedFields = [...database.fields].sort((a, b) =>
- // compareString(
- // fieldIndexes[a.id] ?? a.index,
- // fieldIndexes[b.id] ?? b.index,
- // ),
- // );
- // if (sortedFields.length <= 1) {
- // return;
- // }
- // let beforeIndex: string | null = null;
- // let afterIndex: string | null = null;
- // if (after === 'name') {
- // const firstField = sortedFields[0];
- // if (firstField.id === id) {
- // return;
- // }
- // afterIndex = firstField.index;
- // } else {
- // const afterFieldArrayIndex = sortedFields.findIndex(
- // (f) => f.id === after,
- // );
- // if (afterFieldArrayIndex === -1) {
- // return;
- // }
- // beforeIndex = sortedFields[afterFieldArrayIndex].index;
- // afterIndex =
- // afterFieldArrayIndex < sortedFields.length - 1
- // ? sortedFields[afterFieldArrayIndex + 1].index
- // : null;
- // }
- // const newIndex = generateNodeIndex(beforeIndex, afterIndex);
- // setFieldIndexes({
- // ...fieldIndexes,
- // [id]: newIndex,
- // });
- // updateFieldIndexesMutation.mutate({ fieldId: id, index: newIndex });
+ const field = database.fields.find((f) => f.id === id);
+ if (!field) {
+ return;
+ }
+
+ if (database.fields.length <= 1) {
+ return;
+ }
+
+ const mergedIndexes = database.fields
+ .map((f) => {
+ return {
+ id: f.id,
+ databaseIndex: f.index,
+ viewIndex: fieldIndexes[f.id],
+ };
+ })
+ .sort((a, b) =>
+ compareString(
+ a.viewIndex ?? a.databaseIndex,
+ b.viewIndex ?? b.databaseIndex,
+ ),
+ );
+
+ let previousIndex: string | null = null;
+ let nextIndex: string | null = null;
+ if (after === 'name') {
+ const lowestIndex = mergedIndexes[0];
+ nextIndex = lowestIndex.viewIndex ?? lowestIndex.databaseIndex;
+ } else {
+ const afterFieldArrayIndex = mergedIndexes.findIndex(
+ (f) => f.id === after,
+ );
+ if (afterFieldArrayIndex === -1) {
+ return;
+ }
+
+ previousIndex =
+ mergedIndexes[afterFieldArrayIndex].viewIndex ??
+ mergedIndexes[afterFieldArrayIndex].databaseIndex;
+
+ if (afterFieldArrayIndex < mergedIndexes.length) {
+ nextIndex =
+ mergedIndexes[afterFieldArrayIndex + 1].viewIndex ??
+ mergedIndexes[afterFieldArrayIndex + 1].databaseIndex;
+ }
+ }
+
+ let newIndex = generateNodeIndex(previousIndex, nextIndex);
+
+ const lastDatabaseField = mergedIndexes.sort((a, b) =>
+ compareString(a.databaseIndex, b.databaseIndex),
+ )[mergedIndexes.length - 1];
+
+ const newPotentialFieldIndex = generateNodeIndex(
+ lastDatabaseField.databaseIndex,
+ null,
+ );
+
+ if (newPotentialFieldIndex === newIndex) {
+ newIndex = generateNodeIndex(previousIndex, newPotentialFieldIndex);
+ }
+
+ setFieldIndexes({
+ ...fieldIndexes,
+ [id]: newIndex,
+ });
+ updateFieldIndexesMutation.mutate({ fieldId: id, index: newIndex });
},
}}
>
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index e854d6f5..57080a5e 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -18,11 +18,11 @@ export const buildNodeWithChildren = (
};
export const generateNodeIndex = (
- before?: string | null,
- after?: string | null,
+ previous?: string | null,
+ next?: string | null,
) => {
- const lower = before === undefined ? null : before;
- const upper = after === undefined ? null : after;
+ const lower = previous === undefined ? null : previous;
+ const upper = next === undefined ? null : next;
return generateKeyBetween(lower, upper);
};