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.

disable edit for non-owner

Kiran K83c6b944633a
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 b25b3f2e22..874e522349 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
@@ -5,9 +5,11 @@ import {
FOLDER_WORKSPACE_ACCESS,
} from "@/lib/link-folder/constants";
import { FolderProps, FolderUserProps } from "@/lib/link-folder/types";
+import useUser from "@/lib/swr/use-user";
import useWorkspace from "@/lib/swr/use-workspace";
+import { AskToEditButton } from "@/ui/folders/ask-to-edit-button";
import { Avatar, Globe } from "@dub/ui";
-import { fetcher } from "@dub/utils";
+import { cn, fetcher } from "@dub/utils";
import { FolderUserRole } from "@prisma/client";
import { ChevronLeft, FolderIcon } from "lucide-react";
import Link from "next/link";
@@ -17,6 +19,7 @@ import { toast } from "sonner";
import useSWR from "swr";
export const FolderUsersPageClient = ({ folderId }: { folderId: string }) => {
+ const { user } = useUser();
const [isUpdating, setIsUpdating] = useState(false);
const { id: workspaceId, slug: workspaceSlug } = useWorkspace();
const [workspaceAccessLevel, setWorkspaceAccessLevel] = useState<string>();
@@ -70,6 +73,10 @@ export const FolderUsersPageClient = ({ folderId }: { folderId: string }) => {
await Promise.all([mutateFolder(), mutateUsers()]);
};
+ const currentUser = users?.find((user) => user.id === user?.id);
+
+ console.log(currentUser);
+
return (
<>
<Link
@@ -103,23 +110,29 @@ export const FolderUsersPageClient = ({ folderId }: { folderId: string }) => {
</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.keys(FOLDER_WORKSPACE_ACCESS).map((access) => (
- <option value={access} key={access}>
- {FOLDER_WORKSPACE_ACCESS[access]}
+ {currentUser?.role === "owner" && (
+ <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.keys(FOLDER_WORKSPACE_ACCESS).map((access) => (
+ <option value={access} key={access}>
+ {FOLDER_WORKSPACE_ACCESS[access]}
+ </option>
+ ))}
+ <option value="" key="no-access">
+ No access
</option>
- ))}
- <option value="" key="no-access">
- No access
- </option>
- </select>
+ </select>
+ )}
+
+ {currentUser?.role === "viewer" || currentUser?.role === null ? (
+ <AskToEditButton folder={folder} workspaceId={workspaceId!} />
+ ) : null}
</>
) : (
<FolderPlaceholder />
@@ -131,15 +144,25 @@ export const FolderUsersPageClient = ({ folderId }: { folderId: string }) => {
? Array.from({ length: 5 }).map((_, i) => (
<UserPlaceholder key={i} />
))
- : users?.map((user) => <UserCard key={user.id} user={user} />)}
+ : users?.map((user) => (
+ <UserCard key={user.id} user={user} currentUser={currentUser} />
+ ))}
</div>
</div>
</>
);
};
-const UserCard = ({ user }: { user: FolderUserProps }) => {
- const [role, setRole] = useState<FolderUserRole | "">(user.role);
+const UserCard = ({
+ user,
+ currentUser,
+}: {
+ user: FolderUserProps;
+ currentUser?: FolderUserProps;
+}) => {
+ const [role, setRole] = useState<FolderUserRole | "">(user.role || "");
+
+ const canUpdateRole = currentUser?.role === "owner";
return (
<div
@@ -160,8 +183,14 @@ const UserCard = ({ user }: { user: FolderUserProps }) => {
<div className="flex items-center gap-x-3">
<select
- className="rounded-md border border-gray-200 text-xs text-gray-900 focus:border-gray-600 focus:ring-gray-600"
- value={role || ""}
+ className={cn(
+ "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": !canUpdateRole,
+ },
+ )}
+ value={role}
+ disabled={!canUpdateRole}
onChange={(e) => {
setRole(e.target.value as FolderUserRole);
}}
diff --git a/apps/web/ui/folders/ask-to-edit-button.tsx b/apps/web/ui/folders/ask-to-edit-button.tsx
new file mode 100644
index 0000000000..88b66f6474
--- /dev/null
+++ b/apps/web/ui/folders/ask-to-edit-button.tsx
@@ -0,0 +1,45 @@
+"use client";
+
+import { requestFolderEditAccessAction } from "@/lib/actions/request-folder-edit-access";
+import { FolderProps } from "@/lib/types";
+import { Button } from "@dub/ui";
+import { useAction } from "next-safe-action/hooks";
+import { toast } from "sonner";
+
+interface AskToEditButtonProps {
+ folder: FolderProps;
+ workspaceId: string;
+}
+
+export const AskToEditButton = ({
+ folder,
+ workspaceId,
+}: AskToEditButtonProps) => {
+ const { executeAsync, isExecuting } = useAction(
+ requestFolderEditAccessAction,
+ {
+ onSuccess: () => {
+ toast.success("Request sent to folder owner.");
+ },
+ onError: ({ error }) => {
+ toast.error(error.serverError?.serverError);
+ },
+ },
+ );
+
+ return (
+ <Button
+ text={isExecuting ? "Sending..." : "Ask to edit"}
+ variant="outline"
+ className="h-8 w-fit rounded-md border border-gray-200 text-gray-900"
+ disabled={isExecuting}
+ loading={isExecuting}
+ onClick={() =>
+ executeAsync({
+ workspaceId: workspaceId!,
+ folderId: folder.id,
+ })
+ }
+ />
+ );
+};
diff --git a/apps/web/ui/folders/folder-card.tsx b/apps/web/ui/folders/folder-card.tsx
index 39d1c9f92e..6349cd554b 100644
--- a/apps/web/ui/folders/folder-card.tsx
+++ b/apps/web/ui/folders/folder-card.tsx
@@ -1,19 +1,17 @@
"use client";
-import { requestFolderEditAccessAction } from "@/lib/actions/request-folder-edit-access";
import useWorkspace from "@/lib/swr/use-workspace";
import { FolderProps } from "@/lib/types";
import { Button, PenWriting, Popover, Users } from "@dub/ui";
import { Globe } from "@dub/ui/src/icons";
import { cn, nFormatter } from "@dub/utils";
import { FolderIcon } from "lucide-react";
-import { useAction } from "next-safe-action/hooks";
import { useRouter } from "next/navigation";
import { useState } from "react";
-import { toast } from "sonner";
import { useDeleteFolderModal } from "../modals/delete-folder-modal";
import { useRenameFolderModal } from "../modals/rename-folder-modal";
import { Delete, ThreeDots } from "../shared/icons";
+import { AskToEditButton } from "./ask-to-edit-button";
interface LinksCount {
folderId: string;
@@ -36,18 +34,6 @@ export const FolderCard = ({ folder, linksCount }: FolderCardProps) => {
const { DeleteFolderModal, setShowDeleteFolderModal } =
useDeleteFolderModal(folder);
- const { executeAsync, isExecuting } = useAction(
- requestFolderEditAccessAction,
- {
- onSuccess: () => {
- toast.success("Request sent to folder owner.");
- },
- onError: ({ error }) => {
- toast.error(error.serverError?.serverError);
- },
- },
- );
-
const linkCount =
linksCount?.find((link) => link.folderId === folder.id)?.count || 0;
@@ -63,19 +49,7 @@ export const FolderCard = ({ folder, linksCount }: FolderCardProps) => {
</div>
</div>
<div className="flex items-center gap-2">
- <Button
- text={isExecuting ? "Sending..." : "Ask to edit"}
- variant="outline"
- className="h-8 rounded-md border border-gray-200"
- disabled={isExecuting}
- loading={isExecuting}
- onClick={() =>
- executeAsync({
- workspaceId: workspaceId!,
- folderId: folder.id,
- })
- }
- />
+ <AskToEditButton folder={folder} workspaceId={workspaceId!} />
<Popover
content={