Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update the workspace access via UI

Kiran K10e5ad93f3d7
diff --git a/apps/web/app/api/folders/[folderId]/route.ts b/apps/web/app/api/folders/[folderId]/route.ts
index 209d908a12..fed23ab04a 100644
--- a/apps/web/app/api/folders/[folderId]/route.ts
+++ b/apps/web/app/api/folders/[folderId]/route.ts
@@ -10,11 +10,36 @@ import {
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
+// GET /api/folders/[folderId] - get information about a folder
+export const GET = withWorkspace(
+ async ({ params, workspace }) => {
+ const { folderId } = params;
+
+ const folder = await prisma.folder.findUniqueOrThrow({
+ where: {
+ id: folderId,
+ projectId: workspace.id,
+ },
+ include: {
+ users: true,
+ },
+ });
+
+ return NextResponse.json(folderSchema.parse(folder));
+ },
+ {
+ requiredPermissions: ["folders.read"],
+ },
+);
+
// PATCH /api/folders/[folderId] – update a folder for a workspace
export const PATCH = withWorkspace(
async ({ req, params, workspace, session }) => {
const { folderId } = params;
- const { name } = updateFolderBodySchema.parse(await parseRequestBody(req));
+
+ const { name, accessLevel } = updateFolderBodySchema.parse(
+ await parseRequestBody(req),
+ );
const folder = await prisma.folder.findUniqueOrThrow({
where: {
@@ -40,12 +65,11 @@ export const PATCH = withWorkspace(
},
data: {
name,
+ accessLevel,
},
});
- const response = folderSchema.parse(updatedFolder);
-
- return NextResponse.json(response);
+ return NextResponse.json(folderSchema.parse(updatedFolder));
} catch (error) {
if (error.code === "P2002") {
throw new DubApiError({
diff --git a/apps/web/app/api/folders/route.ts b/apps/web/app/api/folders/route.ts
index b1d3f52ebd..cd9c5c489e 100644
--- a/apps/web/app/api/folders/route.ts
+++ b/apps/web/app/api/folders/route.ts
@@ -27,9 +27,7 @@ export const GET = withWorkspace(
},
});
- const response = folderSchema.array().parse(folders);
-
- return NextResponse.json(response, {
+ return NextResponse.json(folderSchema.array().parse(folders), {
headers,
});
},
@@ -84,9 +82,7 @@ export const POST = withWorkspace(
},
});
- const response = folderSchema.parse(newFolder);
-
- return NextResponse.json(response, {
+ return NextResponse.json(folderSchema.parse(newFolder), {
headers,
status: 201,
});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page-client.tsx
index ec03001d69..c8b003dbf7 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page-client.tsx
@@ -1,23 +1,67 @@
"use client";
import { clientAccessCheck } from "@/lib/api/tokens/permissions";
+import {
+ FOLDER_WORKSPACE_ACCESS,
+ FOLDER_WORKSPACE_ACCESS_DESCRIPTION,
+} from "@/lib/link-folder/access";
+import { FolderProps } from "@/lib/link-folder/types";
import useUsers from "@/lib/swr/use-users";
import useWorkspace from "@/lib/swr/use-workspace";
import { WorkspaceUserProps } from "@/lib/types";
-import { Avatar, Button } from "@dub/ui";
-import { cn } from "@dub/utils";
-import { ChevronLeft } from "lucide-react";
+import { Avatar, Globe } from "@dub/ui";
+import { cn, fetcher } from "@dub/utils";
+import { ChevronLeft, FolderIcon } from "lucide-react";
import Link from "next/link";
+import { notFound } from "next/navigation";
import { useState } from "react";
+import { toast } from "sonner";
+import useSWR, { mutate } from "swr";
-export const FolderUsersPageClient = () => {
+export const FolderUsersPageClient = ({ folderId }: { folderId: string }) => {
const { users } = useUsers();
- const { role, slug } = useWorkspace();
+ const [isUpdating, setIsUpdating] = useState(false);
+ const { id: workspaceId, slug: workspaceSlug } = useWorkspace();
+ const [workspaceAccessLevel, setWorkspaceAccessLevel] = useState<string>();
+
+ const { data: folder, isLoading } = useSWR<FolderProps>(
+ `/api/folders/${folderId}?workspaceId=${workspaceId}`,
+ fetcher,
+ );
+
+ if (!isLoading && !folder) {
+ notFound();
+ }
+
+ const updateWorkspaceAccessLevel = async (accessLevel: string) => {
+ setIsUpdating(true);
+ setWorkspaceAccessLevel(accessLevel);
+
+ const response = await fetch(
+ `/api/folders/${folderId}?workspaceId=${workspaceId}`,
+ {
+ method: "PATCH",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ accessLevel }),
+ },
+ );
+
+ setIsUpdating(false);
+
+ if (!response.ok) {
+ const error = await response.json();
+ toast.error(error.message);
+ return;
+ }
+
+ toast.success("Workspace access updated!");
+ await mutate(`/api/folders/${folderId}?workspaceId=${workspaceId}`);
+ };
return (
<>
<Link
- href={`/${slug}/settings/folders`}
+ href={`/${workspaceSlug}/settings/folders`}
className="flex items-center gap-x-1"
>
<ChevronLeft className="size-4" />
@@ -25,48 +69,55 @@ export const FolderUsersPageClient = () => {
</Link>
<div className="rounded-lg border border-gray-200 bg-white">
- <div className="flex flex-col items-center justify-between space-y-3 border-b border-gray-200 p-5 sm:flex-row sm:space-y-0 sm:p-10">
- <div className="flex flex-col space-y-3">
- <h2 className="text-xl font-medium">People</h2>
- <p className="text-sm text-gray-500">
- Teammates that have access to this workspace.
- </p>
- </div>
- <div className="flex space-x-2">
- <Button
- text="Invite"
- className="h-9"
- disabledTooltip={
- clientAccessCheck({
- action: "workspaces.write",
- role,
- customPermissionDescription: "invite new teammates",
- }).error || undefined
- }
- />
- </div>
- </div>
-
- <div className="grid divide-y divide-gray-200">
- {users ? (
- users.length > 0 ? (
- users.map((user) => <UserCard key={user.id} user={user} />)
- ) : (
- <div className="flex flex-col items-center justify-center py-10">
- <img
- src="https://assets.dub.co/misc/video-park.svg"
- alt="No invitations sent"
- width={300}
- height={300}
- className="pointer-events-none -my-8"
- />
- <p className="text-sm text-gray-500">No invitations sent</p>
+ <div className="flex items-center justify-between border-b px-5 py-6 sm:flex-row sm:space-y-0">
+ {folder ? (
+ <>
+ <div className="flex items-center gap-x-4">
+ <div className="rounded-full bg-green-200 p-0.5 sm:block">
+ <div className="rounded-full border-2 border-white p-2 sm:p-2.5">
+ <FolderIcon className="size-3" />
+ </div>
+ </div>
+ <div className="flex flex-col gap-2">
+ <span className="text-sm font-semibold leading-none text-gray-900">
+ {folder.name}
+ </span>
+ <div className="flex items-center gap-1">
+ <Globe className="size-3.5 text-gray-500" />
+ <span className="text-[13px] font-normal leading-[14.30px] text-gray-500">
+ 100 links
+ </span>
+ </div>
+ </div>
</div>
- )
+
+ <select
+ className="rounded-md border border-gray-200 text-xs text-gray-900 focus:border-gray-600 focus:ring-gray-600"
+ value={workspaceAccessLevel || folder?.accessLevel}
+ disabled={isUpdating}
+ onChange={(e) => {
+ updateWorkspaceAccessLevel(e.target.value);
+ }}
+ >
+ {Object.values(FOLDER_WORKSPACE_ACCESS).map((access) => (
+ <option value={access} key={access}>
+ {FOLDER_WORKSPACE_ACCESS_DESCRIPTION[access]}
+ </option>
+ ))}
+ </select>
+ </>
) : (
- Array.from({ length: 5 }).map((_, i) => <UserPlaceholder key={i} />)
+ <FolderPlaceholder />
)}
</div>
+
+ <div className="grid divide-y divide-gray-200">
+ {users
+ ? users.map((user) => <UserCard key={user.id} user={user} />)
+ : Array.from({ length: 5 }).map((_, i) => (
+ <UserPlaceholder key={i} />
+ ))}
+ </div>
</div>
</>
);
@@ -86,14 +137,16 @@ const UserCard = ({ user }: { user: WorkspaceUserProps }) => {
return (
<div
key={id}
- className="flex items-center justify-between space-x-3 px-4 py-3 sm:pl-8"
+ className="flex items-center justify-between space-x-3 px-5 py-3"
>
<div className="flex items-start space-x-3">
<div className="flex items-center space-x-3">
<Avatar user={user} />
<div className="flex flex-col">
- <h3 className="text-sm font-medium">{name || email}</h3>
- <p className="text-xs text-gray-500">{email}</p>
+ <h3 className="text-xs font-medium text-gray-800">
+ {name || email}
+ </h3>
+ <p className="text-xs font-normal text-gray-400">{email}</p>
</div>
</div>
</div>
@@ -101,7 +154,7 @@ const UserCard = ({ user }: { user: WorkspaceUserProps }) => {
<div className="flex items-center gap-x-3">
<select
className={cn(
- "rounded-md border border-gray-200 text-xs text-gray-500 focus:border-gray-600 focus:ring-gray-600",
+ "rounded-md border border-gray-200 text-xs text-gray-900 focus:border-gray-600 focus:ring-gray-600",
{
"cursor-not-allowed bg-gray-100": permissionsError,
},
@@ -121,8 +174,24 @@ const UserCard = ({ user }: { user: WorkspaceUserProps }) => {
);
};
+const FolderPlaceholder = () => (
+ <>
+ <div className="flex items-center gap-x-4">
+ <div className="h-10 w-10 animate-pulse rounded-full bg-gray-200" />
+ <div className="flex flex-col gap-2">
+ <div className="h-4 w-32 animate-pulse rounded bg-gray-200" />
+ <div className="flex items-center gap-1">
+ <div className="h-3.5 w-3.5 animate-pulse rounded bg-gray-200" />
+ <div className="h-3 w-24 animate-pulse rounded bg-gray-200" />
+ </div>
+ </div>
+ </div>
+ <div className="h-6 w-24 animate-pulse rounded bg-gray-200" />
+ </>
+);
+
const UserPlaceholder = () => (
- <div className="flex items-center justify-between space-x-3 px-4 py-3 sm:px-8">
+ <div className="flex items-center justify-between space-x-3 px-5 py-3">
<div className="flex items-center space-x-3">
<div className="h-10 w-10 animate-pulse rounded-full bg-gray-200" />
<div className="flex flex-col">
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page.tsx
index 07a4372bb7..2d1a6fdf62 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/[folderId]/members/page.tsx
@@ -1,5 +1,9 @@
import { FolderUsersPageClient } from "./page-client";
-export default async function FolderUsersPage() {
- return <FolderUsersPageClient />;
+export default async function FolderUsersPage({
+ params,
+}: {
+ params: { folderId: string };
+}) {
+ return <FolderUsersPageClient folderId={params.folderId} />;
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page-client.tsx
index d34a90e57c..915a35d45b 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page-client.tsx
@@ -7,8 +7,9 @@ import { FolderCardPlaceholder } from "@/ui/folders/folder-card-placeholder";
import { useAddFolderModal } from "@/ui/modals/add-folder-modal";
import EmptyState from "@/ui/shared/empty-state";
import { TooltipContent } from "@dub/ui";
+import { Folder } from "@dub/ui/src/icons";
import { InfoTooltip } from "@dub/ui/src/tooltip";
-import { Folder } from "lucide-react";
+// import { Folder } from "lucide-react";
export const FoldersPageClient = () => {
const { folders, isLoading } = useFolders();
@@ -50,6 +51,7 @@ export const FoldersPageClient = () => {
icon={Folder}
title="No folders found for this workspace"
/>
+ <AddFolderButton />
</div>
)}
diff --git a/apps/web/lib/link-folder/access.ts b/apps/web/lib/link-folder/access.ts
index a823b454bb..0a8107565d 100644
--- a/apps/web/lib/link-folder/access.ts
+++ b/apps/web/lib/link-folder/access.ts
@@ -1,5 +1,5 @@
import { DubApiError } from "../api/errors";
-import { FolderWithPermissions } from "./types";
+import { FolderProps } from "./types";
export const FOLDER_WORKSPACE_ACCESS = {
CAN_VIEW: "can_view",
@@ -56,7 +56,7 @@ export const canPerformActionOnFolder = ({
userId,
action,
}: {
- folder: FolderWithPermissions;
+ folder: FolderProps;
userId: string;
action: keyof typeof FOLDER_PERMISSIONS;
}) => {
@@ -76,7 +76,7 @@ export const throwIfNotAllowed = ({
userId,
action,
}: {
- folder: FolderWithPermissions;
+ folder: FolderProps;
userId: string;
action: keyof typeof FOLDER_PERMISSIONS;
}) => {
diff --git a/apps/web/lib/link-folder/types.ts b/apps/web/lib/link-folder/types.ts
index f62c778b58..17f99134fb 100644
--- a/apps/web/lib/link-folder/types.ts
+++ b/apps/web/lib/link-folder/types.ts
@@ -1,9 +1,11 @@
-import { Folder, FolderUser } from "@prisma/client";
+import { FolderUser } from "@prisma/client";
+import z from "../zod";
+import { folderSchema } from "../zod/schemas/folders";
import { FOLDER_WORKSPACE_ACCESS } from "./access";
-export type FolderWithPermissions = Folder & {
- users: FolderUser[];
-};
-
export type FolderWorkspaceAccess =
(typeof FOLDER_WORKSPACE_ACCESS)[keyof typeof FOLDER_WORKSPACE_ACCESS];
+
+export type FolderProps = z.infer<typeof folderSchema> & {
+ users: FolderUser[];
+};
diff --git a/apps/web/lib/zod/schemas/folders.ts b/apps/web/lib/zod/schemas/folders.ts
index ea8daf8819..25f15e629b 100644
--- a/apps/web/lib/zod/schemas/folders.ts
+++ b/apps/web/lib/zod/schemas/folders.ts
@@ -19,4 +19,4 @@ export const createFolderBodySchema = z.object({
accessLevel: workspaceAccessLevelSchema,
});
-export const updateFolderBodySchema = createFolderBodySchema;
+export const updateFolderBodySchema = createFolderBodySchema.partial();
diff --git a/apps/web/scripts/seed-users.ts b/apps/web/scripts/seed-users.ts
index e8a0e43409..f5beefc74c 100644
--- a/apps/web/scripts/seed-users.ts
+++ b/apps/web/scripts/seed-users.ts
@@ -1,6 +1,6 @@
+import "dotenv-flow/config";
import { hashPassword } from "@/lib/auth/password";
import { prisma } from "@/lib/prisma";
-import "dotenv-flow/config";
async function main() {
const users = [
@@ -21,7 +21,7 @@ async function main() {
},
];
- const workspaceId = "cm163sjmf0003ivxf15rebtja";
+ const workspaceId = "cm1ae20j2000311gdbhejlt35";
// Create users
await prisma.user.createMany({
diff --git a/apps/web/ui/modals/add-folder-modal.tsx b/apps/web/ui/modals/add-folder-modal.tsx
index 0c456f39b0..73b480ff59 100644
--- a/apps/web/ui/modals/add-folder-modal.tsx
+++ b/apps/web/ui/modals/add-folder-modal.tsx
@@ -51,6 +51,7 @@ function AddFolderButton({
text="Create folder"
shortcut="C"
onClick={() => setShowAddFolderModal(true)}
+ className="w-fit"
/>
);
}
diff --git a/packages/ui/src/icons/nucleo/folder.tsx b/packages/ui/src/icons/nucleo/folder.tsx
new file mode 100644
index 0000000000..d3c93deea1
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/folder.tsx
@@ -0,0 +1,26 @@
+import { SVGProps } from "react";
+
+export function Folder(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ fill="none"
+ {...props}
+ >
+ <g>
+ <g
+ stroke="#374151"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ >
+ <path d="M2 7.778V4.222c0-.982.796-1.778 1.778-1.778h1.734c.54 0 1.049.245 1.387.665l.536.669h4.787c.982 0 1.778.795 1.778 1.778v2.528"></path>
+ <path d="M3.778 6h8.444C13.204 6 14 6.796 14 7.778v4c0 .982-.796 1.778-1.778 1.778H3.778A1.777 1.777 0 012 11.778v-4C2 6.796 2.796 6 3.778 6z"></path>
+ </g>
+ </g>
+ </svg>
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.ts
index a41482986c..e96989c45a 100644
--- a/packages/ui/src/icons/nucleo/index.ts
+++ b/packages/ui/src/icons/nucleo/index.ts
@@ -36,6 +36,7 @@ export * from "./filter-bars";
export * from "./filter2";
export * from "./flag-wavy";
export * from "./flag2";
+export * from "./folder";
export * from "./gaming-console";
export * from "./gear";
export * from "./gear2";