Skip to content

[Experiment] Remove clientId from serverThirdwebClient #7203

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions apps/dashboard/src/@/components/ui/decimal-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Input } from "./input";

export function DecimalInput(props: {
value: string;
onChange: (value: string) => void;
maxValue?: number;
id?: string;
className?: string;
}) {
return (
<Input
id={props.id}
type="text"
value={props.value}
className={props.className}
inputMode="decimal"
onChange={(e) => {
const number = Number(e.target.value);
// ignore if string becomes invalid number
if (Number.isNaN(number)) {
return;
}

if (props.maxValue && number > props.maxValue) {
return;
}

// replace leading multiple zeros with single zero
let cleanedValue = e.target.value.replace(/^0+/, "0");

// replace leading zero before decimal point
if (!cleanedValue.includes(".")) {
cleanedValue = cleanedValue.replace(/^0+/, "");
}

props.onChange(cleanedValue || "0");
}}
/>
);
}
3 changes: 3 additions & 0 deletions apps/dashboard/src/@/constants/thirdweb-client.client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NEXT_PUBLIC_DASHBOARD_CLIENT_ID } from "./public-envs";
import { getConfiguredThirdwebClient } from "./thirdweb.server";

export function getClientThirdwebClient(params?: {
Expand All @@ -7,5 +8,7 @@ export function getClientThirdwebClient(params?: {
return getConfiguredThirdwebClient({
secretKey: params?.jwt ?? undefined,
teamId: params?.teamId ?? undefined,
type: "client",
clientId: NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
});
}
3 changes: 3 additions & 0 deletions apps/dashboard/src/@/constants/thirdweb-client.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import "server-only";

import { NEXT_PUBLIC_DASHBOARD_CLIENT_ID } from "./public-envs";
import { DASHBOARD_THIRDWEB_SECRET_KEY } from "./server-envs";
import { getConfiguredThirdwebClient } from "./thirdweb.server";

export const serverThirdwebClient = getConfiguredThirdwebClient({
teamId: undefined,
secretKey: DASHBOARD_THIRDWEB_SECRET_KEY,
clientId: NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
type: "server",
});
58 changes: 40 additions & 18 deletions apps/dashboard/src/@/constants/thirdweb.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
NEXT_PUBLIC_IPFS_GATEWAY_URL,
} from "@/constants/public-envs";
import { NEXT_PUBLIC_IPFS_GATEWAY_URL } from "@/constants/public-envs";
import {
THIRDWEB_BRIDGE_URL,
THIRDWEB_BUNDLER_DOMAIN,
Expand All @@ -22,10 +19,21 @@ import {
import { getZkPaymasterData } from "thirdweb/wallets/smart";
import { getVercelEnv } from "../../lib/vercel-utils";

export function getConfiguredThirdwebClient(options: {
secretKey: string | undefined;
teamId: string | undefined;
}): ThirdwebClient {
export function getConfiguredThirdwebClient(
options:
| {
type: "server";
secretKey: string;
clientId: string | undefined;
teamId: string | undefined;
}
| {
type: "client";
clientId: string;
secretKey: string | undefined;
teamId: string | undefined;
},
): ThirdwebClient {
if (getVercelEnv() !== "production") {
// if not on production: run this when creating a client to set the domains
setThirdwebDomains({
Expand Down Expand Up @@ -73,16 +81,30 @@ export function getConfiguredThirdwebClient(options: {
});
}

return createThirdwebClient({
teamId: options.teamId,
secretKey: options.secretKey,
clientId: NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
config: {
storage: {
gatewayUrl: NEXT_PUBLIC_IPFS_GATEWAY_URL,
},
},
});
return createThirdwebClient(
// this ternary is purely for making typescript happy - both are same object
options.type === "server"
? {
teamId: options.teamId,
secretKey: options.secretKey,
clientId: options.clientId,
config: {
storage: {
gatewayUrl: NEXT_PUBLIC_IPFS_GATEWAY_URL,
},
},
}
: {
teamId: options.teamId,
secretKey: options.secretKey,
clientId: options.clientId,
config: {
storage: {
gatewayUrl: NEXT_PUBLIC_IPFS_GATEWAY_URL,
},
},
},
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ChevronRightIcon,
ExternalLinkIcon,
} from "lucide-react";
import { useState } from "react";
import { useEffect, useState } from "react";
import { type ThirdwebContract, toTokens } from "thirdweb";
import type { ChainMetadata } from "thirdweb/chains";
import {
Expand Down Expand Up @@ -199,6 +199,7 @@ export function RecentTransfers(props: {
}) {
const rowsPerPage = 10;
const [page, setPage] = useState(0);
const [hasFetchedOnce, setHasFetchedOnce] = useState(false);

const tokenQuery = useTokenTransfers({
chainId: props.clientContract.chain.id,
Expand All @@ -207,11 +208,18 @@ export function RecentTransfers(props: {
limit: rowsPerPage,
});

// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
if (!tokenQuery.isPending) {
setHasFetchedOnce(true);
}
}, [tokenQuery.isPending]);

return (
<div>
<RecentTransfersUI
data={tokenQuery.data ?? []}
isPending={tokenQuery.isPending}
isPending={tokenQuery.isPending && !hasFetchedOnce}
rowsPerPage={rowsPerPage}
tokenMetadata={{
decimals: props.decimals,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
"use client";
import { Spinner } from "@/components/ui/Spinner/Spinner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { SkeletonContainer } from "@/components/ui/skeleton";
import { cn } from "@/lib/utils";
import { useMutation, useQuery } from "@tanstack/react-query";
import { TransactionButton } from "components/buttons/TransactionButton";
import {
CheckIcon,
CircleIcon,
MinusIcon,
PlusIcon,
XIcon,
} from "lucide-react";
import { CheckIcon, CircleIcon, XIcon } from "lucide-react";
import { useTheme } from "next-themes";
import { useState } from "react";
import { toast } from "sonner";
Expand All @@ -33,6 +25,7 @@ import {
import { useActiveAccount, useSendTransaction } from "thirdweb/react";
import { getClaimParams } from "thirdweb/utils";
import { tryCatch } from "utils/try-catch";
import { DecimalInput } from "../../../../../../../../../../@/components/ui/decimal-input";
import { getSDKTheme } from "../../../../../../../../components/sdk-component-theme";
import { PublicPageConnectButton } from "../../../_components/PublicPageConnectButton";
import { getCurrencyMeta } from "../../_utils/getCurrencyMeta";
Expand All @@ -53,7 +46,7 @@ export function ClaimTokenCardUI(props: {
symbol: string;
};
}) {
const [quantity, setQuantity] = useState(1);
const [quantity, setQuantity] = useState("1");
const account = useActiveAccount();
const { theme } = useTheme();
const sendClaimTx = useSendTransaction({
Expand Down Expand Up @@ -216,6 +209,7 @@ export function ClaimTokenCardUI(props: {
quantity={quantity}
setQuantity={setQuantity}
id="token-amount"
symbol={props.symbol}
/>
{/* <p className="text-xs text-muted-foreground">Maximum purchasable: {tokenData.maxPurchasable} tokens</p> */}
</div>
Expand Down Expand Up @@ -246,9 +240,7 @@ export function ClaimTokenCardUI(props: {
{/* Quantity */}
<div className="flex justify-between font-medium text-sm">
<span>Quantity</span>
<span>
{quantity} Token{quantity > 1 ? "s" : ""}
</span>
<span>{quantity}</span>
</div>

{/* Total Price */}
Expand All @@ -265,7 +257,7 @@ export function ClaimTokenCardUI(props: {
claimParamsData.pricePerTokenWei,
claimParamsData.decimals,
),
) * quantity
) * Number(quantity)
} ${claimParamsData.symbol}`
: undefined
}
Expand Down Expand Up @@ -293,7 +285,7 @@ export function ClaimTokenCardUI(props: {
txChainID={props.contract.chain.id}
disabled={approveAndClaim.isPending || !claimParamsData}
>
Buy Token{quantity > 1 ? "s" : ""}
Buy
</TransactionButton>
) : (
<PublicPageConnectButton connectButtonClassName="!w-full" />
Expand Down Expand Up @@ -350,39 +342,25 @@ function StepUI(props: {
}

function PriceInput(props: {
quantity: number;
setQuantity: (quantity: number) => void;
quantity: string;
setQuantity: (quantity: string) => void;
id: string;
symbol: string;
}) {
return (
<div className="flex items-center">
<Input
<div className="relative">
<DecimalInput
id={props.id}
type="number"
value={props.quantity}
onChange={(e) =>
props.setQuantity(Math.max(1, Number(e.target.value) || 1))
}
min="1"
className="!text-xl h-12 rounded-r-none border-r-0 font-semibold "
value={String(props.quantity)}
onChange={(value) => {
console.log("value", value);
props.setQuantity(value);
}}
className="!text-2xl h-auto truncate bg-muted/50 pr-14 font-bold"
/>

<Button
variant="outline"
className="h-12 w-16 rounded-none border-r-0 bg-background p-0 disabled:opacity-100"
onClick={() => props.setQuantity(props.quantity - 1)}
disabled={props.quantity <= 1}
>
<MinusIcon className="size-4" />
</Button>

<Button
className="h-12 w-16 rounded-l-none bg-background p-0 disabled:opacity-100"
variant="outline"
onClick={() => props.setQuantity(props.quantity + 1)}
>
<PlusIcon className="size-4" />
</Button>
<div className="-translate-y-1/2 absolute top-1/2 right-4 text-base text-muted-foreground">
{props.symbol}
</div>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { FormFieldSetup } from "@/components/blocks/FormFieldSetup";
import { TokenSelector } from "@/components/blocks/TokenSelector";
import { DynamicHeight } from "@/components/ui/DynamicHeight";
import { Input } from "@/components/ui/input";
import { DecimalInput } from "@/components/ui/decimal-input";
import { Switch } from "@/components/ui/switch";
import type { ThirdwebClient } from "thirdweb";
import type { TokenDistributionForm } from "../form";
Expand Down Expand Up @@ -112,38 +112,3 @@ export function TokenSaleSection(props: {
</DynamicHeight>
);
}

function DecimalInput(props: {
value: string;
onChange: (value: string) => void;
maxValue?: number;
}) {
return (
<Input
type="text"
value={props.value}
inputMode="decimal"
onChange={(e) => {
const number = Number(e.target.value);
// ignore if string becomes invalid number
if (Number.isNaN(number)) {
return;
}

if (props.maxValue && number > props.maxValue) {
return;
}

// replace leading multiple zeros with single zero
let cleanedValue = e.target.value.replace(/^0+/, "0");

// replace leading zero before decimal point
if (!cleanedValue.includes(".")) {
cleanedValue = cleanedValue.replace(/^0+/, "");
}

props.onChange(cleanedValue || "0");
}}
/>
);
}
Loading