Skip to content

Commit

Permalink
fix: video upload component (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertfolch-redeemeum authored Oct 25, 2024
1 parent 093232e commit 2fe943c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 59 deletions.
21 changes: 11 additions & 10 deletions packages/react-kit/src/components/buttons/BaseButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import { Typography } from "../ui/Typography";
import { theme } from "../../theme";
import { Loading } from "../ui/loading/Loading";
import { ButtonSize } from "../ui/buttonSize";

import { AddDollarPrefixToKeys } from "../../types/helpers";
const colors = theme.colors.light;

const ButtonWithThemeProps = styled.button<{
size: ButtonSizeProp;
fill: boolean | undefined;
theme: BaseButtonTheme;
}>`
const ButtonWithThemeProps = styled.button<
AddDollarPrefixToKeys<{
size: ButtonSizeProp;
fill: boolean | undefined;
}> & { theme: BaseButtonTheme }
>`
${() => Styles.button};
${(props) => Styles[props.size as keyof typeof Styles]}
${(props) => Styles[props.$size as keyof typeof Styles]}
border-style: solid;
border-color: ${(props) => props.theme?.borderColor || "transparent"};
border-width: ${(props) => props.theme?.borderWidth || 0}px;
Expand All @@ -35,7 +36,7 @@ const ButtonWithThemeProps = styled.button<{
stroke: ${(props) => props.theme?.color || "#000000"};
}
${(props) =>
props.fill
props.$fill
? css`
width: 100%;
`
Expand Down Expand Up @@ -175,8 +176,8 @@ export const BaseButton = forwardRef<HTMLButtonElement, BaseButtonProps>(
<ButtonWithThemeProps
onClick={onClick}
type={type}
size={size}
fill={fill ? fill : undefined}
$size={size}
$fill={fill ? fill : undefined}
theme={theme}
{...rest}
ref={ref}
Expand Down
25 changes: 15 additions & 10 deletions packages/react-kit/src/components/form/Upload/BaseUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ function BaseUpload({
}
handleLoading(true);
try {
const imagePreview = await loadMedia(fileSrc || "");
if (imagePreview) {
setPreview(imagePreview);
const videoPreview = await loadMedia(fileSrc || "");
if (videoPreview) {
setPreview(videoPreview);
} else {
console.warn(
`imagePreview ${imagePreview} is falsy in loadIpfsImagePreview`
`videoPreview ${videoPreview} is falsy in loadIpfsImagePreview`
);
}
} catch (error) {
Expand Down Expand Up @@ -234,6 +234,7 @@ function BaseUpload({
borderRadius: borderRadius ? `${borderRadius}${borderRadiusUnit}` : "",
width: width ? `100%` : ""
};
const showPreview = field.value && field.value?.length !== 0 && preview;
return (
<>
{withEditor && showEditor && (
Expand Down Expand Up @@ -313,17 +314,21 @@ function BaseUpload({
<Loading size={2} />
) : (
<>
{field.value && field.value?.length !== 0 && preview ? (
{showPreview ? (
<>
{isVideoOnly ? (
<VideoPreview
src={
preview?.startsWith("http")
? preview
: "data:video/mp4;base64," +
preview?.substring(
"data:application/octet-stream;base64,".length
)
: preview?.startsWith(
"data:application/octet-stream;base64,"
)
? "data:video/mp4;base64," +
preview?.substring(
"data:application/octet-stream;base64,".length
)
: preview
}
autoPlay
muted
Expand All @@ -341,7 +346,7 @@ function BaseUpload({
) : (
<Image size={24} />
)}
{placeholder && (
{placeholder && !showPreview && (
<Typography tag="p" marginBottom={0} textAlign="center">
{placeholder}
</Typography>
Expand Down
2 changes: 1 addition & 1 deletion packages/react-kit/src/components/ui/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const Video: React.FC<VideoProps> = ({
return <ComponentWhileLoading />;
}
return (
<VideoWrapper {...rest}>
<VideoWrapper {...rest} className="video-container">
<VideoPlaceholder $position="static">
<Typography tag="div">
<Loading />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ const IconBlock = forwardRef<
borderRadius: CSSProperties["borderRadius"];
}
>(function IconBlock(props, ref) {
const { color, backgroundColor, borderRadius } = props;
if ("href" in props) {
const { color, backgroundColor, borderRadius, ...restProps } = props;
return (
<IconBlockLink
ref={ref as React.ForwardedRef<HTMLAnchorElement>}
$color={color}
$backgroundColor={backgroundColor}
$borderRadius={borderRadius}
{...props}
{...restProps}
/>
);
}

const { color, backgroundColor, borderRadius, ...restProps } = props;
return (
<IconBlockButton
// ignoring 'button' 'type' conflict between React and styled-components
Expand All @@ -134,7 +134,7 @@ const IconBlock = forwardRef<
$color={color}
$backgroundColor={backgroundColor}
$borderRadius={borderRadius}
{...props}
{...restProps}
/>
);
});
Expand Down
16 changes: 9 additions & 7 deletions packages/react-kit/src/stories/buttons/Upload.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { Meta } from "@storybook/react";
import { QueryClientProviderCustom, Upload } from "../..";
import { BaseUploadProps, QueryClientProviderCustom, Upload } from "../..";
import { EnvironmentProvider } from "../../components/environment/EnvironmentProvider";
import { IpfsProvider } from "../../components/ipfs/IpfsProvider";
import { Formik } from "formik";
Expand All @@ -20,7 +20,6 @@ export default {
},
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
tags: ["autodocs"],
// args: { onClick: fn() },
argTypes: {
name: {
table: {
Expand Down Expand Up @@ -69,20 +68,23 @@ export default {
} satisfies Meta<typeof Upload>;

const BASE_ARGS = {
name
} as const;
name,
placeholder: "",
withEditor: false,
saveButtonTheme: undefined
} as const satisfies BaseUploadProps;

// More on args: https://storybook.js.org/docs/react/writing-stories/args
export const Base = {
args: {
...BASE_ARGS
}
} satisfies BaseUploadProps
};

export const VideoOnly = {
args: {
...BASE_ARGS,
accept: "video/mp4",
withUpload: true
}
withUpload: false
} satisfies BaseUploadProps
};
30 changes: 3 additions & 27 deletions packages/react-kit/src/types/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@ type Falsy = false | 0 | "" | null | undefined;

export const isTruthy = <T>(x: T | Falsy): x is T => !!x;

//#region Omit doesnt work well when using it with a type that has [x:string]: any, in that case there is no autocomplete: https://github.com/microsoft/TypeScript/issues/31153
type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K;
} extends { [_ in keyof T]: infer U }
? {} extends U
? never
: U
: never; // I don't know why not just U work here, but ({} extends U ? never : U) work

type OmitFromKnownKeys<T, K extends keyof T> =
KnownKeys<T> extends infer U
? [U] extends [keyof T]
? Pick<T, Exclude<U, K>>
: never
: never;
export type ExtendedOmit<T, K extends keyof T> = OmitFromKnownKeys<T, K> &
(string extends K
? {}
: string extends keyof T
? { [n: string]: T[Exclude<keyof T, number>] }
: {}) & // support number property
(number extends K
? {}
: number extends keyof T
? { [n: number]: T[Exclude<keyof T, string>] }
: {}); // support number property
//#endregion
export type AddDollarPrefixToKeys<T> = {
[K in keyof T as `$${string & K}`]: T[K];
};

0 comments on commit 2fe943c

Please sign in to comment.