Fix flash of untranslated content by adding inline Skeleton Loaders on EventType (#4519)

* Add support for easy use of inline skeleton

* Fix user being taken to /v2 path

* Fixes

* Fix TS

Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
Hariom BalharaandPeer Richelsen authored and GitHub committed 2022-09-21 16:01:47 +00:00
1 parent 42c128b790
commit 631e974769
6 files changed
+112 -30

No files matched your search

+13 -10
View File
@@ -1,5 +1,5 @@
import { useId } from "@radix-ui/react-id";
import React, { forwardRef, ReactElement, ReactNode, Ref, useCallback, useMemo, useState } from "react";
import React, { forwardRef, ReactElement, ReactNode, Ref, useCallback, useState } from "react";
import { Check, Circle, Info, X, Eye, EyeOff } from "react-feather";
import {
FieldErrors,
@@ -13,7 +13,7 @@ import {
import classNames from "@calcom/lib/classNames";
import { getErrorFromUnknown } from "@calcom/lib/errors";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Tooltip } from "@calcom/ui/v2";
import { Skeleton, Tooltip } from "@calcom/ui/v2";
import showToast from "@calcom/ui/v2/core/notifications";
import { Alert } from "../../../Alert";
@@ -37,7 +37,7 @@ export function Label(props: JSX.IntrinsicElements["label"]) {
return (
<label
{...props}
className={classNames("block pb-2 text-sm font-medium leading-none text-gray-700", props.className)}>
className={classNames("mb-2 block text-sm font-medium leading-none text-gray-700", props.className)}>
{props.children}
</label>
);
@@ -166,15 +166,14 @@ type InputFieldProps = {
const InputField = forwardRef<HTMLInputElement, InputFieldProps>(function InputField(props, ref) {
const id = useId();
const { t: _t } = useLocale();
const { t: _t, isLocaleReady } = useLocale();
const t = props.t || _t;
const name = props.name || "";
const {
label = t(name),
labelProps,
labelClassName,
/** Prevents displaying untranslated placeholder keys */
placeholder = t(name + "_placeholder") !== name + "_placeholder" ? t(name + "_placeholder") : "",
placeholder = isLocaleReady ? t(name + "_placeholder") : "",
className,
addOnLeading,
addOnSuffix,
@@ -188,15 +187,19 @@ const InputField = forwardRef<HTMLInputElement, InputFieldProps>(function InputF
...passThrough
} = props;
const translatedPlaceholder = isLocaleReady ? placeholder : "";
return (
<div className={classNames(containerClassName)}>
{!!name && (
<Label
<Skeleton
as={Label}
htmlFor={id}
loadingClassName="w-16"
{...labelProps}
className={classNames(labelClassName, labelSrOnly && "sr-only", props.error && "text-red-900")}>
{label}
</Label>
</Skeleton>
)}
{addOnLeading || addOnSuffix ? (
<div
@@ -221,7 +224,7 @@ const InputField = forwardRef<HTMLInputElement, InputFieldProps>(function InputF
</div>
<Input
id={id}
placeholder={placeholder}
placeholder={translatedPlaceholder}
className={classNames(
className,
addOnLeading && "rounded-l-none",
@@ -233,7 +236,7 @@ const InputField = forwardRef<HTMLInputElement, InputFieldProps>(function InputF
/>
</div>
) : (
<Input id={id} placeholder={placeholder} className={className} {...passThrough} ref={ref} />
<Input id={id} placeholder={translatedPlaceholder} className={className} {...passThrough} ref={ref} />
)}
<HintsOrErrors hintErrors={hintErrors} fieldName={name} t={t} />
{hint && <div className="text-gray mt-2 flex items-center text-sm text-gray-700">{hint}</div>}
@@ -7,6 +7,8 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
import { SVGComponent } from "@calcom/types/SVGComponent";
import { Icon } from "@calcom/ui/Icon";
import { Skeleton } from "../../skeleton";
export type VerticalTabItemProps = {
name: string;
info?: string;
@@ -55,8 +57,12 @@ const VerticalTabItem = function ({
<props.icon className="mr-[10px] h-[16px] w-[16px] self-start stroke-[2px] md:mt-0" />
)}
<div>
<p>{t(name)}</p>
{info && <p className="pt-1 text-xs font-normal">{t(info)}</p>}
<Skeleton as="p">{t(name)}</Skeleton>
{info && (
<Skeleton as="p" className="mt-1 text-xs font-normal">
{t(info)}
</Skeleton>
)}
</div>
{!disableChevron && isCurrent && (
<div className="ml-auto self-center">
+52 -1
View File
@@ -1,4 +1,7 @@
import React from "react";
import classNames from "@calcom/lib/classNames";
import { useLocale } from "@calcom/lib/hooks/useLocale";
type SkeletonBaseProps = {
className?: string;
@@ -14,6 +17,54 @@ const SkeletonAvatar: React.FC<SkeletonBaseProps> = ({ className }) => {
return <div className={classNames(`mt-1 rounded-full bg-gray-200 ltr:mr-2 rtl:ml-2`, className)} />;
};
type SkeletonProps<T> = {
as: keyof JSX.IntrinsicElements | React.FC;
className?: string;
children: React.ReactNode;
loading?: boolean;
waitForTranslation?: boolean;
loadingClassName?: string;
} & (T extends React.FC<infer P>
? P
: T extends keyof JSX.IntrinsicElements
? JSX.IntrinsicElements[T]
: never);
const Skeleton = <T extends keyof JSX.IntrinsicElements | React.FC>({
as,
className = "",
children,
loading = false,
/**
* Assumes that the text needs translation by default and wait for it.
*/
waitForTranslation = true,
/**
* Classes that you need only in loading state
*/
loadingClassName = "",
...rest
}: SkeletonProps<T>) => {
const { isLocaleReady } = useLocale();
loading = (waitForTranslation ? !isLocaleReady : false) || loading;
const Component = as;
return (
<Component
className={classNames(
loading
? classNames(
"font-size-0 dark:white-300 animate-pulse rounded-md bg-gray-300 text-transparent",
loadingClassName
)
: "",
className
)}
{...rest}>
{children}
</Component>
);
};
const SkeletonText: React.FC<SkeletonBaseProps & { invisible?: boolean }> = ({
className = "",
invisible = false,
@@ -42,4 +93,4 @@ const SkeletonContainer: React.FC<SkeletonContainer> = ({ children, as, classNam
return <Component className={classNames("animate-pulse", className)}>{children}</Component>;
};
export { SkeletonAvatar, SkeletonText, SkeletonButton, SkeletonContainer };
export { Skeleton, SkeletonAvatar, SkeletonText, SkeletonButton, SkeletonContainer };