The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Hold button to confirm
Tim Wilsonc886c3528b96
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsxindex 0b4c622f62..69249af6fc 100644--- a/apps/web/ui/partners/payout-invoice-sheet.tsx+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx@@ -18,7 +18,9 @@ import {DynamicTooltipWrapper,Gear,GreekTemple,+ PaperPlane,Sheet,+ ShimmerDots,SimpleTooltipContent,Table,TooltipContent,@@ -36,7 +38,7 @@ import {} from "@dub/utils";import { useAction } from "next-safe-action/hooks";import { useRouter } from "next/navigation";-import { Fragment, useEffect, useMemo, useState } from "react";+import { Fragment, useEffect, useMemo, useRef, useState } from "react";import { toast } from "sonner";import Stripe from "stripe";import useSWR from "swr";@@ -123,6 +125,7 @@ function PayoutInvoiceSheetContent() {),[eligiblePayouts, excludedPayoutIds],);+const { executeAsync: confirmPayouts, isPending } = useAction(confirmPayoutsAction,{@@ -466,16 +469,13 @@ function PayoutInvoiceSheetContent() {</div><div className="flex items-center justify-end gap-2 border-t border-neutral-200 p-5">- <Button- type="button"- variant="primary"- loading={isPending}+ <ConfirmPayoutsButtononClick={async () => {if (!workspaceId || !selectedPaymentMethod) {- return;+ return false;}- await confirmPayouts({+ const result = await confirmPayouts({workspaceId,paymentMethodId: selectedPaymentMethod.id,cutoffPeriod,@@ -484,14 +484,27 @@ function PayoutInvoiceSheetContent() {fee: fee ?? 0,total: total ?? 0,});++ if (!result?.data?.invoiceId) return false;++ setTimeout(+ () =>+ result?.data?.invoiceId &&+ router.push(+ `/${slug}/program/payouts/success?invoiceId=${result.data.invoiceId}`,+ ),+ 1000,+ );++ return true;}}text={amount && amount > 0- ? `Confirm ${currencyFormatter(amount / 100, {+ ? `Hold to confirm ${currencyFormatter(amount / 100, {minimumFractionDigits: 2,maximumFractionDigits: 2,})} payout`- : "Confirm payout"+ : "Hold to confirm payout"}disabled={eligiblePayoutsLoading || !selectedPaymentMethod || amount === 0@@ -549,3 +562,158 @@ export function PayoutInvoiceSheet() {</Sheet>);}++function ConfirmPayoutsButton({+ onClick,+ text,+ disabled,+ disabledTooltip,+}: {+ onClick: () => Promise<boolean>;+ text: string;+ disabled: boolean;+ disabledTooltip: React.ReactNode;+}) {+ const loadingBar = useRef<HTMLDivElement>(null);++ const holding = useRef(false);+ const progress = useRef(0);++ const requestRef = useRef<number | null>();+ const previousTimeRef = useRef();++ // Rounded progress to nearest tenth+ const [roundedProgress, setRoundedProgress] = useState(0);++ const [isSuccess, setIsSuccess] = useState(false);++ const animate = (time) => {+ if (previousTimeRef.current !== undefined) {+ const deltaTime = time - previousTimeRef.current;++ if (progress.current < 1) {+ progress.current = Math.max(+ 0,+ Math.min(+ 1,+ progress.current + deltaTime * (holding.current ? 0.0005 : -0.001),+ ),+ );++ setRoundedProgress(Math.round(progress.current * 10) / 10);++ if (loadingBar.current)+ loadingBar.current.style.width = `${progress.current * 100}%`;+ }+ }++ previousTimeRef.current = time;+ requestRef.current = requestAnimationFrame(animate);+ };++ const submitting = useRef(false);++ // Submit when the progress is >= 1 and not already submitting+ useEffect(() => {+ if (roundedProgress < 1 || submitting.current) return;++ submitting.current = true;+ onClick()+ .then((result) => {+ if (result) {+ setIsSuccess(true);+ } else {+ progress.current = 0;+ setRoundedProgress(0);+ submitting.current = false;+ }+ })+ .catch(() => {+ progress.current = 0;+ setRoundedProgress(0);+ submitting.current = false;+ });+ }, [roundedProgress]);++ useEffect(() => {+ requestRef.current = requestAnimationFrame(animate);+ return () => cancelAnimationFrame(requestRef.current!);+ }, []);++ return (+ <Button+ type="button"+ variant="primary"+ className={cn(+ "relative overflow-hidden",+ isSuccess && "border-green-500 bg-green-500",+ )}+ textWrapperClassName="!overflow-visible"+ {...(!disabled &&+ !disabledTooltip && {+ onPointerDown: () => (holding.current = true),+ onPointerUp: () => (holding.current = false),+ onPointerLeave: () => (holding.current = false),+ onPointerCancel: () => (holding.current = false),+ })}+ text={+ <>+ <div+ ref={loadingBar}+ className={cn(+ "pointer-events-none absolute inset-y-0 left-0 overflow-hidden",+ !isSuccess && "bg-[linear-gradient(90deg,#fff1,#fff4)]",+ )}+ >+ <ShimmerDots+ className="inset-[unset] inset-y-0 left-0 w-[600px] opacity-30"+ color={[1, 1, 1]}+ />+ </div>+ <div className="relative text-center">+ <div+ className={cn(+ "transition-[transform,opacity] duration-300",+ roundedProgress >= 0.5 && "-translate-y-4 opacity-0",+ )}+ >+ {text}+ </div>+ <div+ className={cn(+ "pointer-events-none absolute inset-0 transition-[transform,opacity] duration-300",+ roundedProgress < 0.5 && "translate-y-4 opacity-0",+ roundedProgress >= 1 && "-translate-y-4 opacity-0",+ )}+ aria-hidden+ >+ Preparing payout...+ </div>+ <div+ className={cn(+ "pointer-events-none absolute inset-0 flex items-center justify-center transition-[transform,opacity] duration-300",+ roundedProgress < 1 && "-translate-x-1 translate-y-4 opacity-0",+ roundedProgress >= 1 && isSuccess && "translate-y-4 opacity-0",+ )}+ aria-hidden+ >+ <PaperPlane className="size-4" />+ </div>+ <div+ className={cn(+ "pointer-events-none absolute inset-0 flex items-center justify-center transition-[transform,opacity] duration-300",+ (roundedProgress < 1 || !isSuccess) &&+ "translate-y-4 opacity-0",+ )}+ aria-hidden+ >+ Payout sent+ </div>+ </div>+ </>+ }+ disabled={disabled}+ disabledTooltip={disabledTooltip}+ />+ );+}diff --git a/packages/ui/src/shimmer-dots.tsx b/packages/ui/src/shimmer-dots.tsxindex 37c3897ca3..066b19b538 100644--- a/packages/ui/src/shimmer-dots.tsx+++ b/packages/ui/src/shimmer-dots.tsx@@ -18,6 +18,7 @@ uniform float time;uniform float dotSize;uniform float cellSize;uniform float speed;+uniform vec3 color;// Gold noise: https://stackoverflow.com/a/28095165float PHI = 1.61803398874989484820459;@@ -38,7 +39,7 @@ void main(void) {float opacity = dot * fadeEffect;- vec4 fragColor = vec4(vec3(0.0), opacity);+ vec4 fragColor = vec4(color, opacity);fragColor.rgb *= fragColor.a;gl_FragColor = fragColor;@@ -52,11 +53,13 @@ export function ShimmerDots({dotSize = 1,cellSize = 3,speed = 5,+ color = [0, 0, 0],className,}: {dotSize?: number;cellSize?: number;speed?: number;+ color?: [number, number, number];className?: string;}) {const canvasRef = useRef<HTMLCanvasElement>(null);@@ -116,6 +119,7 @@ export function ShimmerDots({const dotSizeUniform = gl.getUniformLocation(shaderProgram, "dotSize");const cellSizeUniform = gl.getUniformLocation(shaderProgram, "cellSize");const speedUniform = gl.getUniformLocation(shaderProgram, "speed");+ const colorUniform = gl.getUniformLocation(shaderProgram, "color");gl.useProgram(shaderProgram);@@ -130,6 +134,7 @@ export function ShimmerDots({gl.uniform1f(dotSizeUniform, dotSize * window.devicePixelRatio);gl.uniform1f(cellSizeUniform, cellSize * window.devicePixelRatio);gl.uniform1f(speedUniform, speed);+ gl.uniform3f(colorUniform, color[0], color[1], color[2]);let animationFrameId: number;let lastTimestamp = 0;