Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: No option to install/disconnect app from app detail page #17997

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1e03777
fix: No option to install/disconnect app from app detail page
asadath1395 Dec 4, 2024
2a4e4f6
Merge with upstream
asadath1395 Dec 4, 2024
1cdd908
Fix types
asadath1395 Dec 4, 2024
2bb744f
Merge branch 'main' into fix-install-disconnect-app-details-page
asadath1395 Dec 5, 2024
9aab30c
Merge with upstream and resolve conflicts
asadath1395 Dec 11, 2024
fd1e41d
Merge branch 'fix-install-disconnect-app-details-page' of https://git…
asadath1395 Dec 11, 2024
283b2bd
Merge branch 'main' into fix-install-disconnect-app-details-page
asadath1395 Dec 12, 2024
764b83a
Merge branch 'main' into fix-install-disconnect-app-details-page
asadath1395 Dec 17, 2024
5838426
Merge branch 'main' into fix-install-disconnect-app-details-page
asadath1395 Jan 1, 2025
2f9888e
fix: Install button showing up even after it is installed for all tar…
hariombalhara Jan 1, 2025
454ffa2
chore: Simplified installOrDisconnectAppButton making it easy to read
hariombalhara Jan 1, 2025
06e33d5
Merge branch 'main' into fix-install-disconnect-app-details-page
asadath1395 Jan 2, 2025
6f4e90a
Merge branch 'main' into fix-install-disconnect-app-details-page
PeerRich Jan 6, 2025
81c8640
Merge branch 'main' into fix-install-disconnect-app-details-page
PeerRich Jan 7, 2025
e58568b
Merge branch 'main' into fix-install-disconnect-app-details-page
PeerRich Jan 9, 2025
d6e67dd
Merge branch 'main' into fix-install-disconnect-app-details-page
anikdhabal Jan 9, 2025
9992d8b
Merge branch 'main' into fix-install-disconnect-app-details-page
asadath1395 Jan 10, 2025
5c05bd5
Merge branch 'main' into fix-install-disconnect-app-details-page
keithwillcode Jan 15, 2025
19b0bff
Merge branch 'main' into fix-install-disconnect-app-details-page
PeerRich Jan 17, 2025
568f5ee
Merge branch 'main' into fix-install-disconnect-app-details-page
PeerRich Jan 17, 2025
ce8a7af
Merge branch 'main' into fix-install-disconnect-app-details-page
PeerRich Jan 18, 2025
3a74ccd
Merge branch 'main' into fix-install-disconnect-app-details-page
PeerRich Jan 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 107 additions & 76 deletions apps/web/components/apps/AppPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { App as AppType } from "@calcom/types/App";
import { Badge, Button, Icon, SkeletonButton, SkeletonText, showToast } from "@calcom/ui";

import { InstallAppButtonChild } from "./InstallAppButtonChild";
import { MultiDisconnectIntegration } from "./MultiDisconnectIntegration";

