Skip to content

Commit

Permalink
feat: display shared connection different
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Mar 31, 2024
1 parent 936ccc7 commit 6e745bd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/app/api/database/[database_id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ export const GET = withDatabaseOperation(async ({ database: databaseInfo }) => {
});

export const DELETE = withDatabaseOperation(
async ({ database: databaseInfo, permission }) => {
if (!permission.isOwner) {
async ({ database: databaseInfo, user }) => {
const isOriginalOwner = databaseInfo.userId === user.id;

if (!isOriginalOwner) {
return NextResponse.json({ error: "Unauthorization" }, { status: 500 });
}

Expand All @@ -55,7 +57,7 @@ export const DELETE = withDatabaseOperation(
);

export const PUT = withDatabaseOperation(
async ({ database: databaseInfo, permission, body }) => {
async ({ database: databaseInfo, body, user }) => {
const parsed = databaseSchema.safeParse(body);

if (!parsed.success) {
Expand All @@ -69,7 +71,8 @@ export const PUT = withDatabaseOperation(

const data = parsed.data;

if (!permission.isOwner) {
const isOriginalOwner = databaseInfo.userId === user.id;
if (!isOriginalOwner) {
return NextResponse.json({ error: "Unauthorization" }, { status: 500 });
}

Expand Down
12 changes: 11 additions & 1 deletion src/app/api/databases/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SavedConnectionItem } from "@/app/connect/saved-connection-storage";
import { db } from "@/db";
import { database, database_user_role } from "@/db/schema";
import { database, database_user_role, user as userTable } from "@/db/schema";
import withUser from "@/lib/with-user";
import { and, desc, eq, isNull } from "drizzle-orm";
import { NextResponse } from "next/server";
Expand All @@ -12,6 +12,7 @@ export const GET = withUser(async ({ user }) => {
.select()
.from(database_user_role)
.innerJoin(database, eq(database.id, database_user_role.databaseId))
.innerJoin(userTable, eq(userTable.id, database.userId))
.where(
and(eq(database_user_role.userId, user.id), isNull(database.deletedAt))
)
Expand All @@ -27,6 +28,15 @@ export const GET = withUser(async ({ user }) => {
description: d.database.description,
storage: "remote",
label: d.database.color,
shared:
d.database.userId === d.database_user_role.userId
? undefined
: {
sharedBy: {
id: d.user.id,
name: d.user.name,
},
},
} as SavedConnectionItem)
),
});
Expand Down
24 changes: 19 additions & 5 deletions src/app/connect/saved-connection-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ContextMenuItem,
ContextMenuSeparator,
} from "@/components/ui/context-menu";
import { LucidePencil, LucideTrash } from "lucide-react";
import { LucidePencil, LucideTrash, LucideUsers } from "lucide-react";
import { useState } from "react";
import {
SavedConnectionItem,
Expand All @@ -28,7 +28,14 @@ export default function ConnectionItemCard({

return (
<ContextMenu onOpenChange={setOpen}>
<ContextMenuTrigger>
<ContextMenuTrigger
disabled={!!conn.shared}
onContextMenu={(e) => {
if (conn.shared) {
e.preventDefault();
}
}}
>
<Link
className={cn(
"border rounded w-[285px] flex overflow-hidden hover:border-secondary hover:bg-secondary",
Expand All @@ -55,9 +62,16 @@ export default function ConnectionItemCard({
</div>
<div className="p-2">
<h2 className="line-clamp-1">{conn.name}</h2>
<p className="text-gray-600 text-xs line-clamp-2 h-8">
{conn.description || <i>No description</i>}
</p>
{conn.shared ? (
<p className="text-gray-600 text-xs line-clamp-2 h-8 mt-1">
<LucideUsers className="w-4 h-4 mr-1 inline" />
Shared by <strong>{conn.shared.sharedBy.name}</strong>
</p>
) : (
<p className="text-gray-600 text-xs line-clamp-2 h-8">
{conn.description || <i>No description</i>}
</p>
)}
</div>
</Link>
</ContextMenuTrigger>
Expand Down
4 changes: 4 additions & 0 deletions src/app/connect/saved-connection-storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiUser } from "@/lib/api/api-database-response";
import parseSafeJson from "../../lib/json-safe";

export const DRIVER_DETAIL = Object.freeze({
Expand Down Expand Up @@ -60,6 +61,9 @@ export interface SavedConnectionItem {
name: string;
description?: string;
label?: SavedConnectionLabel;
shared?: {
sharedBy: ApiUser;
};
}

export interface SavedConnectionItemConfigConfig {
Expand Down
2 changes: 2 additions & 0 deletions src/components/table-cell/createEditableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ function InputCellEditor({
if (y >= state.getRowsCount()) return;

shouldExit.current = false;
applyChange(value, false);

state.setFocus(y, x);
state.scrollToFocusCell(x === 0 ? "left" : "right", "bottom");
e.preventDefault();
Expand Down

0 comments on commit 6e745bd

Please sign in to comment.