From 369457fcdb8dea713ea788aeb53593318395f3cd Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:14:28 +0900 Subject: [PATCH 1/9] Separate posthog provider component --- .../keychain/src/components/ErrorBoundary.tsx | 2 +- .../connect/create/CreateController.tsx | 2 +- packages/keychain/src/components/home.tsx | 2 +- .../src/components/provider/index.tsx | 2 +- .../src/components/provider/posthog.tsx | 16 ++++++++++ packages/keychain/src/context/posthog.tsx | 30 ++++--------------- packages/keychain/src/hooks/connection.ts | 2 +- packages/keychain/src/hooks/posthog.tsx | 10 +++++++ 8 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 packages/keychain/src/components/provider/posthog.tsx create mode 100644 packages/keychain/src/hooks/posthog.tsx diff --git a/packages/keychain/src/components/ErrorBoundary.tsx b/packages/keychain/src/components/ErrorBoundary.tsx index fe6bf3072..6a5211f0d 100644 --- a/packages/keychain/src/components/ErrorBoundary.tsx +++ b/packages/keychain/src/components/ErrorBoundary.tsx @@ -4,7 +4,7 @@ import { AlertIcon, ExternalIcon, Button } from "@cartridge/ui-next"; import { useConnection } from "@/hooks/connection"; import { CARTRIDGE_DISCORD_LINK } from "@/const"; import { Link } from "react-router-dom"; -import { usePostHog } from "@/context/posthog"; +import { usePostHog } from "@/hooks/posthog"; import { useEffect } from "react"; export class ErrorBoundary extends React.Component< diff --git a/packages/keychain/src/components/connect/create/CreateController.tsx b/packages/keychain/src/components/connect/create/CreateController.tsx index aa7d2e6fc..f0cc0c4df 100644 --- a/packages/keychain/src/components/connect/create/CreateController.tsx +++ b/packages/keychain/src/components/connect/create/CreateController.tsx @@ -10,7 +10,7 @@ import { useCreateController } from "./useCreateController"; import { ErrorAlert } from "@/components/ErrorAlert"; import { VerifiableControllerTheme } from "@/context/theme"; import InAppSpy from "inapp-spy"; -import { usePostHog } from "@/context/posthog"; +import { usePostHog } from "@/hooks/posthog"; interface CreateControllerViewProps { theme: VerifiableControllerTheme; diff --git a/packages/keychain/src/components/home.tsx b/packages/keychain/src/components/home.tsx index dd6ec8e27..2961ec683 100644 --- a/packages/keychain/src/components/home.tsx +++ b/packages/keychain/src/components/home.tsx @@ -13,7 +13,7 @@ import { Settings } from "./settings"; import { SignMessage } from "./SignMessage"; import { PageLoading } from "./Loading"; import { execute } from "@/utils/connection/execute"; -import { usePostHog } from "@/context/posthog"; +import { usePostHog } from "@/hooks/posthog"; export function Home() { const { context, setContext, controller, error, policies, upgrade } = diff --git a/packages/keychain/src/components/provider/index.tsx b/packages/keychain/src/components/provider/index.tsx index 915c1d94f..14e28cde9 100644 --- a/packages/keychain/src/components/provider/index.tsx +++ b/packages/keychain/src/components/provider/index.tsx @@ -4,12 +4,12 @@ import { useConnectionValue } from "@/hooks/connection"; import { CartridgeAPIProvider } from "@cartridge/utils/api/cartridge"; import { ENDPOINT } from "@/utils/graphql"; import { ConnectionContext } from "./connection"; +import { PostHogProvider } from "./posthog"; import { ControllerThemeProvider } from "./theme"; import { jsonRpcProvider, StarknetConfig, voyager } from "@starknet-react/core"; import { sepolia, mainnet } from "@starknet-react/chains"; import { constants, num } from "starknet"; import { BrowserRouter } from "react-router-dom"; -import { PostHogProvider } from "@/context/posthog"; export function Provider({ children }: PropsWithChildren) { const connection = useConnectionValue(); diff --git a/packages/keychain/src/components/provider/posthog.tsx b/packages/keychain/src/components/provider/posthog.tsx new file mode 100644 index 000000000..22dec6c75 --- /dev/null +++ b/packages/keychain/src/components/provider/posthog.tsx @@ -0,0 +1,16 @@ +import { PropsWithChildren } from "react"; +import { PostHogContext, PostHogWrapper } from "@/context/posthog"; + +export function PostHogProvider({ children }: PropsWithChildren) { + const posthog = new PostHogWrapper(import.meta.env.VITE_POSTHOG_KEY!, { + host: import.meta.env.VITE_POSTHOG_HOST, + persistence: "memory", + autocapture: false, + }); + + return ( + + {children} + + ); +} diff --git a/packages/keychain/src/context/posthog.tsx b/packages/keychain/src/context/posthog.tsx index 9f73e20be..bc590bc87 100644 --- a/packages/keychain/src/context/posthog.tsx +++ b/packages/keychain/src/context/posthog.tsx @@ -1,10 +1,10 @@ -import { createContext, useContext, PropsWithChildren } from "react"; +import { createContext } from "react"; import PostHog from "posthog-js-lite"; // eslint-disable-next-line @typescript-eslint/no-explicit-any type Properties = Record; -class PostHogWrapper extends PostHog { +export class PostHogWrapper extends PostHog { isLocal = typeof window !== "undefined" && window.location.hostname.includes("localhost"); @@ -87,26 +87,6 @@ interface PostHogContextType { posthog: PostHogWrapper; } -const PostHogContext = createContext(undefined); - -export function PostHogProvider({ children }: PropsWithChildren) { - const posthog = new PostHogWrapper(import.meta.env.VITE_POSTHOG_KEY!, { - host: import.meta.env.VITE_POSTHOG_HOST, - persistence: "memory", - autocapture: false, - }); - - return ( - - {children} - - ); -} - -export function usePostHog() { - const context = useContext(PostHogContext); - if (!context) { - return undefined; - } - return context.posthog; -} +export const PostHogContext = createContext( + undefined, +); diff --git a/packages/keychain/src/hooks/connection.ts b/packages/keychain/src/hooks/connection.ts index 7c7151c09..8a59fc00b 100644 --- a/packages/keychain/src/hooks/connection.ts +++ b/packages/keychain/src/hooks/connection.ts @@ -18,7 +18,7 @@ import { ConnectionContextValue, } from "@/components/provider/connection"; import { UpgradeInterface, useUpgrade } from "./upgrade"; -import { usePostHog } from "@/context/posthog"; +import { usePostHog } from "@/hooks/posthog"; import { Policies } from "@cartridge/presets"; import { defaultTheme, diff --git a/packages/keychain/src/hooks/posthog.tsx b/packages/keychain/src/hooks/posthog.tsx new file mode 100644 index 000000000..9b42db60f --- /dev/null +++ b/packages/keychain/src/hooks/posthog.tsx @@ -0,0 +1,10 @@ +import { useContext } from "react"; +import { PostHogContext } from "@/context/posthog"; + +export function usePostHog() { + const context = useContext(PostHogContext); + if (!context) { + return undefined; + } + return context.posthog; +} From d3d2262e9ff5444534fe61ce160cd675616f1858 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:20:19 +0900 Subject: [PATCH 2/9] Separate arcade provider --- .../profile/src/components/context/arcade.tsx | 18 +----------------- .../profile/src/components/context/index.tsx | 1 - packages/profile/src/context/arcade.ts | 18 ++++++++++++++++++ packages/profile/src/context/index.ts | 0 packages/profile/src/hooks/arcade.ts | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) create mode 100644 packages/profile/src/context/arcade.ts create mode 100644 packages/profile/src/context/index.ts diff --git a/packages/profile/src/components/context/arcade.tsx b/packages/profile/src/components/context/arcade.tsx index 4950f5172..c5e83918d 100644 --- a/packages/profile/src/components/context/arcade.tsx +++ b/packages/profile/src/components/context/arcade.tsx @@ -1,5 +1,4 @@ import { - createContext, ReactNode, useCallback, useContext, @@ -19,25 +18,10 @@ import { RegistryOptions, } from "@bal7hazar/arcade-sdk"; import { constants } from "starknet"; +import { ArcadeContext } from "@/context/arcade"; const CHAIN_ID = constants.StarknetChainId.SN_SEPOLIA; -/** - * Interface defining the shape of the Arcade context. - */ -interface ArcadeContextType { - /** The Arcade client instance */ - chainId: string; - provider: ExternalProvider; - pins: { [playerId: string]: string[] }; - games: { [gameId: string]: GameModel }; -} - -/** - * React context for sharing Arcade-related data throughout the application. - */ -export const ArcadeContext = createContext(null); - /** * Provider component that makes Arcade context available to child components. * diff --git a/packages/profile/src/components/context/index.tsx b/packages/profile/src/components/context/index.tsx index 7bb7874b7..0ff06f971 100644 --- a/packages/profile/src/components/context/index.tsx +++ b/packages/profile/src/components/context/index.tsx @@ -1,4 +1,3 @@ -export { ArcadeContext } from "./arcade"; export { ConnectionContext } from "./connection"; export { DataContext } from "./data"; export { ThemeContext } from "./theme"; diff --git a/packages/profile/src/context/arcade.ts b/packages/profile/src/context/arcade.ts new file mode 100644 index 000000000..8b429b717 --- /dev/null +++ b/packages/profile/src/context/arcade.ts @@ -0,0 +1,18 @@ +import { ArcadeProvider, GameModel } from "@bal7hazar/arcade-sdk"; +import { createContext } from "react"; + +/** + * Interface defining the shape of the Arcade context. + */ +interface ArcadeContextType { + /** The Arcade client instance */ + chainId: string; + provider: ArcadeProvider; + pins: { [playerId: string]: string[] }; + games: { [gameId: string]: GameModel }; +} + +/** + * React context for sharing Arcade-related data throughout the application. + */ +export const ArcadeContext = createContext(null); diff --git a/packages/profile/src/context/index.ts b/packages/profile/src/context/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/packages/profile/src/hooks/arcade.ts b/packages/profile/src/hooks/arcade.ts index c2dbf0010..79cf5494c 100644 --- a/packages/profile/src/hooks/arcade.ts +++ b/packages/profile/src/hooks/arcade.ts @@ -1,5 +1,5 @@ import { useContext } from "react"; -import { ArcadeContext } from "../components/context/arcade"; +import { ArcadeContext } from "@/context/arcade"; /** * Custom hook to access the Arcade context and account information. From 0943016001a62534a8213a8a863dbc1ef8d52955 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:24:06 +0900 Subject: [PATCH 3/9] Separate connection provider --- .../src/components/context/connection.tsx | 58 ++++--------------- .../profile/src/components/context/index.tsx | 1 - packages/profile/src/context/connection.ts | 41 +++++++++++++ packages/profile/src/hooks/context.ts | 7 +-- 4 files changed, 53 insertions(+), 54 deletions(-) create mode 100644 packages/profile/src/context/connection.ts diff --git a/packages/profile/src/components/context/connection.tsx b/packages/profile/src/components/context/connection.tsx index 99eb0627f..020f7f995 100644 --- a/packages/profile/src/components/context/connection.tsx +++ b/packages/profile/src/components/context/connection.tsx @@ -1,6 +1,5 @@ import { connectToParent } from "@cartridge/penpal"; import { - createContext, useState, ReactNode, useEffect, @@ -8,51 +7,14 @@ import { } from "react"; import { ETH_CONTRACT_ADDRESS, - isIframe, normalize, STRK_CONTRACT_ADDRESS, } from "@cartridge/utils"; -import { Call, constants, getChecksumAddress, RpcProvider } from "starknet"; +import { constants, getChecksumAddress, RpcProvider } from "starknet"; import { useNavigate, useSearchParams } from "react-router-dom"; +import { ConnectionContext, initialState, ParentMethods } from "@/context/connection"; -type ConnectionContextType = { - origin: string; - parent: ParentMethods; - provider: RpcProvider; - chainId: string; - erc20: string[]; - project?: string; - namespace?: string; - version?: string; - isVisible: boolean; - setIsVisible: (isVisible: boolean) => void; -}; - -type ParentMethods = { - close: () => void; - openSettings: () => void; - openPurchaseCredits: () => void; - openExecute: (calls: Call[], chain?: string) => Promise; -}; - -const initialState: ConnectionContextType = { - origin: location.origin, - parent: { - close: () => {}, - openSettings: () => {}, - openPurchaseCredits: () => {}, - openExecute: async () => false, - }, - provider: new RpcProvider({ nodeUrl: import.meta.env.VITE_RPC_SEPOLIA }), - chainId: "", - erc20: [], - isVisible: !isIframe(), - setIsVisible: () => {}, -}; - -export const ConnectionContext = - createContext(initialState); - +import { ConnectionContextType } from "@/context/connection"; export function ConnectionProvider({ children }: { children: ReactNode }) { const [state, setState] = useState(initialState); @@ -90,13 +52,13 @@ export function ConnectionProvider({ children }: { children: ReactNode }) { STRK_CONTRACT_ADDRESS, ...(erc20Param ? decodeURIComponent(erc20Param) - .split(",") - .filter( - (address) => - ![ETH_CONTRACT_ADDRESS, STRK_CONTRACT_ADDRESS].includes( - getChecksumAddress(address), - ), - ) + .split(",") + .filter( + (address) => + ![ETH_CONTRACT_ADDRESS, STRK_CONTRACT_ADDRESS].includes( + getChecksumAddress(address), + ), + ) : []), ]; } diff --git a/packages/profile/src/components/context/index.tsx b/packages/profile/src/components/context/index.tsx index 0ff06f971..fea59667a 100644 --- a/packages/profile/src/components/context/index.tsx +++ b/packages/profile/src/components/context/index.tsx @@ -1,4 +1,3 @@ -export { ConnectionContext } from "./connection"; export { DataContext } from "./data"; export { ThemeContext } from "./theme"; export { Provider } from "./provider"; diff --git a/packages/profile/src/context/connection.ts b/packages/profile/src/context/connection.ts new file mode 100644 index 000000000..b8dcaa5a3 --- /dev/null +++ b/packages/profile/src/context/connection.ts @@ -0,0 +1,41 @@ +import { isIframe } from "@cartridge/utils"; +import { createContext } from "react"; +import { Call, RpcProvider } from "starknet"; + +export type ConnectionContextType = { + origin: string; + parent: ParentMethods; + provider: RpcProvider; + chainId: string; + erc20: string[]; + project?: string; + namespace?: string; + version?: string; + isVisible: boolean; + setIsVisible: (isVisible: boolean) => void; +}; + +export type ParentMethods = { + close: () => void; + openSettings: () => void; + openPurchaseCredits: () => void; + openExecute: (calls: Call[], chain?: string) => Promise; +}; + +export const initialState: ConnectionContextType = { + origin: location.origin, + parent: { + close: () => {}, + openSettings: () => {}, + openPurchaseCredits: () => {}, + openExecute: async () => false, + }, + provider: new RpcProvider({ nodeUrl: import.meta.env.VITE_RPC_SEPOLIA }), + chainId: "", + erc20: [], + isVisible: !isIframe(), + setIsVisible: () => {}, +}; + +export const ConnectionContext = + createContext(initialState); diff --git a/packages/profile/src/hooks/context.ts b/packages/profile/src/hooks/context.ts index ea9576d20..03b110290 100644 --- a/packages/profile/src/hooks/context.ts +++ b/packages/profile/src/hooks/context.ts @@ -1,9 +1,6 @@ import { useContext } from "react"; -import { - ThemeContext, - ConnectionContext, - DataContext, -} from "@/components/context"; +import { ThemeContext, DataContext } from "@/components/context"; +import { ConnectionContext } from "@/context/connection"; export function useTheme() { return useContext(ThemeContext); From 8ec00a341ca0d2021ebe2fc11bf9483383f61cd7 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:26:10 +0900 Subject: [PATCH 4/9] Separate data provider --- .../profile/src/components/context/data.tsx | 18 ++---------------- .../profile/src/components/context/index.tsx | 1 - packages/profile/src/context/data.ts | 18 ++++++++++++++++++ packages/profile/src/hooks/context.ts | 3 ++- 4 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 packages/profile/src/context/data.ts diff --git a/packages/profile/src/components/context/data.tsx b/packages/profile/src/components/context/data.tsx index f4deaf0cf..61b6edc45 100644 --- a/packages/profile/src/components/context/data.tsx +++ b/packages/profile/src/components/context/data.tsx @@ -1,21 +1,7 @@ -import { createContext, useState, ReactNode } from "react"; +import { useState, ReactNode } from "react"; import { useAchievements } from "@/hooks/achievements"; +import { DataContext } from "@/context/data"; -type DataContextType = { - trophies: ReturnType; - setAccountAddress: (address: string | undefined) => void; -}; - -const initialState: DataContextType = { - trophies: { - achievements: [], - players: [], - isLoading: false, - }, - setAccountAddress: () => {}, -}; - -export const DataContext = createContext(initialState); export function DataProvider({ children }: { children: ReactNode }) { const [accountAddress, setAccountAddress] = useState( diff --git a/packages/profile/src/components/context/index.tsx b/packages/profile/src/components/context/index.tsx index fea59667a..77cf0fc98 100644 --- a/packages/profile/src/components/context/index.tsx +++ b/packages/profile/src/components/context/index.tsx @@ -1,3 +1,2 @@ -export { DataContext } from "./data"; export { ThemeContext } from "./theme"; export { Provider } from "./provider"; diff --git a/packages/profile/src/context/data.ts b/packages/profile/src/context/data.ts new file mode 100644 index 000000000..0bde35c8a --- /dev/null +++ b/packages/profile/src/context/data.ts @@ -0,0 +1,18 @@ +import { useAchievements } from "@/hooks/achievements"; +import { createContext } from "react"; + +export type DataContextType = { + trophies: ReturnType; + setAccountAddress: (address: string | undefined) => void; +}; + +const initialState: DataContextType = { + trophies: { + achievements: [], + players: [], + isLoading: false, + }, + setAccountAddress: () => {}, +}; + +export const DataContext = createContext(initialState); diff --git a/packages/profile/src/hooks/context.ts b/packages/profile/src/hooks/context.ts index 03b110290..325103fe3 100644 --- a/packages/profile/src/hooks/context.ts +++ b/packages/profile/src/hooks/context.ts @@ -1,6 +1,7 @@ import { useContext } from "react"; -import { ThemeContext, DataContext } from "@/components/context"; +import { ThemeContext } from "@/components/context"; import { ConnectionContext } from "@/context/connection"; +import { DataContext } from "@/context/data"; export function useTheme() { return useContext(ThemeContext); From dce3c118c6c2d08e25f6a6b735ca009b1b0750ef Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:28:39 +0900 Subject: [PATCH 5/9] Separate theme provider --- .../profile/src/components/context/index.tsx | 1 - .../profile/src/components/context/theme.tsx | 22 +++---------------- packages/profile/src/context/theme.ts | 19 ++++++++++++++++ packages/profile/src/hooks/context.ts | 2 +- 4 files changed, 23 insertions(+), 21 deletions(-) create mode 100644 packages/profile/src/context/theme.ts diff --git a/packages/profile/src/components/context/index.tsx b/packages/profile/src/components/context/index.tsx index 77cf0fc98..9c0bfb304 100644 --- a/packages/profile/src/components/context/index.tsx +++ b/packages/profile/src/components/context/index.tsx @@ -1,2 +1 @@ -export { ThemeContext } from "./theme"; export { Provider } from "./provider"; diff --git a/packages/profile/src/components/context/theme.tsx b/packages/profile/src/components/context/theme.tsx index f2d7c76e9..f9045747b 100644 --- a/packages/profile/src/components/context/theme.tsx +++ b/packages/profile/src/components/context/theme.tsx @@ -4,33 +4,17 @@ import { ControllerTheme, } from "@cartridge/presets"; import { useThemeEffect } from "@cartridge/ui-next"; -import { createContext, useCallback, useEffect, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import { useSearchParams } from "react-router-dom"; import { useConnection } from "@/hooks/context"; +import { ColorScheme, ThemeContext, initialState } from "@/context/theme"; -type ColorScheme = "dark" | "light" | "system"; - -type ThemeProviderProps = { +export type ThemeProviderProps = { children: React.ReactNode; defaultScheme?: ColorScheme; storageKey?: string; }; -type ThemeProviderContextType = { - colorScheme: ColorScheme; - setColorScheme: (colorMode: ColorScheme) => void; - theme: ControllerTheme; -}; - -const initialState: ThemeProviderContextType = { - colorScheme: "system", - setColorScheme: () => null, - theme: defaultTheme, -}; - -export const ThemeContext = - createContext(initialState); - export function ThemeProvider({ children, defaultScheme = "system", diff --git a/packages/profile/src/context/theme.ts b/packages/profile/src/context/theme.ts new file mode 100644 index 000000000..88f6f851c --- /dev/null +++ b/packages/profile/src/context/theme.ts @@ -0,0 +1,19 @@ +import { createContext } from "react"; +import { defaultTheme, ControllerTheme } from "@cartridge/presets"; + +export type ColorScheme = "dark" | "light" | "system"; + +type ThemeProviderContextType = { + colorScheme: ColorScheme; + setColorScheme: (colorMode: ColorScheme) => void; + theme: ControllerTheme; +}; + +export const initialState: ThemeProviderContextType = { + colorScheme: "system", + setColorScheme: () => null, + theme: defaultTheme, +}; + +export const ThemeContext = + createContext(initialState); diff --git a/packages/profile/src/hooks/context.ts b/packages/profile/src/hooks/context.ts index 325103fe3..5a2d40bc1 100644 --- a/packages/profile/src/hooks/context.ts +++ b/packages/profile/src/hooks/context.ts @@ -1,5 +1,5 @@ import { useContext } from "react"; -import { ThemeContext } from "@/components/context"; +import { ThemeContext } from "@/context/theme"; import { ConnectionContext } from "@/context/connection"; import { DataContext } from "@/context/data"; From 2d7521163232441de4011aa2904e5332feda0696 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:30:45 +0900 Subject: [PATCH 6/9] Move components/context/provider -> components/provider --- packages/profile/src/components/context/index.tsx | 1 - .../profile/src/components/{context => provider}/arcade.tsx | 0 .../profile/src/components/{context => provider}/connection.tsx | 0 packages/profile/src/components/{context => provider}/data.tsx | 0 .../src/components/{context/provider.tsx => provider/index.tsx} | 1 + packages/profile/src/components/{context => provider}/theme.tsx | 0 packages/profile/src/main.tsx | 2 +- 7 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 packages/profile/src/components/context/index.tsx rename packages/profile/src/components/{context => provider}/arcade.tsx (100%) rename packages/profile/src/components/{context => provider}/connection.tsx (100%) rename packages/profile/src/components/{context => provider}/data.tsx (100%) rename packages/profile/src/components/{context/provider.tsx => provider/index.tsx} (99%) rename packages/profile/src/components/{context => provider}/theme.tsx (100%) diff --git a/packages/profile/src/components/context/index.tsx b/packages/profile/src/components/context/index.tsx deleted file mode 100644 index 9c0bfb304..000000000 --- a/packages/profile/src/components/context/index.tsx +++ /dev/null @@ -1 +0,0 @@ -export { Provider } from "./provider"; diff --git a/packages/profile/src/components/context/arcade.tsx b/packages/profile/src/components/provider/arcade.tsx similarity index 100% rename from packages/profile/src/components/context/arcade.tsx rename to packages/profile/src/components/provider/arcade.tsx diff --git a/packages/profile/src/components/context/connection.tsx b/packages/profile/src/components/provider/connection.tsx similarity index 100% rename from packages/profile/src/components/context/connection.tsx rename to packages/profile/src/components/provider/connection.tsx diff --git a/packages/profile/src/components/context/data.tsx b/packages/profile/src/components/provider/data.tsx similarity index 100% rename from packages/profile/src/components/context/data.tsx rename to packages/profile/src/components/provider/data.tsx diff --git a/packages/profile/src/components/context/provider.tsx b/packages/profile/src/components/provider/index.tsx similarity index 99% rename from packages/profile/src/components/context/provider.tsx rename to packages/profile/src/components/provider/index.tsx index 92ef544ae..850633b8d 100644 --- a/packages/profile/src/components/context/provider.tsx +++ b/packages/profile/src/components/provider/index.tsx @@ -49,3 +49,4 @@ export function Provider({ children }: PropsWithChildren) { ); } + diff --git a/packages/profile/src/components/context/theme.tsx b/packages/profile/src/components/provider/theme.tsx similarity index 100% rename from packages/profile/src/components/context/theme.tsx rename to packages/profile/src/components/provider/theme.tsx diff --git a/packages/profile/src/main.tsx b/packages/profile/src/main.tsx index 5b1571d4b..7c12d5b0f 100644 --- a/packages/profile/src/main.tsx +++ b/packages/profile/src/main.tsx @@ -2,7 +2,7 @@ import { StrictMode } from "react"; import { createRoot } from "react-dom/client"; import { SonnerToaster } from "@cartridge/ui-next"; import { App } from "@/components/app"; -import { Provider } from "@/components/context"; +import { Provider } from "@/components/provider"; import "./index.css"; From c16e7142bcd4aa4145af5b192d857d55d8330baa Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:41:50 +0900 Subject: [PATCH 7/9] Separate primitive variants from component --- .../src/components/provider/connection.tsx | 27 ++++++----- .../profile/src/components/provider/data.tsx | 1 - .../profile/src/components/provider/index.tsx | 1 - .../components/primitives/alert-dialog.tsx | 2 +- .../src/components/primitives/badge.tsx | 4 +- .../components/primitives/button/index.tsx | 42 +++++++++++++++++ .../{button.tsx => button/utils.ts} | 46 +------------------ .../components/primitives/toggle-group.tsx | 2 +- .../components/primitives/toggle/index.tsx | 21 +++++++++ .../{toggle.tsx => toggle/utils.ts} | 26 +---------- 10 files changed, 83 insertions(+), 89 deletions(-) create mode 100644 packages/ui-next/src/components/primitives/button/index.tsx rename packages/ui-next/src/components/primitives/{button.tsx => button/utils.ts} (55%) create mode 100644 packages/ui-next/src/components/primitives/toggle/index.tsx rename packages/ui-next/src/components/primitives/{toggle.tsx => toggle/utils.ts} (51%) diff --git a/packages/profile/src/components/provider/connection.tsx b/packages/profile/src/components/provider/connection.tsx index 020f7f995..96beee06e 100644 --- a/packages/profile/src/components/provider/connection.tsx +++ b/packages/profile/src/components/provider/connection.tsx @@ -1,10 +1,5 @@ import { connectToParent } from "@cartridge/penpal"; -import { - useState, - ReactNode, - useEffect, - useCallback, -} from "react"; +import { useState, ReactNode, useEffect, useCallback } from "react"; import { ETH_CONTRACT_ADDRESS, normalize, @@ -12,7 +7,11 @@ import { } from "@cartridge/utils"; import { constants, getChecksumAddress, RpcProvider } from "starknet"; import { useNavigate, useSearchParams } from "react-router-dom"; -import { ConnectionContext, initialState, ParentMethods } from "@/context/connection"; +import { + ConnectionContext, + initialState, + ParentMethods, +} from "@/context/connection"; import { ConnectionContextType } from "@/context/connection"; export function ConnectionProvider({ children }: { children: ReactNode }) { @@ -52,13 +51,13 @@ export function ConnectionProvider({ children }: { children: ReactNode }) { STRK_CONTRACT_ADDRESS, ...(erc20Param ? decodeURIComponent(erc20Param) - .split(",") - .filter( - (address) => - ![ETH_CONTRACT_ADDRESS, STRK_CONTRACT_ADDRESS].includes( - getChecksumAddress(address), - ), - ) + .split(",") + .filter( + (address) => + ![ETH_CONTRACT_ADDRESS, STRK_CONTRACT_ADDRESS].includes( + getChecksumAddress(address), + ), + ) : []), ]; } diff --git a/packages/profile/src/components/provider/data.tsx b/packages/profile/src/components/provider/data.tsx index 61b6edc45..9c57dfca6 100644 --- a/packages/profile/src/components/provider/data.tsx +++ b/packages/profile/src/components/provider/data.tsx @@ -2,7 +2,6 @@ import { useState, ReactNode } from "react"; import { useAchievements } from "@/hooks/achievements"; import { DataContext } from "@/context/data"; - export function DataProvider({ children }: { children: ReactNode }) { const [accountAddress, setAccountAddress] = useState( undefined, diff --git a/packages/profile/src/components/provider/index.tsx b/packages/profile/src/components/provider/index.tsx index 850633b8d..92ef544ae 100644 --- a/packages/profile/src/components/provider/index.tsx +++ b/packages/profile/src/components/provider/index.tsx @@ -49,4 +49,3 @@ export function Provider({ children }: PropsWithChildren) { ); } - diff --git a/packages/ui-next/src/components/primitives/alert-dialog.tsx b/packages/ui-next/src/components/primitives/alert-dialog.tsx index 9a8655ad2..cb161842b 100644 --- a/packages/ui-next/src/components/primitives/alert-dialog.tsx +++ b/packages/ui-next/src/components/primitives/alert-dialog.tsx @@ -4,7 +4,7 @@ import * as React from "react"; import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"; import { cn } from "@/utils"; -import { buttonVariants } from "@/components/primitives/button"; +import { buttonVariants } from "@/components/primitives/button/utils"; const AlertDialog = AlertDialogPrimitive.Root; diff --git a/packages/ui-next/src/components/primitives/badge.tsx b/packages/ui-next/src/components/primitives/badge.tsx index 36209e8b8..58639e2fd 100644 --- a/packages/ui-next/src/components/primitives/badge.tsx +++ b/packages/ui-next/src/components/primitives/badge.tsx @@ -29,10 +29,8 @@ export interface BadgeProps extends React.HTMLAttributes, VariantProps {} -function Badge({ className, variant, ...props }: BadgeProps) { +export function Badge({ className, variant, ...props }: BadgeProps) { return (
); } - -export { Badge, badgeVariants }; diff --git a/packages/ui-next/src/components/primitives/button/index.tsx b/packages/ui-next/src/components/primitives/button/index.tsx new file mode 100644 index 000000000..58dcb2eb6 --- /dev/null +++ b/packages/ui-next/src/components/primitives/button/index.tsx @@ -0,0 +1,42 @@ +import * as React from "react"; +import { Slot } from "@radix-ui/react-slot"; +import { type VariantProps } from "class-variance-authority"; +import { cn } from "@/utils"; +import { Spinner } from "@/components/spinner"; +import { buttonVariants } from "./utils"; + +export interface ButtonProps + extends React.ButtonHTMLAttributes, + VariantProps { + asChild?: boolean; + isLoading?: boolean; +} + +export const Button = React.forwardRef( + ( + { + className, + variant, + size, + asChild = false, + isLoading, + children, + disabled, + ...props + }, + ref, + ) => { + const Comp = asChild ? Slot : "button"; + return ( + + {isLoading ? : children} + + ); + }, +); +Button.displayName = "Button"; diff --git a/packages/ui-next/src/components/primitives/button.tsx b/packages/ui-next/src/components/primitives/button/utils.ts similarity index 55% rename from packages/ui-next/src/components/primitives/button.tsx rename to packages/ui-next/src/components/primitives/button/utils.ts index 03b83be6e..f210bd0a9 100644 --- a/packages/ui-next/src/components/primitives/button.tsx +++ b/packages/ui-next/src/components/primitives/button/utils.ts @@ -1,10 +1,6 @@ -import * as React from "react"; -import { Slot } from "@radix-ui/react-slot"; -import { cva, type VariantProps } from "class-variance-authority"; -import { cn } from "@/utils"; -import { Spinner } from "../spinner"; +import { cva } from "class-variance-authority"; -const buttonVariants = cva( +export const buttonVariants = cva( "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 uppercase font-mono gap-1.5", { variants: { @@ -34,41 +30,3 @@ const buttonVariants = cva( }, }, ); - -export interface ButtonProps - extends React.ButtonHTMLAttributes, - VariantProps { - asChild?: boolean; - isLoading?: boolean; -} - -const Button = React.forwardRef( - ( - { - className, - variant, - size, - asChild = false, - isLoading, - children, - disabled, - ...props - }, - ref, - ) => { - const Comp = asChild ? Slot : "button"; - return ( - - {isLoading ? : children} - - ); - }, -); -Button.displayName = "Button"; - -export { Button, buttonVariants }; diff --git a/packages/ui-next/src/components/primitives/toggle-group.tsx b/packages/ui-next/src/components/primitives/toggle-group.tsx index ac360df2a..d74182ed9 100644 --- a/packages/ui-next/src/components/primitives/toggle-group.tsx +++ b/packages/ui-next/src/components/primitives/toggle-group.tsx @@ -5,7 +5,7 @@ import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group"; import { VariantProps } from "class-variance-authority"; import { cn } from "@/utils"; -import { toggleVariants } from "@/components/primitives/toggle"; +import { toggleVariants } from "@/components/primitives/toggle/utils"; const ToggleGroupContext = React.createContext< VariantProps diff --git a/packages/ui-next/src/components/primitives/toggle/index.tsx b/packages/ui-next/src/components/primitives/toggle/index.tsx new file mode 100644 index 000000000..624b1228c --- /dev/null +++ b/packages/ui-next/src/components/primitives/toggle/index.tsx @@ -0,0 +1,21 @@ +"use client"; + +import * as React from "react"; +import * as TogglePrimitive from "@radix-ui/react-toggle"; +import { type VariantProps } from "class-variance-authority"; +import { cn } from "@/utils"; +import { toggleVariants } from "./utils"; + +export const Toggle = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef & + VariantProps +>(({ className, variant, size, ...props }, ref) => ( + +)); + +Toggle.displayName = TogglePrimitive.Root.displayName; diff --git a/packages/ui-next/src/components/primitives/toggle.tsx b/packages/ui-next/src/components/primitives/toggle/utils.ts similarity index 51% rename from packages/ui-next/src/components/primitives/toggle.tsx rename to packages/ui-next/src/components/primitives/toggle/utils.ts index a516aab4c..7eca27ce9 100644 --- a/packages/ui-next/src/components/primitives/toggle.tsx +++ b/packages/ui-next/src/components/primitives/toggle/utils.ts @@ -1,12 +1,6 @@ -"use client"; +import { cva } from "class-variance-authority"; -import * as React from "react"; -import * as TogglePrimitive from "@radix-ui/react-toggle"; -import { cva, type VariantProps } from "class-variance-authority"; - -import { cn } from "@/utils"; - -const toggleVariants = cva( +export const toggleVariants = cva( "inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors hover:bg-muted hover:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground", { variants: { @@ -27,19 +21,3 @@ const toggleVariants = cva( }, }, ); - -const Toggle = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef & - VariantProps ->(({ className, variant, size, ...props }, ref) => ( - -)); - -Toggle.displayName = TogglePrimitive.Root.displayName; - -export { Toggle, toggleVariants }; From 20ecb06274fc006d4810de24144c4382a0759608 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:43:55 +0900 Subject: [PATCH 8/9] Remove empty file --- packages/profile/src/context/index.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 packages/profile/src/context/index.ts diff --git a/packages/profile/src/context/index.ts b/packages/profile/src/context/index.ts deleted file mode 100644 index e69de29bb..000000000 From bcadfc26b712e259821bd5fe7938d88203de022f Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 20 Jan 2025 19:50:07 +0900 Subject: [PATCH 9/9] Add missing dependencies in hooks --- packages/profile/src/components/achievements/trophy.tsx | 2 +- packages/profile/src/hooks/account.ts | 9 +-------- packages/profile/src/hooks/collection.ts | 2 +- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/profile/src/components/achievements/trophy.tsx b/packages/profile/src/components/achievements/trophy.tsx index 53e274a51..f6a6aa93e 100644 --- a/packages/profile/src/components/achievements/trophy.tsx +++ b/packages/profile/src/components/achievements/trophy.tsx @@ -442,7 +442,7 @@ Do you have what it takes to carve your name into history? )}&url=${encodeURIComponent(url)}`; window.open(twitterUrl, "_blank", "noopener,noreferrer"); - }, [url, xhandle, title, earning, date]); + }, [url, xhandle, title, earning, date, percentage]); if (!url || !xhandle) return null; diff --git a/packages/profile/src/hooks/account.ts b/packages/profile/src/hooks/account.ts index 8b8874eeb..ab5eaf885 100644 --- a/packages/profile/src/hooks/account.ts +++ b/packages/profile/src/hooks/account.ts @@ -120,14 +120,7 @@ export function useAccountInfo({ nameOrAddress }: { nameOrAddress: string }) { return "Please input a valid Starknet address"; } return ""; - }, [ - starkError, - controllerError, - address, - walletError, - controllerName, - starkName, - ]); + }, [starkError, controllerError, address, controllerName, starkName]); const warning = useMemo(() => { return walletError; diff --git a/packages/profile/src/hooks/collection.ts b/packages/profile/src/hooks/collection.ts index 84a84fc82..a5d3d3963 100644 --- a/packages/profile/src/hooks/collection.ts +++ b/packages/profile/src/hooks/collection.ts @@ -75,7 +75,7 @@ export function useCollection({ tokenIds = [] }: { tokenIds?: string[] }) { collection, assets, }; - }, [data, indexerUrl]); + }, [data?.tokenBalances?.edges, indexerUrl, tokenIds]); return { collection, assets, status }; }