export type AppPageProps = {
name: string;
Expand Down Expand Up @@ -98,6 +99,11 @@ export const AppPage = ({
* which is caused by heavy queries in getServersideProps. This causes the loader to turn off before the page changes.
*/
const [isLoading, setIsLoading] = useState<boolean>(mutation.isPending);
const availableForTeams = doesAppSupportTeamInstall({
appCategories: categories,
concurrentMeetings: concurrentMeetings,
isPaid: !!paid,
});

const handleAppInstall = () => {
setIsLoading(true);
Expand All @@ -113,13 +119,7 @@ export const AppPage = ({
step: AppOnboardingSteps.EVENT_TYPES_STEP,
}),
});
} else if (
!doesAppSupportTeamInstall({
appCategories: categories,
concurrentMeetings: concurrentMeetings,
isPaid: !!paid,
})
) {
} else if (!availableForTeams) {
mutation.mutate({ type });
} else {
router.push(getAppOnboardingUrl({ slug, step: AppOnboardingSteps.ACCOUNTS_STEP }));
Expand All @@ -132,8 +132,14 @@ export const AppPage = ({
useGrouping: false,
}).format(price);

const [existingCredentials, setExistingCredentials] = useState<number[]>([]);
const [showDisconnectIntegration, setShowDisconnectIntegration] = useState(false);
const [existingCredentials, setExistingCredentials] = useState<
NonNullable<typeof appDbQuery.data>["credentials"]
>([]);

/**
* Marks whether the app is installed for all possible teams and the user.
*/
const [appInstalledForAllTargets, setAppInstalledForAllTargets] = useState(false);

const appDbQuery = trpc.viewer.appCredentialsByType.useQuery({ appType: type });

Expand All @@ -142,12 +148,15 @@ export const AppPage = ({
const data = appDbQuery.data;

const credentialsCount = data?.credentials.length || 0;
setShowDisconnectIntegration(
data?.userAdminTeams.length ? credentialsCount >= data?.userAdminTeams.length : credentialsCount > 0
);
setExistingCredentials(data?.credentials.map((credential) => credential.id) || []);
setExistingCredentials(data?.credentials || []);

const appInstalledForAllTargets =
availableForTeams && data?.userAdminTeams
? credentialsCount >= data?.userAdminTeams.length
: credentialsCount > 0;
setAppInstalledForAllTargets(appInstalledForAllTargets);
},
[appDbQuery.data]
[appDbQuery.data, availableForTeams]
);

const dependencyData = trpc.viewer.appsRouter.queryForDependencies.useQuery(dependencies, {
Expand All @@ -168,6 +177,89 @@ export const AppPage = ({
}
}, []);

const installOrDisconnectAppButton = () => {
if (appDbQuery.isPending) {
return <SkeletonButton className="h-10 w-24" />;
}

const MultiInstallButtonEl = (
<InstallAppButton
type={type}
disableInstall={disableInstall}
teamsPlanRequired={teamsPlanRequired}
render={({ useDefaultComponent, ...props }) => {
if (useDefaultComponent) {
props = {
...props,
onClick: () => {
handleAppInstall();
},
loading: isLoading,
};
}
return <InstallAppButtonChild multiInstall paid={paid} {...props} />;
}}
/>
);

const SingleInstallButtonEl = (
<InstallAppButton
type={type}
disableInstall={disableInstall}
teamsPlanRequired={teamsPlanRequired}
render={({ useDefaultComponent, ...props }) => {
if (useDefaultComponent) {
props = {
...props,
onClick: () => {
handleAppInstall();
},
loading: isLoading,
};
}
return <InstallAppButtonChild credentials={appDbQuery.data?.credentials} paid={paid} {...props} />;
}}
/>
);

return (
<div className="flex items-center space-x-3">
{isGlobal ||
(existingCredentials.length > 0 && allowedMultipleInstalls ? (
<div className="flex space-x-3">
<Button StartIcon="check" color="secondary" disabled>
{existingCredentials.length > 0
? t("active_install", { count: existingCredentials.length })
: t("default")}
</Button>
{!isGlobal && !appInstalledForAllTargets && MultiInstallButtonEl}
</div>
) : (
!appInstalledForAllTargets && SingleInstallButtonEl
))}

{existingCredentials.length > 0 && (
Comment on lines +226 to +241
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this logic a bit easier to understand by moving the JSX to variables above

<>
{existingCredentials.length > 1 ? (
<MultiDisconnectIntegration
credentials={existingCredentials}
onSuccess={() => appDbQuery.refetch()}
/>
) : (
<DisconnectIntegration
buttonProps={{ color: "secondary" }}
label={t("disconnect")}
credentialId={Number(existingCredentials[0].id)}
teamId={existingCredentials[0].teamId}
onSuccess={() => appDbQuery.refetch()}
/>
Comment on lines +243 to +255
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could make MultiDisconnectIntegration a wrapper over DisconnectIntegration so that it can handle single as well multiple disconnects. It keeps the consumer logic simple

)}
</>
)}
</div>
);
};

return (
<div className="relative mt-4 flex-1 flex-col items-start justify-start px-4 md:mt-0 md:flex md:px-8 lg:flex-row lg:px-0">
{hasDescriptionItems && (
Expand Down Expand Up @@ -240,68 +332,7 @@ export const AppPage = ({
)}
</header>
</div>
{!appDbQuery.isPending ? (
isGlobal ||
(existingCredentials.length > 0 && allowedMultipleInstalls ? (
<div className="flex space-x-3">
<Button StartIcon="check" color="secondary" disabled>
{existingCredentials.length > 0
? t("active_install", { count: existingCredentials.length })
: t("default")}
</Button>
{!isGlobal && (
<InstallAppButton
type={type}
disableInstall={disableInstall}
teamsPlanRequired={teamsPlanRequired}
render={({ useDefaultComponent, ...props }) => {
if (useDefaultComponent) {
props = {
...props,
onClick: () => {
handleAppInstall();
},
loading: isLoading,
};
}
return <InstallAppButtonChild multiInstall paid={paid} {...props} />;
}}
/>
)}
</div>
) : showDisconnectIntegration ? (
<DisconnectIntegration
buttonProps={{ color: "secondary" }}
label={t("disconnect")}
credentialId={existingCredentials[0]}
onSuccess={() => {
appDbQuery.refetch();
}}
/>
) : (
<InstallAppButton
type={type}
disableInstall={disableInstall}
teamsPlanRequired={teamsPlanRequired}
render={({ useDefaultComponent, ...props }) => {
if (useDefaultComponent) {
props = {
...props,
onClick: () => {
handleAppInstall();
},
loading: isLoading,
};
}
return (
<InstallAppButtonChild credentials={appDbQuery.data?.credentials} paid={paid} {...props} />
);
}}
/>
))
) : (
<SkeletonButton className="h-10 w-24" />
)}
{installOrDisconnectAppButton()}

{dependencies &&
(!dependencyData.isPending ? (
Expand Down
109 changes: 109 additions & 0 deletions apps/web/components/apps/MultiDisconnectIntegration.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { useState } from "react";

import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc/react";
import type { AppRouter } from "@calcom/trpc/server/routers/_app";
import {
Button,
Dropdown,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuItem,
DropdownItem,
Dialog,
ConfirmationDialogContent,
showToast,
} from "@calcom/ui";

import type { inferRouterOutputs } from "@trpc/server";

type RouterOutput = inferRouterOutputs<AppRouter>;
type Credentials = RouterOutput["viewer"]["appCredentialsByType"]["credentials"];

interface Props {
credentials: Credentials;
onSuccess?: () => void;
}

export function MultiDisconnectIntegration({ credentials, onSuccess }: Props) {
const { t } = useLocale();
const utils = trpc.useUtils();
const [credentialToDelete, setCredentialToDelete] = useState<{
id: number;
teamId: number | null;
name: string | null;
} | null>(null);
const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false);

const mutation = trpc.viewer.deleteCredential.useMutation({
onSuccess: () => {
showToast(t("app_removed_successfully"), "success");
onSuccess && onSuccess();
setConfirmationDialogOpen(false);
},
onError: () => {
showToast(t("error_removing_app"), "error");
setConfirmationDialogOpen(false);
},
async onSettled() {
await utils.viewer.connectedCalendars.invalidate();
await utils.viewer.integrations.invalidate();
},
});

return (
<>
<Dropdown>
<DropdownMenuTrigger asChild>
<Button color="secondary">{t("disconnect")}</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuLabel>
<div className="w-48 text-left text-xs">{t("disconnect_app_from")}</div>
</DropdownMenuLabel>
{credentials.map((cred) => (
<DropdownMenuItem key={cred.id}>
<DropdownItem
type="button"
color="destructive"
className="hover:bg-subtle hover:text-emphasis w-full border-0"
StartIcon={cred.teamId ? "users" : "user"}
onClick={() => {
setCredentialToDelete({
id: cred.id,
teamId: cred.teamId,
name: cred.team?.name || cred.user?.name || null,
});
setConfirmationDialogOpen(true);
}}>
<div className="flex flex-col text-left">
<span>{cred.team?.name || cred.user?.name || t("unnamed")}</span>
</div>
</DropdownItem>
</DropdownMenuItem>
))}
</DropdownMenuContent>
</Dropdown>

<Dialog open={confirmationDialogOpen} onOpenChange={setConfirmationDialogOpen}>
<ConfirmationDialogContent
variety="danger"
title={t("remove_app")}
confirmBtnText={t("yes_remove_app")}
onConfirm={() => {
if (credentialToDelete) {
mutation.mutate({
id: credentialToDelete.id,
...(credentialToDelete.teamId ? { teamId: credentialToDelete.teamId } : {}),
});
}
}}>
<p className="mt-5">
{t("are_you_sure_you_want_to_remove_this_app_from")} {credentialToDelete?.name || t("unnamed")}?
</p>
</ConfirmationDialogContent>
</Dialog>
</>
);
}
2 changes: 2 additions & 0 deletions apps/web/public/static/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -2891,6 +2891,8 @@
"calendar_conflict_settings": "Calendar Conflict Settings",
"select_calendars_conflict_check": "Select which calendars to check for conflicts",
"group_meeting": "Group Meeting",
"are_you_sure_you_want_to_remove_this_app_from": "Are you sure you want to remove this app from",
"disconnect_app_from": "Disconnect app from",
"salesforce_ignore_guests": "Do not create new records for guests added to the booking",
"google_meet": "Google Meet",
"zoom": "Zoom",
Expand Down
5 changes: 3 additions & 2 deletions packages/features/apps/components/DisconnectIntegration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { DisconnectIntegrationComponent, showToast } from "@calcom/ui";

export default function DisconnectIntegration(props: {
credentialId: number;
teamId?: number | null;
label?: string;
trashIcon?: boolean;
isGlobal?: boolean;
onSuccess?: () => void;
buttonProps?: ButtonProps;
}) {
const { t } = useLocale();
const { onSuccess, credentialId } = props;
const { onSuccess, credentialId, teamId } = props;
const [modalOpen, setModalOpen] = useState(false);
const utils = trpc.useUtils();

Expand All @@ -38,7 +39,7 @@ export default function DisconnectIntegration(props: {

return (
<DisconnectIntegrationComponent
onDeletionConfirmation={() => mutation.mutate({ id: credentialId })}
onDeletionConfirmation={() => mutation.mutate({ id: credentialId, ...(teamId ? { teamId } : {}) })}
isModalOpen={modalOpen}
onModalOpen={() => setModalOpen((prevValue) => !prevValue)}
{...props}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ export const appCredentialsByTypeHandler = async ({ ctx, input }: AppCredentials
],
type: input.appType,
},
include: {
user: {
select: {
name: true,
},
},
team: {
select: {
name: true,
},
},
},
});

// For app pages need to return which teams the user can install the app on
Expand Down
Loading