Skip to content

Commit

Permalink
resolve circular dependencies (#1414)
Browse files Browse the repository at this point in the history
* fix(dcd): remove SelectRolePanel

* fix(dcd): extract constant and update import

* fix(dcd): refactor and pull useSubmit hook apart

* chore: prevent barrel file import

* chore: isolate more util functions

* Revert "fix(dcd): remove SelectRolePanel"

This reverts commit 960fb06.

* refactor: move fuelSign into utils

* fix(dcd): pull apart ToolbarPlugin

* fix: update imports

* fix: rename imports

* fix(dcd): pull apart EditGuildDrawerContext

* fix: update EditGuildProvider import
  • Loading branch information
dominik-stumpf authored Jul 29, 2024
1 parent 412e241 commit cf25a5d
Show file tree
Hide file tree
Showing 29 changed files with 302 additions and 302 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useGuild from "components/[guild]/hooks/useGuild"
import useShowErrorToast from "hooks/useShowErrorToast"
import { SignedValidation, useSubmitWithSign } from "hooks/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/types"
import useToast from "hooks/useToast"
import fetcher from "utils/fetcher"

Expand Down
2 changes: 1 addition & 1 deletion src/components/[guild]/EditGuild/EditGuildDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { Chain } from "wagmiConfig/chains"
import LeaveButton from "../LeaveButton"
import useGuildPermission from "../hooks/useGuildPermission"
import useUser from "../hooks/useUser"
import Admins from "./components/Admins"
import Admins from "./components/Admins/Admins"
import BackgroundImageUploader from "./components/BackgroundImageUploader"
import ChangingGuildPinDesignAlert from "./components/ChangingGuildPinDesignAlert"
import ColorPicker from "./components/ColorPicker"
Expand Down
29 changes: 3 additions & 26 deletions src/components/[guild]/EditGuild/EditGuildDrawerContext.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
import { useDisclosure } from "@chakra-ui/react"
import dynamic from "next/dynamic"
import { PropsWithChildren, createContext, useContext } from "react"
import useGuild from "../hooks/useGuild"
import useGuildPermission from "../hooks/useGuildPermission"
import { createContext, useContext } from "react"

const EditGuildDrawerContext = createContext<{
export const EditGuildDrawerContext = createContext<{
isOpen: boolean
onOpen: () => void
onClose: () => void
}>(undefined)

const DynamicEditGuildDrawer = dynamic(() => import("./EditGuildDrawer"))

const EditGuildDrawerProvider = ({
children,
}: PropsWithChildren<unknown>): JSX.Element => {
const { isDetailed } = useGuild()
const { isAdmin } = useGuildPermission()
const { isOpen, onOpen, onClose } = useDisclosure()

return (
<EditGuildDrawerContext.Provider value={{ isOpen, onOpen, onClose }}>
{children}
{isAdmin && isDetailed && (
<DynamicEditGuildDrawer isOpen={isOpen} onClose={onClose} />
)}
</EditGuildDrawerContext.Provider>
)
}

const useEditGuildDrawer = () => useContext(EditGuildDrawerContext)

export { EditGuildDrawerProvider, useEditGuildDrawer }
export { useEditGuildDrawer }
24 changes: 24 additions & 0 deletions src/components/[guild]/EditGuild/EditGuildDrawerProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useDisclosure } from "@chakra-ui/react"
import dynamic from "next/dynamic"
import { PropsWithChildren } from "react"
import useGuild from "../hooks/useGuild"
import useGuildPermission from "../hooks/useGuildPermission"
import { EditGuildDrawerContext } from "./EditGuildDrawerContext"

const DynamicEditGuildDrawer = dynamic(() => import("./EditGuildDrawer"))
export const EditGuildDrawerProvider = ({
children,
}: PropsWithChildren<unknown>): JSX.Element => {
const { isDetailed } = useGuild()
const { isAdmin } = useGuildPermission()
const { isOpen, onOpen, onClose } = useDisclosure()

return (
<EditGuildDrawerContext.Provider value={{ isOpen, onOpen, onClose }}>
{children}
{isAdmin && isDetailed && (
<DynamicEditGuildDrawer isOpen={isOpen} onClose={onClose} />
)}
</EditGuildDrawerContext.Provider>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { ADDRESS_REGEX } from "utils/guildCheckout/constants"
import shortenHex from "utils/shortenHex"
import { useEnsAddress } from "wagmi"
import { mainnet } from "wagmi/chains"
import TransferOwnership from "../TransferOwnership"
import TransferOwnership from "../TransferOwnership/TransferOwnership"

export const isValidAddress = (address: string) =>
ADDRESS_REGEX.test(address) || FUEL_ADDRESS_REGEX.test(address)
Expand Down
3 changes: 0 additions & 3 deletions src/components/[guild]/EditGuild/components/Admins/index.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AccessCheckJob, JoinJob } from "@guildxyz/types"
import useGuild from "components/[guild]/hooks/useGuild"
import useMembership from "components/explorer/hooks/useMembership"
import { useFetcherWithSign } from "hooks/useFetcherWithSign"
import { UseSubmitOptions } from "hooks/useSubmit/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/types"
import { atom, useAtom } from "jotai"
import useSWRImmutable from "swr/immutable"
import { groupBy } from "../utils/mapAccessJobState"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { env } from "env"
import { useFetcherWithSign } from "hooks/useFetcherWithSign"
import usePopupWindow from "hooks/usePopupWindow"
import useSubmit, { SignedValidation, useSubmitWithSign } from "hooks/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/types"
import { useSetAtom } from "jotai"
import { OAuthResultParams } from "pages/oauth-result"
import { useCallback, useMemo } from "react"
Expand Down
4 changes: 2 additions & 2 deletions src/components/[guild]/JoinModal/hooks/useJoin.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { JoinJob } from "@guildxyz/types"
import { CircleWavyCheck } from "@phosphor-icons/react"
import { GUILD_PIN_MAINTENANCE } from "components/[guild]/Requirements/components/GuildCheckout/MintGuildPin/MintGuildPin"
import { GUILD_PIN_MAINTENANCE } from "components/[guild]/Requirements/components/GuildCheckout/MintGuildPin/constants"
import { useMintGuildPinContext } from "components/[guild]/Requirements/components/GuildCheckout/MintGuildPinContext"
import useGuild from "components/[guild]/hooks/useGuild"
import useUser from "components/[guild]/hooks/useUser"
import { UseSubmitOptions } from "hooks/useSubmit/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/types"
import { useToastWithButton, useToastWithTweetButton } from "hooks/useToast"
import { useRouter } from "next/router"
import { useState } from "react"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useMembership from "components/explorer/hooks/useMembership"
import useCustomPosthogEvents from "hooks/useCustomPosthogEvents"
import { useFetcherWithSign } from "hooks/useFetcherWithSign"
import useSubmit from "hooks/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/useSubmit"
import { UseSubmitOptions } from "hooks/useSubmit/types"
import { atom, useAtom } from "jotai"
import useUsersPoints from "rewards/Points/useUsersPoints"
import getGuildPlatformsOfRoles from "../utils/getGuildPlatformsOfRoles"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Modal } from "components/common/Modal"
import { useMintGuildPinContext } from "../../MintGuildPinContext"
import AlphaTag from "../../components/AlphaTag"
import GuildPinImage from "../../components/GuildPinImage"
import { GUILD_PIN_MAINTENANCE } from "../MintGuildPin"
import { GUILD_PIN_MAINTENANCE } from "../constants"
import FuelGuildPinFees from "./FuelGuildPinFees"
import MintFuelGuildPinButton from "./MintFuelGuildPinButton"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import TransactionStatusModal from "../components/TransactionStatusModal"
import GuildPinOpenseaLink from "../components/TransactionStatusModal/components/GuildPinOpenseaLink"
import MintGuildPinButton from "../components/buttons/MintGuildPinButton"
import SwitchNetworkButton from "../components/buttons/SwitchNetworkButton"

export const GUILD_PIN_MAINTENANCE = false
import { GUILD_PIN_MAINTENANCE } from "./constants"

const DynamicActivateGuildPinModal = dynamic(
() => import("./components/ActivateGuildPinModal")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GUILD_PIN_MAINTENANCE = false
25 changes: 3 additions & 22 deletions src/components/lexical/plugins/ToolbarPlugin/ToolbarPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
$createQuoteNode,
$isHeadingNode,
} from "@lexical/rich-text"
import { $isAtNodeEnd, $wrapNodes } from "@lexical/selection"
import { $wrapNodes } from "@lexical/selection"
import { $getNearestNodeOfType, mergeRegister } from "@lexical/utils"
import {
ArrowClockwise,
Expand Down Expand Up @@ -56,8 +56,8 @@ import {
import { useCallback, useEffect, useRef, useState } from "react"
import ImageEditor from "./components/ImageEditor"
import LinkEditor from "./components/LinkEditor"

export const LOW_PRIORITY = 1
import { LOW_PRIORITY } from "./constants"
import { getSelectedNode } from "./utils"

const supportedBlockTypes = [
"paragraph",
Expand Down Expand Up @@ -94,25 +94,6 @@ const blockTypeToBlockIcon: Record<
code: Code,
}

export const getSelectedNode = (selection) => {
const anchor = selection.anchor
const focus = selection.focus
const anchorNode = selection.anchor.getNode()
const focusNode = selection.focus.getNode()

if (anchorNode === focusNode) {
return anchorNode
}

const isBackward = selection.isBackward()

if (isBackward) {
return $isAtNodeEnd(focus) ? anchorNode : focusNode
} else {
return $isAtNodeEnd(anchor) ? focusNode : anchorNode
}
}

const BlockOptionsMenuList = ({
editor,
blockType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { $getSelection, $isRangeSelection, SELECTION_CHANGE_COMMAND } from "lexi
import { useCallback, useEffect, useRef, useState } from "react"
import { FormProvider, useForm } from "react-hook-form"
import { ensureUrlProtocol } from "utils/ensureUrlProtocol"
import { LOW_PRIORITY, getSelectedNode } from "../ToolbarPlugin"
import { LOW_PRIORITY } from "../constants"
import { getSelectedNode } from "../utils"

type LinkEditorProps = {
isOpen: boolean
Expand Down
1 change: 1 addition & 0 deletions src/components/lexical/plugins/ToolbarPlugin/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LOW_PRIORITY = 1
20 changes: 20 additions & 0 deletions src/components/lexical/plugins/ToolbarPlugin/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { $isAtNodeEnd } from "@lexical/selection"

export const getSelectedNode = (selection) => {
const anchor = selection.anchor
const focus = selection.focus
const anchorNode = selection.anchor.getNode()
const focusNode = selection.focus.getNode()

if (anchorNode === focusNode) {
return anchorNode
}

const isBackward = selection.isBackward()

if (isBackward) {
return $isAtNodeEnd(focus) ? anchorNode : focusNode
} else {
return $isAtNodeEnd(anchor) ? focusNode : anchorNode
}
}
2 changes: 1 addition & 1 deletion src/hooks/useSetKeyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { StoredKeyPair, setKeyPairToIdb } from "utils/keyPair"
import { recaptchaAtom, shouldUseReCAPTCHAAtom } from "utils/recaptcha"
import { checksumAddress } from "viem"
import useSubmit from "./useSubmit"
import { MessageParams, SignProps, UseSubmitOptions } from "./useSubmit/useSubmit"
import { MessageParams, SignProps, UseSubmitOptions } from "./useSubmit/types"

function getSiweMessage({
addr,
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useSubmit/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULT_MESSAGE = "Please sign this message"
export const DEFAULT_SIGN_LOADING_TEXT = "Check your wallet"
4 changes: 2 additions & 2 deletions src/hooks/useSubmit/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default, sign, useSubmitWithSign } from "./useSubmit"
export type { SignedValidation, Validation } from "./useSubmit"
export { default, useSubmitWithSign } from "./useSubmit"
export type { SignedValidation, Validation } from "./types"
46 changes: 46 additions & 0 deletions src/hooks/useSubmit/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Account } from "fuels"
import { ValidationMethod } from "types"
import { PublicClient, WalletClient } from "viem"

type SignBaseProps = {
address: `0x${string}`
payload: string
chainId?: string
forcePrompt: boolean
keyPair?: CryptoKeyPair
msg?: string
ts?: number
getMessageToSign?: (params: MessageParams) => string
}

export type SignProps = SignBaseProps & {
publicClient: PublicClient
walletClient: WalletClient
}

export type FuelSignProps = SignBaseProps & { wallet: Account }

export type SignedValidation = { signedPayload: string; validation: Validation }

export type Validation = {
params: MessageParams
sig: string
}

export type MessageParams = {
msg: string
addr: string
method: ValidationMethod
chainId?: string
hash?: string
nonce: string
ts: string
}

export type UseSubmitOptions<ResponseType = void> = {
onSuccess?: (response: ResponseType) => void
onError?: (error: any) => void

// Use catefully! If this is set to true, a .onSubmit() call can reject!
allowThrow?: boolean
}
Loading

0 comments on commit cf25a5d

Please sign in to comment.