Skip to content

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

Implement table view settings

Hakan Shehud6673f0164b7
diff --git a/desktop/src/components/databases/boards/board-view.tsx b/desktop/src/components/databases/boards/board-view.tsx
index f2598680..3bee7028 100644
--- a/desktop/src/components/databases/boards/board-view.tsx
+++ b/desktop/src/components/databases/boards/board-view.tsx
@@ -1,10 +1,20 @@
import React from 'react';
import { BoardViewNode } from '@/types/databases';
+import { ViewTabs } from '@/components/databases/view-tabs';
interface BoardViewProps {
node: BoardViewNode;
}
export const BoardView = ({ node }: BoardViewProps) => {
- return <div>BoardView</div>;
+ return (
+ <React.Fragment>
+ <div className="mt-2 flex flex-row justify-between border-b">
+ <ViewTabs />
+ </div>
+ <div className="mt-2 w-full min-w-full max-w-full overflow-auto pr-5">
+ board view {node.id}
+ </div>
+ </React.Fragment>
+ );
};
diff --git a/desktop/src/components/databases/calendars/calendar-view.tsx b/desktop/src/components/databases/calendars/calendar-view.tsx
index 701036d1..6b35fe6f 100644
--- a/desktop/src/components/databases/calendars/calendar-view.tsx
+++ b/desktop/src/components/databases/calendars/calendar-view.tsx
@@ -1,10 +1,20 @@
import React from 'react';
import { CalendarViewNode } from '@/types/databases';
+import { ViewTabs } from '@/components/databases/view-tabs';
interface CalendarViewProps {
node: CalendarViewNode;
}
export const CalendarView = ({ node }: CalendarViewProps) => {
- return <div>CalendarView</div>;
+ return (
+ <React.Fragment>
+ <div className="mt-2 flex flex-row justify-between border-b">
+ <ViewTabs />
+ </div>
+ <div className="mt-2 w-full min-w-full max-w-full overflow-auto pr-5">
+ calendar view {node.id}
+ </div>
+ </React.Fragment>
+ );
};
diff --git a/desktop/src/components/databases/database-container-node.tsx b/desktop/src/components/databases/database-container-node.tsx
index 3d604ee8..e07f01e6 100644
--- a/desktop/src/components/databases/database-container-node.tsx
+++ b/desktop/src/components/databases/database-container-node.tsx
@@ -1,9 +1,7 @@
import React from 'react';
import { LocalNode } from '@/types/nodes';
-import { DatabaseContext } from '@/contexts/database';
-import { DatabaseViews } from './database-views';
-import { ScrollArea } from '@/components/ui/scroll-area';
import { useDatabaseQuery } from '@/queries/use-database-query';
+import { Database } from '@/components/databases/database';
interface DatabaseContainerNodeProps {
node: LocalNode;
@@ -20,13 +18,5 @@ export const DatabaseContainerNode = ({ node }: DatabaseContainerNodeProps) => {
return null;
}
- return (
- <DatabaseContext.Provider
- value={{ id: node.id, name: data?.name, fields: data?.fields }}
- >
- <ScrollArea className="h-full max-h-full w-full overflow-y-auto px-10 pb-12">
- <DatabaseViews key={node.id} views={data.views} />
- </ScrollArea>
- </DatabaseContext.Provider>
- );
+ return <Database node={data} />;
};
diff --git a/desktop/src/components/databases/database-views.tsx b/desktop/src/components/databases/database-views.tsx
deleted file mode 100644
index d25d308d..00000000
--- a/desktop/src/components/databases/database-views.tsx
+++ /dev/null
@@ -1,36 +0,0 @@
-import React from 'react';
-import { ViewTab } from '@/components/databases/view-tab';
-import { ViewActions } from '@/components/databases/view-actions';
-import { ViewNode } from '@/types/databases';
-import { View } from '@/components/databases/view';
-import { ViewCreateButton } from '@/components/databases/view-create-button';
-
-interface DatabaseViewsProps {
- views: ViewNode[];
-}
-
-export const DatabaseViews = ({ views }: DatabaseViewsProps) => {
- const [selectedView, setSelectedView] = React.useState<ViewNode | null>(
- views.length > 0 ? views[0] : null,
- );
-
- return (
- <div className="group/database">
- <div className="mt-2 flex flex-row justify-between border-b">
- <div className="flex flex-row items-center gap-3">
- {views.map((view) => (
- <ViewTab
- key={view.id}
- view={view}
- isActive={view.id === selectedView?.id}
- onClick={() => setSelectedView(view)}
- />
- ))}
- <ViewCreateButton />
- </div>
- {selectedView && <ViewActions viewId={selectedView.id} />}
- </div>
- {selectedView && <View node={selectedView} />}
- </div>
- );
-};
diff --git a/desktop/src/components/databases/database.tsx b/desktop/src/components/databases/database.tsx
new file mode 100644
index 00000000..27c2e8f8
--- /dev/null
+++ b/desktop/src/components/databases/database.tsx
@@ -0,0 +1,39 @@
+import React from 'react';
+import { DatabaseContext } from '@/contexts/database';
+import { DatabaseNode } from '@/types/databases';
+import { ScrollArea } from '@radix-ui/react-scroll-area';
+import { View } from '@/components/databases/view';
+
+interface DatabaseProps {
+ node: DatabaseNode;
+}
+
+export const Database = ({ node }: DatabaseProps) => {
+ const [activeViewId, setActiveViewId] = React.useState<string>(
+ node.views[0].id,
+ );
+ const activeView = node.views.find((view) => view.id === activeViewId);
+
+ React.useEffect(() => {
+ if (!activeView) {
+ setActiveViewId(node.views[0].id);
+ }
+ }, [node.views, activeViewId]);
+
+ return (
+ <DatabaseContext.Provider
+ value={{
+ id: node.id,
+ name: node.name,
+ fields: node.fields,
+ views: node.views,
+ activeViewId: activeViewId,
+ setActiveViewId: setActiveViewId,
+ }}
+ >
+ <ScrollArea className="group/database h-full max-h-full w-full overflow-y-auto px-10 pb-12">
+ {activeView && <View node={activeView} />}
+ </ScrollArea>
+ </DatabaseContext.Provider>
+ );
+};
diff --git a/desktop/src/components/databases/view-actions.tsx b/desktop/src/components/databases/tables/table-view-actions.tsx
similarity index 62%
rename from desktop/src/components/databases/view-actions.tsx
rename to desktop/src/components/databases/tables/table-view-actions.tsx
index 4bc55b41..da90abec 100644
--- a/desktop/src/components/databases/view-actions.tsx
+++ b/desktop/src/components/databases/tables/table-view-actions.tsx
@@ -1,16 +1,12 @@
import React from 'react';
-import { ViewSettingsButton } from '@/components/databases/view-settings-button';
import { ViewSortButton } from '@/components/databases/view-sort-button';
import { ViewFiltersPopover } from '@/components/databases/filters/view-filters-popover';
+import { TableViewSettingsPopover } from '@/components/databases/tables/table-view-settings-popover';
-interface ViewActionsProps {
- viewId: string;
-}
-
-export const ViewActions = ({ viewId }: ViewActionsProps) => {
+export const TableViewActions = () => {
return (
<div className="invisible flex flex-row items-center justify-end group-hover/database:visible">
- <ViewSettingsButton />
+ <TableViewSettingsPopover />
<ViewSortButton />
<ViewFiltersPopover />
</div>
diff --git a/desktop/src/components/databases/tables/table-view-settings-popover.tsx b/desktop/src/components/databases/tables/table-view-settings-popover.tsx
new file mode 100644
index 00000000..73463c60
--- /dev/null
+++ b/desktop/src/components/databases/tables/table-view-settings-popover.tsx
@@ -0,0 +1,174 @@
+import React from 'react';
+import isHotkey from 'is-hotkey';
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from '@/components/ui/popover';
+import { Icon } from '@/components/ui/icon';
+import { Input } from '@/components/ui/input';
+import { useTableView } from '@/contexts/table-view';
+import { Separator } from '@/components/ui/separator';
+import { useDatabase } from '@/contexts/database';
+import { cn } from '@/lib/utils';
+import { getFieldIcon } from '@/lib/databases';
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from '@/components/ui/tooltip';
+import { FieldDeleteDialog } from '@/components/databases/fields/field-delete-dialog';
+import { useNodeUpdateNameMutation } from '@/mutations/use-node-update-name-mutation';
+import { ViewDeleteDialog } from '@/components/databases/view-delete-dialog';
+
+export const TableViewSettingsPopover = () => {
+ const tableView = useTableView();
+ const database = useDatabase();
+ const nodeUpdateNameMutation = useNodeUpdateNameMutation();
+
+ const [name, setName] = React.useState(tableView.name);
+ const [open, setOpen] = React.useState(false);
+ const [openDelete, setOpenDelete] = React.useState(false);
+ const [deleteFieldId, setDeleteFieldId] = React.useState<string | null>(null);
+
+ const canEditDatabase = true;
+ const canEditView = true;
+ const canDeleteView = true;
+
+ return (
+ <React.Fragment>
+ <Popover open={open} onOpenChange={setOpen}>
+ <PopoverTrigger>
+ <div className="flex cursor-pointer items-center rounded-md p-1.5 hover:bg-gray-50">
+ <Icon name="settings-3-line" />
+ </div>
+ </PopoverTrigger>
+ <PopoverContent className="mr-4 flex w-[600px] flex-col gap-1.5 p-2">
+ <Input
+ value={name}
+ onChange={(e) => {
+ setName(e.target.value);
+ }}
+ onBlur={() => {
+ if (
+ name !== tableView.name &&
+ !nodeUpdateNameMutation.isPending
+ ) {
+ nodeUpdateNameMutation.mutate({ id: tableView.id, name });
+ }
+ }}
+ onKeyDown={(e) => {
+ if (
+ isHotkey('enter', e) &&
+ name !== tableView.name &&
+ !nodeUpdateNameMutation.isPending
+ ) {
+ nodeUpdateNameMutation.mutate({ id: tableView.id, name });
+ }
+ }}
+ />
+ <Separator />
+ <div className="flex flex-col gap-2 text-sm">
+ <p className="my-1 font-semibold">Fields</p>
+ {database.fields.map((field) => {
+ const isHidden = tableView.isHiddenField(field.id);
+
+ return (
+ <div
+ key={field.id}
+ className={cn(
+ 'flex flex-row items-center justify-between gap-2 p-0.5',
+ 'cursor-pointer rounded-md hover:bg-gray-50',
+ )}
+ >
+ <div className="flex flex-row items-center gap-2">
+ <Icon name={getFieldIcon(field.dataType)} />
+ <div>{field.name}</div>
+ </div>
+ <div className="flex flex-row items-center gap-2">
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Icon
+ name={isHidden ? 'eye-off-line' : 'eye-line'}
+ className={
+ canEditView ? 'cursor-pointer' : 'opacity-50'
+ }
+ onClick={() => {
+ if (!canEditView) return;
+
+ if (isHidden) {
+ tableView.showField(field.id);
+ } else {
+ tableView.hideField(field.id);
+ }
+ }}
+ />
+ </TooltipTrigger>
+ <TooltipContent className="flex flex-row items-center gap-2">
+ {isHidden
+ ? 'Show field in this view'
+ : 'Hide field from this view'}
+ </TooltipContent>
+ </Tooltip>
+ {canEditDatabase && (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Icon
+ name="delete-bin-line"
+ onClick={() => {
+ setDeleteFieldId(field.id);
+ setOpen(false);
+ }}
+ />
+ </TooltipTrigger>
+ <TooltipContent className="flex flex-row items-center gap-2">
+ Delete field from database
+ </TooltipContent>
+ </Tooltip>
+ )}
+ </div>
+ </div>
+ );
+ })}
+ </div>
+ {canEditView && canDeleteView && (
+ <React.Fragment>
+ <Separator />
+ <div className="flex flex-col gap-2 text-sm">
+ <p className="my-1 font-semibold">Settings</p>
+ <div
+ className="flex cursor-pointer flex-row items-center gap-1 rounded-md p-0.5 hover:bg-gray-50"
+ onClick={() => {
+ setOpenDelete(true);
+ setOpen(false);
+ }}
+ >
+ <Icon name="delete-bin-line" />
+ <span>Delete view</span>
+ </div>
+ </div>
+ </React.Fragment>
+ )}
+ </PopoverContent>
+ </Popover>
+ {deleteFieldId && (
+ <FieldDeleteDialog
+ id={deleteFieldId}
+ open={!!deleteFieldId}
+ onOpenChange={(open) => {
+ if (!open) {
+ setDeleteFieldId(null);
+ }
+ }}
+ />
+ )}
+ {openDelete && (
+ <ViewDeleteDialog
+ id={tableView.id}
+ open={openDelete}
+ onOpenChange={setOpenDelete}
+ />
+ )}
+ </React.Fragment>
+ );
+};
diff --git a/desktop/src/components/databases/tables/table-view-settings.tsx b/desktop/src/components/databases/tables/table-view-settings.tsx
deleted file mode 100644
index b72828bd..00000000
--- a/desktop/src/components/databases/tables/table-view-settings.tsx
+++ /dev/null
@@ -1,5 +0,0 @@
-import React from 'react';
-
-export const TableViewSettings = () => {
- return <div>TableViewSettings</div>;
-};
diff --git a/desktop/src/components/databases/tables/table-view.tsx b/desktop/src/components/databases/tables/table-view.tsx
index 1744a0db..48b195b4 100644
--- a/desktop/src/components/databases/tables/table-view.tsx
+++ b/desktop/src/components/databases/tables/table-view.tsx
@@ -12,6 +12,8 @@ import { useUpdateViewFieldIndexMutation } from '@/mutations/use-update-view-fie
import { useUpdateViewNameWidthMutation } from '@/mutations/use-update-view-name-width-mutation';
import { useUpdateViewFieldWidthMutation } from '@/mutations/use-update-view-field-width-mutation';
import { useUpdateViewHiddenFieldMutation } from '@/mutations/use-update-hidden-field-mutation';
+import { ViewTabs } from '@/components/databases/view-tabs';
+import { TableViewActions } from '@/components/databases/tables/table-view-actions';
interface TableViewProps {
node: TableViewNode;
@@ -60,6 +62,7 @@ export const TableView = ({ node }: TableViewProps) => {
<TableViewContext.Provider
value={{
id: node.id,
+ name: node.name,
fields,
hideField: (id: string) => {
if (hiddenFields.includes(id)) {
@@ -86,6 +89,7 @@ export const TableView = ({ node }: TableViewProps) => {
hide: false,
});
},
+ isHiddenField: (id: string) => hiddenFields.includes(id),
getNameWidth: () => nameWidth,
resizeName: (width: number) => {
setNameWidth(width);
@@ -178,6 +182,10 @@ export const TableView = ({ node }: TableViewProps) => {
},
}}
>
+ <div className="mt-2 flex flex-row justify-between border-b">
+ <ViewTabs />
+ <TableViewActions />
+ </div>
<div className="mt-2 w-full min-w-full max-w-full overflow-auto pr-5">
<TableViewHeader />
<TableViewBody />
diff --git a/desktop/src/components/databases/view-delete-dialog.tsx b/desktop/src/components/databases/view-delete-dialog.tsx
new file mode 100644
index 00000000..67b38dd9
--- /dev/null
+++ b/desktop/src/components/databases/view-delete-dialog.tsx
@@ -0,0 +1,58 @@
+import React from 'react';
+import {
+ AlertDialog,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from '@/components/ui/alert-dialog';
+import { Button } from '@/components/ui/button';
+import { useNodeDeleteMutation } from '@/mutations/use-node-delete-mutation';
+
+interface ViewDeleteDialogProps {
+ id: string;
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+}
+
+export const ViewDeleteDialog = ({
+ id,
+ open,
+ onOpenChange,
+}: ViewDeleteDialogProps) => {
+ const { mutate, isPending } = useNodeDeleteMutation();
+
+ return (
+ <AlertDialog open={open} onOpenChange={onOpenChange}>
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>
+ Are you sure you want delete this view?
+ </AlertDialogTitle>
+ <AlertDialogDescription>
+ This action cannot be undone. This view will no longer be accessible
+ and all data in the view will be lost.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <Button
+ variant="destructive"
+ disabled={isPending}
+ onClick={async () => {
+ mutate(id, {
+ onSuccess: () => {
+ onOpenChange(false);
+ },
+ });
+ }}
+ >
+ Delete
+ </Button>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ );
+};
diff --git a/desktop/src/components/databases/view-tabs.tsx b/desktop/src/components/databases/view-tabs.tsx
new file mode 100644
index 00000000..912394a1
--- /dev/null
+++ b/desktop/src/components/databases/view-tabs.tsx
@@ -0,0 +1,22 @@
+import React from 'react';
+import { ViewTab } from '@/components/databases/view-tab';
+import { ViewCreateButton } from '@/components/databases/view-create-button';
+import { useDatabase } from '@/contexts/database';
+
+export const ViewTabs = () => {
+ const database = useDatabase();
+
+ return (
+ <div className="flex flex-row items-center gap-3">
+ {database.views.map((view) => (
+ <ViewTab
+ key={view.id}
+ view={view}
+ isActive={view.id === database.activeViewId}
+ onClick={() => database.setActiveViewId(view.id)}
+ />
+ ))}
+ <ViewCreateButton />
+ </div>
+ );
+};
diff --git a/desktop/src/contexts/database.ts b/desktop/src/contexts/database.ts
index a400c7eb..f2f6f8cd 100644
--- a/desktop/src/contexts/database.ts
+++ b/desktop/src/contexts/database.ts
@@ -1,10 +1,13 @@
-import { FieldNode } from '@/types/databases';
+import { FieldNode, ViewNode } from '@/types/databases';
import { createContext, useContext } from 'react';
interface DatabaseContext {
id: string;
name: string;
fields: FieldNode[];
+ views: ViewNode[];
+ activeViewId: string;
+ setActiveViewId: (viewId: string) => void;
}
export const DatabaseContext = createContext<DatabaseContext>(
diff --git a/desktop/src/contexts/table-view.ts b/desktop/src/contexts/table-view.ts
index ffd0f7a8..8ebca77f 100644
--- a/desktop/src/contexts/table-view.ts
+++ b/desktop/src/contexts/table-view.ts
@@ -3,9 +3,11 @@ import { createContext, useContext } from 'react';
interface TableViewContext {
id: string;
+ name: string;
fields: FieldNode[];
hideField: (id: string) => void;
showField: (id: string) => void;
+ isHiddenField: (id: string) => boolean;
getNameWidth: () => number;
resizeName: (width: number) => void;
getFieldWidth: (id: string, type: FieldDataType) => number;
diff --git a/desktop/src/mutations/use-update-hidden-field-mutation.tsx b/desktop/src/mutations/use-update-hidden-field-mutation.tsx
index 60d34a28..79dfb0be 100644
--- a/desktop/src/mutations/use-update-hidden-field-mutation.tsx
+++ b/desktop/src/mutations/use-update-hidden-field-mutation.tsx
@@ -16,7 +16,7 @@ export const useUpdateViewHiddenFieldMutation = () => {
mutationFn: async (input: UpdateHiddenFieldInput) => {
const { viewId, fieldId, hide } = input;
- if (hide) {
+ if (!hide) {
const query = workspace.schema
.deleteFrom('node_attributes')
.where((eb) =>
diff --git a/desktop/src/queries/use-database-query.tsx b/desktop/src/queries/use-database-query.tsx
index cb94124b..5ddb3f20 100644
--- a/desktop/src/queries/use-database-query.tsx
+++ b/desktop/src/queries/use-database-query.tsx
@@ -348,6 +348,7 @@ const buildTableViewNode = (node: LocalNodeWithAttributes): TableViewNode => {
fieldWidths,
nameWidth: nameWidth,
versionId: node.versionId,
+ filters: [],
};
};
@@ -360,6 +361,7 @@ const buildBoardViewNode = (node: LocalNodeWithAttributes): BoardViewNode => {
id: node.id,
name: name ?? 'Unnamed',
type: 'board_view',
+ filters: [],
};
};
@@ -374,5 +376,6 @@ const buildCalendarViewNode = (
id: node.id,
name: name ?? 'Unnamed',
type: 'calendar_view',
+ filters: [],
};
};