Skip to content

Commit

Permalink
fix: show settings based on permissions and spaceShortcode hook cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle committed Sep 1, 2024
1 parent 4d491b0 commit af55113
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 90 deletions.
72 changes: 44 additions & 28 deletions apps/platform/trpc/routers/spaceRouter/spaceRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,37 +166,53 @@ export const spaceRouter = router({

const spaceIdsDedupe = Array.from(new Set([...memberSpaceIds]));

const orgMemberSpaces = await db.query.spaces.findMany({
where: and(
eq(spaces.orgId, org.id),
or(eq(spaces.type, 'open'), inArray(spaces.id, spaceIdsDedupe))
),
columns: {
publicId: true,
shortcode: true,
name: true,
description: true,
type: true,
avatarTimestamp: true,
convoPrefix: true,
inheritParentPermissions: true,
color: true,
icon: true,
personalSpace: true
},
with: {
parentSpace: {
columns: {
publicId: true
}
const orgMemberSpaces = await db.query.spaces
.findMany({
where: and(
eq(spaces.orgId, org.id),
or(eq(spaces.type, 'open'), inArray(spaces.id, spaceIdsDedupe))
),
columns: {
id: true,
publicId: true,
shortcode: true,
name: true,
description: true,
type: true,
avatarTimestamp: true,
convoPrefix: true,
inheritParentPermissions: true,
color: true,
icon: true,
personalSpace: true
},
subSpaces: {
columns: {
publicId: true
with: {
parentSpace: {
columns: {
publicId: true
}
},
subSpaces: {
columns: {
publicId: true
}
}
}
}
});
})
.then((spaces) =>
spaces.map((space) => {
// if the space is private, or the space id is in the array, then they can see Settings
if (
space.type === 'private' ||
(space.type === 'open' && spaceIdsDedupe.includes(space.id))
) {
return { ...space, canSeeSettings: true };
} else {
// otherwise, they can't see Settings
return { ...space, canSeeSettings: false };
}
})
);

const orgMemberPersonalSpaceQuery = await db.query.orgMembers.findFirst({
where: eq(orgMembers.id, org.memberId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const spaceSettingsRouter = router({
if (!spaceMembershipResponse.role) {
throw new TRPCError({
code: 'FORBIDDEN',
message: 'You are not a member of this Space'
message: "You don't have access to settings for this space"
});
}

Expand Down
64 changes: 29 additions & 35 deletions apps/web/src/app/[orgShortcode]/[spaceShortcode]/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import { z } from 'zod';

export default function SettingsPage() {
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode();
const spaceShortcode = useSpaceShortcode(true);

const [showSaved, setShowSaved] = useState(false);

Expand All @@ -100,9 +100,15 @@ export default function SettingsPage() {
<div className="flex w-full flex-col items-center overflow-y-auto">
<div className="flex max-w-screen-md flex-col gap-8 p-4">
{isLoading ? (
<div>Loading...</div>
<div className="flex items-center justify-center gap-2 text-center font-bold">
<SpinnerGap
className="size-4 animate-spin"
size={16}
/>
<span>Loading...</span>
</div>
) : !spaceSettings?.settings ? (
<div>Space Not Found</div>
<div>{`You don't have access to settings for this space`}</div>
) : (
<div className="flex w-full flex-col gap-8 p-0">
<div className="border-base-5 flex w-full flex-row items-center justify-between gap-4 border-b pb-2">
Expand All @@ -121,16 +127,12 @@ export default function SettingsPage() {

<div className="flex flex-col gap-1">
<NameField
orgShortcode={orgShortcode}
spaceShortcode={spaceShortcode}
initialValue={spaceSettings?.settings?.name}
showSaved={setShowSaved}
isSpaceAdmin={isSpaceAdmin}
/>

<DescriptionField
orgShortcode={orgShortcode}
spaceShortcode={spaceShortcode}
initialValue={spaceSettings?.settings?.description ?? ''}
showSaved={setShowSaved}
isSpaceAdmin={isSpaceAdmin}
Expand Down Expand Up @@ -164,22 +166,16 @@ export default function SettingsPage() {
</div>
</div>
<ColorField
orgShortcode={orgShortcode}
spaceShortcode={spaceShortcode}
initialValue={spaceSettings?.settings?.color}
showSaved={setShowSaved}
isSpaceAdmin={isSpaceAdmin}
/>
<VisibilityField
orgShortcode={orgShortcode}
spaceShortcode={spaceShortcode}
initialValue={spaceSettings?.settings?.type}
showSaved={setShowSaved}
isSpaceAdmin={isSpaceAdmin}
/>
<Workflows
orgShortcode={orgShortcode}
spaceShortcode={spaceShortcode}
showSaved={setShowSaved}
isSpaceAdmin={isSpaceAdmin}
/>
Expand All @@ -196,18 +192,16 @@ export default function SettingsPage() {
}

function NameField({
orgShortcode,
spaceShortcode,
initialValue,
showSaved,
isSpaceAdmin
}: {
orgShortcode: string;
spaceShortcode: string;
initialValue: string;
isSpaceAdmin: boolean;
showSaved: (value: boolean) => void;
}) {
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(true);
const { mutateAsync: setSpaceName, isSuccess: setSpaceNameSuccess } =
platform.spaces.settings.setSpaceName.useMutation();
const [editName, setEditName] = useState(initialValue);
Expand Down Expand Up @@ -254,11 +248,14 @@ function NameField({
label="Space Name"
value={editName}
onChange={(e) => setEditName(e.target.value)}
onBlur={() => setShowEditNameField(false)}
/>
</div>
) : (
<div className="flex flex-row items-center gap-2">
<span className="font-display text-lg">{initialValue}</span>
<span className="font-display text-lg">
{editName ?? initialValue}
</span>
<Button
variant={'ghost'}
size={'icon-sm'}
Expand All @@ -275,18 +272,16 @@ function NameField({
}

function DescriptionField({
orgShortcode,
spaceShortcode,
initialValue,
showSaved,
isSpaceAdmin
}: {
orgShortcode: string;
spaceShortcode: string;
initialValue: string;
isSpaceAdmin: boolean;
showSaved: (value: boolean) => void;
}) {
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(true);
const {
mutateAsync: setSpaceDescription,
isSuccess: setSpaceDescriptionSuccess
Expand Down Expand Up @@ -336,14 +331,15 @@ function DescriptionField({
label="Description"
value={editDescription}
onChange={(e) => setEditDescription(e.target.value)}
onBlur={() => setShowEditDescriptionField(false)}
/>
</div>
) : (
<div className="flex flex-row items-center gap-2">
{initialValue === '' ? (
<span className="text-base-10 text-xs">Description</span>
) : (
<span className="">{initialValue}</span>
<span>{editDescription ?? initialValue}</span>
)}
<Button
variant={'ghost'}
Expand All @@ -361,18 +357,17 @@ function DescriptionField({
}

function ColorField({
orgShortcode,
spaceShortcode,
initialValue,
showSaved,
isSpaceAdmin
}: {
orgShortcode: string;
spaceShortcode: string;
initialValue: string;
isSpaceAdmin: boolean;
showSaved: (value: boolean) => void;
}) {
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(true);

const { mutateAsync: setSpaceColor } =
platform.spaces.settings.setSpaceColor.useMutation();
const [activeColor, setActiveColor] = useState(initialValue);
Expand Down Expand Up @@ -436,18 +431,17 @@ function ColorField({
}

function VisibilityField({
orgShortcode,
spaceShortcode,
initialValue,
showSaved,
isSpaceAdmin
}: {
orgShortcode: string;
spaceShortcode: string;
initialValue: string;
isSpaceAdmin: boolean;
showSaved: (value: boolean) => void;
}) {
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(true);

const { data: canAddSpace } = platform.org.iCanHaz.space.useQuery(
{
orgShortcode: orgShortcode
Expand Down Expand Up @@ -554,16 +548,15 @@ function VisibilityField({
}

function Workflows({
orgShortcode,
spaceShortcode,
showSaved,
isSpaceAdmin
}: {
orgShortcode: string;
spaceShortcode: string;
isSpaceAdmin: boolean;
showSaved: (value: boolean) => void;
}) {
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(true);

const { data: spaceWorkflows, isLoading: workflowsLoading } =
platform.spaces.workflows.getSpacesWorkflows.useQuery({
orgShortcode: orgShortcode,
Expand Down Expand Up @@ -1586,6 +1579,7 @@ function DeleteWorkflowModal({
);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function Tags({
orgShortcode,
spaceShortcode
Expand Down
34 changes: 19 additions & 15 deletions apps/web/src/app/[orgShortcode]/_components/sidebar-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,21 @@ function SpaceItem({
{spaceData.name ?? 'Unnamed Space'}
</span>
</Link>
<div className="w-0 overflow-hidden transition-all group-hover:w-8">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
size="icon-sm"
variant="ghost">
<DotsThree />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
{/* TODO: Add in with the notifications
{spaceData.canSeeSettings && (
<div className="w-0 overflow-hidden transition-all group-hover:w-8">
{/* <DropdownMenu>
<DropdownMenuTrigger asChild> */}
<Button
size="icon-sm"
variant="ghost"
onClick={() =>
router.push(`/${orgShortCode}/${spaceData.shortcode}/settings`)
}>
<DotsThree />
</Button>
{/* </DropdownMenuTrigger>
<DropdownMenuContent>
TODO: Add in with the notifications
<DropdownMenuSub>
<DropdownMenuSubTrigger>Notifications</DropdownMenuSubTrigger>
<DropdownMenuPortal>
Expand All @@ -152,16 +156,16 @@ function SpaceItem({
</DropdownMenuPortal>
</DropdownMenuSub>
<DropdownMenuSeparator />
*/}
<DropdownMenuItem
<DropdownMenuItem
onSelect={() => {
router.push(`/${orgShortCode}/${spaceData.shortcode}/settings`);
}}>
Space Settings
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</DropdownMenu> */}
</div>
)}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function ReplyBox({
const { draft, setDraft, resetDraft } = useDraft(convoId);
const [editorText, setEditorText] = useState(draft.content);
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(false);
const spaceShortcode = useSpaceShortcode();
const [replyTo] = useAtom(replyToMessageAtom);
const [emailIdentity, setEmailIdentity] = useAtom(emailIdentityAtom);
const addConvoToCache = useUpdateConvoMessageList$Cache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ const DeleteButton = memo(function DeleteButton({
const removeConvoFromList = useDeleteConvo$Cache();
const { scopedNavigate } = useOrgScopedRouter();
const currentConvo = useCurrentConvoId();
const spaceShortcode = useSpaceShortcode(false);
const spaceShortcode = useSpaceShortcode();

const { mutate: deleteConvo, isPending: deletingConvo } =
platform.convos.deleteConvo.useMutation({
Expand Down Expand Up @@ -522,7 +522,7 @@ function DeleteModal({
const orgShortcode = useOrgShortcode();
const { scopedNavigate } = useOrgScopedRouter();
const currentConvo = useCurrentConvoId();
const spaceShortcode = useSpaceShortcode(false);
const spaceShortcode = useSpaceShortcode();
const removeConvoFromList = useDeleteConvo$Cache();

// const { mutate: hideConvo, isPending: hidingConvo } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function ConvoListBase({
linkBase
}: ConvoListBaseProps) {
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(false);
const spaceShortcode = useSpaceShortcode();
const [selections, setSelections] = useAtom(convoListSelection);
const [lastSelected, setLastSelected] = useAtom(lastSelectedConvo);

Expand Down
Loading

0 comments on commit af55113

Please sign in to comment.