Skip to content

Commit

Permalink
fix(ssr): ux (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanosync authored Jul 31, 2024
1 parent d5be80e commit d806800
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
18 changes: 10 additions & 8 deletions packages/ord-connect/src/components/OrdConnectKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ export function OrdConnectKit({
renderAvatar={renderAvatar}
/>
) : (
<PreConnectButton openModal={openModal} />
<PreConnectButton disabled={!hasMounted} openModal={openModal} />
);
};

return hasMounted ? (
return (
<>
{renderConnectButton()}
<SelectWalletModal
isOpen={isModalOpen}
closeModal={closeModal}
renderAvatar={renderAvatar}
/>
{hasMounted ? (
<SelectWalletModal
isOpen={isModalOpen}
closeModal={closeModal}
renderAvatar={renderAvatar}
/>
) : null}
</>
) : null;
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import type { OrdConnectKitProp } from "../OrdConnectKit";

interface PreConnectButtonProp extends OrdConnectKitProp {
openModal: () => void;
disabled?: boolean;
}

export function PreConnectButton({ openModal }: PreConnectButtonProp) {
export function PreConnectButton({
openModal,
disabled,
}: PreConnectButtonProp) {
return (
<button

Check warning on line 13 in packages/ord-connect/src/components/PreConnectButton/index.tsx

View workflow job for this annotation

GitHub Actions / Build (Apps & Packages)

A control must be associated with a text label

Check warning on line 13 in packages/ord-connect/src/components/PreConnectButton/index.tsx

View workflow job for this annotation

GitHub Actions / Lint - Typescript and ESLint

A control must be associated with a text label

Check warning on line 13 in packages/ord-connect/src/components/PreConnectButton/index.tsx

View workflow job for this annotation

GitHub Actions / NPM

A control must be associated with a text label
type="button"
onClick={openModal}
disabled={disabled}
data-testid="connect-wallet-button"
className="ord-connect-font ord-connect-wallet-button"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
border: 0px;
}

.ord-connect-wallet-button:disabled {
background-color: #888;
cursor: not-allowed;
}

.ord-connect-wallet-button span::before {
content: "Connect";
}
Expand All @@ -23,10 +28,10 @@
}
}

.ord-connect-wallet-button:hover {
.ord-connect-wallet-button:enabled:hover {
background-color: #f2f2f2;
}

.ord-connect-wallet-button:active {
.ord-connect-wallet-button:enabled:active {
opacity: 0.7;
}
28 changes: 22 additions & 6 deletions packages/ord-connect/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Dispatch, SetStateAction, useCallback, useState } from "react";
import {
Dispatch,
SetStateAction,
useCallback,
useEffect,
useState,
} from "react";

const KEY_PREFIX = "ord-connect";

Expand Down Expand Up @@ -43,18 +49,23 @@ function setItemToLocalStorage<T>(_key: string, value: T) {
export function useLocalStorage<T>(
key: string,
initialValue: T,
options: { initializeWithValue?: boolean } = {},
): [T, Dispatch<SetStateAction<T>>] {
const [state, setInnerState] = useState<T>(() => {
if (typeof window === "undefined") {
return initialValue;
}

const { initializeWithValue = true } = options;
const readValue = useCallback(() => {
const value = getItemFromLocalStorage<T>(key);
if (!value) {
setItemToLocalStorage(key, initialValue);
return initialValue;
}
return value;
}, [initialValue, key]);

const [state, setInnerState] = useState<T>(() => {
if (initializeWithValue) {
return readValue();
}
return initialValue;
});

const setState: Dispatch<SetStateAction<T>> = useCallback(
Expand All @@ -65,5 +76,10 @@ export function useLocalStorage<T>(
[key],
);

useEffect(() => {
setInnerState(readValue());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key]);

return [state, setState];
}
6 changes: 6 additions & 0 deletions packages/ord-connect/src/providers/OrdConnectProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const FORMAT = "format";

export type OrdConnectProviderProps = {
initialNetwork: Network;
ssr?: boolean;
};

/**
Expand All @@ -88,11 +89,13 @@ export type OrdConnectProviderProps = {
*
* @param props - Props object.
* @param props.initialNetwork - Initial network state if network is not set.
* @param props.ssr - Enable SSR.
* @returns Provider component for OrdConnect.
*/
export function OrdConnectProvider({
children,
initialNetwork,
ssr = false,
}: PropsWithChildren<OrdConnectProviderProps>) {
if (!initialNetwork) {
throw new Error("Initial network cannot be empty");
Expand All @@ -102,17 +105,20 @@ export function OrdConnectProvider({
const [address, setAddress] = useLocalStorage<BiAddressString>(
ADDRESS,
EMPTY_BIADDRESS_OBJECT,
{ initializeWithValue: !ssr },
);

const [wallet, setWallet] = useLocalStorage<Wallet | null>(WALLET, null);
const [publicKey, setPublicKey] = useLocalStorage<BiAddressString>(
PUBLIC_KEY,
EMPTY_BIADDRESS_OBJECT,
{ initializeWithValue: !ssr },
);

const [format, setFormat] = useLocalStorage<BiAddressFormat>(
FORMAT,
EMPTY_BIADDRESS_OBJECT,
{ initializeWithValue: !ssr },
);

const [isModalOpen, setIsModalOpen] = useState(false);
Expand Down
Binary file modified preview_wallets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d806800

Please sign in to comment.