Files
cal.diy/apps/web/components/apps/hitpay/Setup.tsx
T
d6546c3107 feat: upgrade tailwind v4 (#24598)
* chore: refactor config files to prevent migration tool errors

* refactor: upgrade with the tailwind migration tool

* chore: restore pre-commit command + mc

* refactor(wip): update dependencies and migrate to Tailwind CSS v4 (mainly web)

* chore: resolve Tailwind v4 migration conflicts from merging main

* chore: remove unused Tailwind packages from config and update dependencies for v4 migration

* chore: uncomment Tailwind CSS utility classes in globals.css

* fix: resolve token conflicts between calcom and coss ui

* fix: textarea scrollbar

* Fix CUI-16

* fix: added @tailwindcss/forms plugin and cleaned up CSS classes in various components to remove unnecessary dark mode styles

* fix: selects and inputs of different sizes

* fix: remove unnecessary leading-20 class from modal titles in various components

* fix: update Checkbox component styles to remove unnecessary border on checked state

* fix: clean up styles in RequiresConfirmationController, Checkbox, and Radio components to enhance consistency

* fix: update button and filter component styles to remove unnecessary rounded classes for consistency

* fix: calendar

* fix: update KBarSearch

* fix: refine styles in Empty and Checkbox components for improved consistency

* Fix focus state email input

* fix: update button hover and active states to use 'not-disabled' instead of 'enabled'

* fix: line-height issues

* fix: update class name for muted background in BookingListItem component

* fix: sidebar spacing

* chore: update class names to use new Tailwind CSS color utilities

* fix embed

* chore: upgrade Tailwind CSS to version 4.1.16 and update related dependencies

* Map css variables and add a playground test for heavy css customization

* suggestion for coss-ui

* refactor: update CSS variable usage and clean up styles

- Replace instances of `--cal-brand-color` with `--cal-brand` in embed-related HTML files.
- Remove the now-unnecessary `addAppCssVars` function from the embed core.
- Import theme tokens in the embed core styles for better consistency.
- Clean up whitespace and formatting in CSS files for improved readability.
- Add a comment in `tokens.css` regarding its usage in both embed and webapp contexts.

* Handle within tokens.css instead of fixing coss-ui

* Remove initial, not needed. Also, remove tailwind.config.js as tailwidn scans the html automaically

* fix: examples app breaking

* fix: modal not resizing correctly

* feat: upgrade atoms to tailwind v4

* fix: atoms build breaking

* fix: atoms build breaking

* chore: upgrate examples/base to tailwind 4

* chore: update globals.css

* fix: add missing scheduler css variables

* fix: PlatformAdditionalCalendarSelector

* chore: update global styles

* chore: update tailwindcss and postcss dependencies to stable versions

* chore: remove unneeded class

* fix: dialog and toast animation

* fix: replace flex-shrink-0 with shrink-0 for consistent styling in various components

* fix: dialog modal for Apple connect

* add margin in SaveFilterSegmentButton

* Fix radix button nested states

* add cursor pointer to buttons but keep dsabled state

* Fix commandK selectors and adds cursor pointer

* Fix teams filter

* fix - round checkboxes

* fix filter checkbox

* fix select indicator's margin

* command group font size

* style: fix badge and tooltip radius

* chore: remove unneeded files

* Delete PR_REVIEW_MANAGED_EVENT_REASSIGNMENT.md

* remove ui-playground leftover

* fix: add missing react phone input styles in atoms

* Delete managed-event-reassignment-flow-and-architecture.mermaid

* fix: inter font not loading

* Add theme to skeleton container so that it can support dark mode

* fix: create custom stack-y-* utilities post tw4 upgrade

* fix: typo

* fix: atoms stack class + remove unused css file

* fix default radius valiue

* fix space-y in embed

* fix skeleton background

* Hardcode radius values to match production

* fix border in embed

* add missing externalThemeClass

* feat: create a custom stack-y-* utility

* fix: add stack utility to atom global css

* fix: Skeleton loader class modalbox

* Add stack-y utility in embed

* fix: add missing stack utilities in atoms globals.css

* update yarn.lock

* add popover portla

* update

---------

Co-authored-by: Sean Brydon <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
Co-authored-by: Ryukemeister <[email protected]>
Co-authored-by: cal.com <[email protected]>
Co-authored-by: Eunjae Lee <[email protected]>
Co-authored-by: Anik Dhabal Babu <[email protected]>
2025-11-25 17:32:28 -03:00

303 lines
9.7 KiB
TypeScript

import { zodResolver } from "@hookform/resolvers/zod";
import { useSession } from "next-auth/react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useState, useEffect } from "react";
import { useForm } from "react-hook-form";
import { Toaster } from "sonner";
import { z } from "zod";
import AppNotInstalledMessage from "@calcom/app-store/_components/AppNotInstalledMessage";
import KeyField from "@calcom/app-store/hitpay/components/KeyInput";
import { hitpayCredentialKeysSchema } from "@calcom/app-store/hitpay/lib/hitpayCredentialKeysSchema";
import type { IHitPaySetupProps } from "@calcom/app-store/hitpay/pages/setup/_getServerSideProps";
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc";
import { Button } from "@calcom/ui/components/button";
import { Switch } from "@calcom/ui/components/form";
import { Icon } from "@calcom/ui/components/icon";
import { showToast } from "@calcom/ui/components/toast";
export default function HitPaySetup(props: IHitPaySetupProps) {
const params = useCompatSearchParams();
if (params?.get("callback") === "true") {
return <HitPaySetupCallback />;
}
return <HitPaySetupPage {...props} />;
}
function HitPaySetupCallback() {
const [error, setError] = useState<string | null>(null);
const searchParams = useCompatSearchParams();
useEffect(() => {
if (!searchParams) {
return;
}
if (!window.opener) {
setError("Something went wrong. Opener not available. Please contact [email protected]");
return;
}
const code = searchParams?.get("code");
const error = searchParams?.get("error");
if (!code) {
setError("declined");
}
if (error) {
setError(error);
return;
}
window.opener.postMessage({
type: "hitpay:oauth:success",
payload: { code },
});
window.close();
}, [searchParams]);
return (
<div>
{error && <p>Authorization failed: {error}</p>}
{!error && <p>Connecting...</p>}
</div>
);
}
function HitPaySetupPage(props: IHitPaySetupProps) {
const [loading, setLoading] = useState<boolean>(false);
const [updatable, setUpdatable] = useState<boolean>(false);
const [isSandbox, SetIsSandbox] = useState<boolean>(props.isSandbox || false);
const [keyData, setKeyData] = useState<
| {
apiKey: string;
saltKey: string;
}
| undefined
>();
const session = useSession();
const router = useRouter();
const { t } = useLocale();
const settingsSchema = z.object({
apiKey: z
.string()
.trim()
.min(64)
.max(128, {
message: t("max_limit_allowed_hint", { limit: 128 }),
}),
saltKey: z
.string()
.trim()
.min(64)
.max(128, {
message: t("max_limit_allowed_hint", { limit: 128 }),
}),
});
const integrations = trpc.viewer.apps.integrations.useQuery({ variant: "payment", appId: "hitpay" });
const [HitPayPaymentAppCredentials] = integrations.data?.items || [];
const [credentialId] = HitPayPaymentAppCredentials?.userCredentialIds || [-1];
const showContent = !!integrations.data && integrations.isSuccess && !!credentialId;
const saveKeysMutation = trpc.viewer.apps.updateAppCredentials.useMutation({
onSuccess: () => {
showToast(t("keys_have_been_saved"), "success");
router.push("/event-types");
},
onError: (error) => {
showToast(error.message, "error");
},
});
const deleteMutation = trpc.viewer.credentials.delete.useMutation({
onSuccess: () => {
router.push("/apps/hitpay");
},
onError: () => {
showToast(t("error_removing_app"), "error");
},
});
const {
register,
handleSubmit,
formState: { errors },
watch,
reset,
} = useForm<z.infer<typeof settingsSchema>>({
reValidateMode: "onChange",
resolver: zodResolver(settingsSchema),
});
useEffect(() => {
const keyObj = isSandbox ? props.sandbox : props.prod;
const _keyData = {
apiKey: keyObj?.apiKey || "",
saltKey: keyObj?.saltKey || "",
};
reset(_keyData);
setKeyData(_keyData);
}, [isSandbox]);
useEffect(() => {
const subscription = watch((value) => {
const { apiKey, saltKey } = value;
if (apiKey && saltKey && (keyData?.apiKey !== apiKey || keyData?.saltKey !== saltKey)) {
setUpdatable(true);
} else {
setUpdatable(false);
}
});
return () => subscription.unsubscribe();
}, [watch, keyData]);
const onSubmit = handleSubmit(async (data) => {
if (loading) return;
setLoading(true);
try {
const keyParams = {
isSandbox,
prod: isSandbox ? props.prod : data,
sandbox: isSandbox ? data : props.sandbox,
};
saveKeysMutation.mutate({
credentialId,
key: hitpayCredentialKeysSchema.parse(keyParams),
});
} catch (error: unknown) {
let message = "";
if (error instanceof Error) {
message = error.message;
}
showToast(message, "error");
} finally {
setLoading(false);
}
});
const onCancel = () => {
deleteMutation.mutate({ id: credentialId });
};
const hitpayIcon = (
<>
<img className="h-5 w-5" src="/app-store/hitpay/icon.svg" alt="HitPay Icon" />
</>
);
if (session.status === "loading") return <></>;
if (integrations.isPending) {
return <div className="absolute z-50 flex h-screen w-full items-center bg-gray-200" />;
}
return (
<>
<div className="bg-default flex h-screen items-center justify-center">
{showContent ? (
<div className="flex w-full w-full max-w-[43em] flex-col items-center justify-center stack-y-4 p-4 lg:stack-y-5">
<div className="rounded bg-blue-50 p-3 text-sm text-blue-700 dark:bg-blue-950 dark:text-blue-300">
<Icon name="info" className="mb-0.5 inline-flex h-4 w-4" /> Create or connect to an existing
HitPay account to receive payments for your paid bookings.
</div>
<form className="w-full stack-y-4" onSubmit={onSubmit}>
<div className="bg-default border-subtle overflow-auto rounded border">
<div className="border-subtle flex items-center justify-between border-b p-4 md:p-5">
<h2 className="text-2xl font-semibold">Account Information</h2>
<div className="ml-auto flex items-center">
<Switch
onCheckedChange={(value) => SetIsSandbox(value as boolean)}
checked={isSandbox}
label="Sandbox"
/>
</div>
</div>
<div className="w-full stack-y-4 p-4 md:p-5">
<div className="w-full">
<KeyField
{...register("apiKey", {
required: true,
})}
id="apiKey"
name="apiKey"
containerClassName="w-full"
label={t("api_key")}
autoComplete="off"
autoCorrect="off"
defaultValue={keyData?.apiKey || ""}
/>
{errors.apiKey && (
<p data-testid="required" className="py-2 text-xs text-red-500">
{errors.apiKey?.message}
</p>
)}
</div>
<div className="w-full">
<KeyField
{...register("saltKey", {
required: true,
})}
id="saltKey"
name="saltKey"
containerClassName="w-full"
label="Salt"
autoComplete="off"
autoCorrect="off"
defaultValue={keyData?.saltKey || ""}
/>
{errors.saltKey && (
<p data-testid="required" className="py-2 text-xs text-red-500">
{errors.saltKey?.message}
</p>
)}
</div>
</div>
</div>
{!props.prod && !props.sandbox ? (
<div className="flex justify-end gap-4">
<Button color="secondary" className="h-10 text-base" onClick={onCancel}>
Cancel
</Button>
<button
className="font-body flex h-10 w-56 items-center justify-center gap-2 rounded-md font-bold text-black shadow transition-all hover:brightness-90 active:scale-95"
style={{
background: "linear-gradient(180deg, #FFDE6E 63.72%, #F8C455 95.24%)",
}}
type="submit"
disabled={loading}>
{hitpayIcon}
<span className="mr-2">Connect with HitPay</span>
</button>
</div>
) : (
<div className="flex justify-end gap-4">
<Link href="/apps/hitpay" className="inline-block">
<Button color="secondary">Go to App Store</Button>
</Link>
<Button color="primary" type="submit" disabled={!updatable}>
Update
</Button>
</div>
)}
</form>
</div>
) : (
<AppNotInstalledMessage appName="hitpay" />
)}
<Toaster position="bottom-right" />
</div>
</>
);
}