Skip to content
Merged
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
15 changes: 12 additions & 3 deletions src/components/RelaySelector/hooks/useSelectedRelay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useEffect, useMemo } from 'react';
import { useLocalStorage } from 'usehooks-ts';
import { RelayMode } from '../type';
import { useSelector } from 'react-redux';
import { RootState } from 'store/configureStore';
import { useReadonlyMyPublicKey } from 'hooks/useMyPublicKey';
import { isValidPublicKey } from 'utils/validator';

const SELECTED_RELAY_MODE_KEY = 'relay-selector:selected-mode:{{pubKey}}';
const SELECTED_RELAY_GROUP_ID_KEY =
Expand All @@ -23,14 +27,19 @@ const getLegacyLocalValue = (key: string) => {
}
};

export function useSelectedRelay(myPublicKey: string) {
export function useSelectedRelay() {
const localPubkey = useReadonlyMyPublicKey();
const pubkey = useSelector(
(state: RootState) => state.loginReducer.publicKey,
);
const myPublicKey = isValidPublicKey(localPubkey) ? localPubkey : pubkey;
const selectedModeKey = SELECTED_RELAY_MODE_KEY.replace(
'{{pubKey}}',
myPublicKey,
myPublicKey ?? 'unknown',
);
const selectedGroupIdKey = SELECTED_RELAY_GROUP_ID_KEY.replace(
'{{pubKey}}',
myPublicKey,
myPublicKey ?? 'unknown',
);

const [selectedMode = getLegacyLocalValue(selectedModeKey), setSelectedMode] =
Expand Down
2 changes: 1 addition & 1 deletion src/components/RelaySelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function RelaySelector({
const myPublicKey = useReadonlyMyPublicKey();
const defaultGroup = useDefaultGroup();
const [relayGroupMap, setRelayGroupMap] = useState<RelayGroupMap>(new Map());
const [selectedRelay, setSelectedRelay] = useSelectedRelay(myPublicKey);
const [selectedRelay, setSelectedRelay] = useSelectedRelay();

useEffect(() => {
if (newConnCallback) {
Expand Down
6 changes: 6 additions & 0 deletions src/store/loginReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export async function getLoginInfo(request: LoginRequest): Promise<Signer> {
return {
mode,
isLoggedIn: isLoggedIn,
publicKey: pk,
getPublicKey: async () => {
return await window.nostr!.getPublicKey();
},
Expand All @@ -198,6 +199,7 @@ export async function getLoginInfo(request: LoginRequest): Promise<Signer> {
return {
mode,
isLoggedIn: isLoggedIn,
publicKey: pk,
getPublicKey: async () => {
return await requestPublicKeyFromDotBit(request.didAlias!);
},
Expand All @@ -219,6 +221,7 @@ export async function getLoginInfo(request: LoginRequest): Promise<Signer> {
return {
mode,
isLoggedIn: true,
publicKey: pk,
getPublicKey: async () => {
return await requestPublicKeyFromNip05DomainName(
request.nip05DomainName!,
Expand All @@ -244,6 +247,7 @@ export async function getLoginInfo(request: LoginRequest): Promise<Signer> {
return {
mode,
isLoggedIn: isLoggedIn,
publicKey: pk,
getPublicKey: getPublicKey,
signEvent: createMetamaskSignEvent(request.evmUsername),
evmUsername: request.evmUsername,
Expand All @@ -264,6 +268,7 @@ export async function getLoginInfo(request: LoginRequest): Promise<Signer> {

return {
mode,
publicKey: pk,
isLoggedIn: isLoggedIn,
getPublicKey: getPublicKey,
signEvent: createWalletConnectSignEvent(request.evmUsername),
Expand All @@ -279,6 +284,7 @@ export async function getLoginInfo(request: LoginRequest): Promise<Signer> {

return {
mode,
publicKey: pk,
isLoggedIn: isLoggedIn,
getPublicKey: async () => {
return await joyIdNostr.getPublicKey();
Expand Down