mirror of
https://github.com/PrimalHQ/primal-web-app.git
synced 2024-09-29 00:10:50 +00:00
Disconnect from default relays when adding a first relay to your list
This commit is contained in:
parent
6d5d66d4b3
commit
93aa898dc1
@ -31,6 +31,7 @@ import { account } from "../translations";
|
||||
|
||||
export type AccountContextStore = {
|
||||
likes: string[],
|
||||
defaultRelays: string[],
|
||||
relays: Relay[],
|
||||
relaySettings: NostrRelays,
|
||||
publicKey: string | undefined,
|
||||
@ -60,11 +61,13 @@ export type AccountContextStore = {
|
||||
removeRelay: (url: string) => void,
|
||||
setConnectToPrimaryRelays: (flag: boolean) => void,
|
||||
changeCachingService: (url?: string) => void,
|
||||
dissconnectDefaultRelays: () => void,
|
||||
},
|
||||
}
|
||||
|
||||
const initialData = {
|
||||
likes: [],
|
||||
defaultRelays: [],
|
||||
relays: [],
|
||||
relaySettings: {},
|
||||
publicKey: undefined,
|
||||
@ -562,6 +565,28 @@ export function AccountProvider(props: { children: number | boolean | Node | JSX
|
||||
reset();
|
||||
};
|
||||
|
||||
const dissconnectDefaultRelays = () => {
|
||||
console.log('dissconnectDefaultRelays');
|
||||
for(let i=0; i < store.defaultRelays.length; i++) {
|
||||
const url = store.defaultRelays[i];
|
||||
|
||||
const relay = store.relays.find(r => r.url === url);
|
||||
|
||||
if (relay) {
|
||||
console.log('dissconnectDefaultRelays: ', relay.url);
|
||||
relay.close();
|
||||
updateStore('relays', () => store.relays.filter(r => r.url !== url));
|
||||
}
|
||||
|
||||
// Add relay to the list of explicitly closed relays
|
||||
relaysExplicitlyClosed.push(url);
|
||||
|
||||
// Reset connection attempts
|
||||
relayAtempts[url] = 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
// EFFECTS --------------------------------------
|
||||
|
||||
@ -726,6 +751,8 @@ export function AccountProvider(props: { children: number | boolean | Node | JSX
|
||||
if (type === 'EVENT') {
|
||||
const resp = JSON.parse(content.content || '[]');
|
||||
|
||||
updateStore('defaultRelays', () => [...resp]);
|
||||
|
||||
const relaySettings: NostrRelays = resp.reduce((acc: NostrRelays, r: string) => ({ ...acc, [r]: { read: true, write: true }}), {});
|
||||
|
||||
if (Object.keys(relaySettings).length > 0) {
|
||||
@ -756,6 +783,7 @@ const [store, updateStore] = createStore<AccountContextStore>({
|
||||
removeRelay,
|
||||
setConnectToPrimaryRelays,
|
||||
changeCachingService,
|
||||
dissconnectDefaultRelays,
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -86,9 +86,17 @@ const Network: Component = () => {
|
||||
|
||||
const onAddRelay = (url: string) => {
|
||||
const rels: string[] = import.meta.env.PRIMAL_PRIORITY_RELAYS?.split(',') || [];
|
||||
|
||||
if (rels.includes(url)) {
|
||||
account?.actions.setConnectToPrimaryRelays(true);
|
||||
}
|
||||
|
||||
const myRelays = relays();
|
||||
|
||||
if (myRelays.length === 0) {
|
||||
account?.actions.dissconnectDefaultRelays()
|
||||
}
|
||||
|
||||
account?.actions.addRelay(url);
|
||||
};
|
||||
|
||||
@ -166,6 +174,10 @@ const Network: Component = () => {
|
||||
{intl.formatMessage(t.network.myRelays)}
|
||||
</div>
|
||||
|
||||
<Show
|
||||
when={relays().length > 0}
|
||||
fallback={<div class={styles.noMyRelays}>{intl.formatMessage(t.network.noMyRelays)}</div>}
|
||||
>
|
||||
<For each={relays()}>
|
||||
{relay => (
|
||||
<button class={styles.relayItem} onClick={() => setConfirmRemoveRelay(relay.url)}>
|
||||
@ -185,6 +197,8 @@ const Network: Component = () => {
|
||||
</button>
|
||||
)}
|
||||
</For>
|
||||
</Show>
|
||||
|
||||
|
||||
<Show when={!isPrimalRelayInUserSettings()}>
|
||||
<Checkbox
|
||||
@ -195,6 +209,7 @@ const Network: Component = () => {
|
||||
/>
|
||||
</Show>
|
||||
|
||||
<Show when={otherRelays().length > 0}>
|
||||
<div class={`${styles.settingsCaption} ${styles.secondCaption}`}>
|
||||
{intl.formatMessage(t.network.recomended)}
|
||||
</div>
|
||||
@ -214,6 +229,8 @@ const Network: Component = () => {
|
||||
)}
|
||||
</For>
|
||||
|
||||
</Show>
|
||||
|
||||
<div class={`${styles.settingsCaption} ${styles.secondCaption}`}>
|
||||
{intl.formatMessage(t.network.customRelay)}
|
||||
</div>
|
||||
|
@ -72,6 +72,7 @@
|
||||
border: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
text-transform: lowercase;
|
||||
|
||||
&:hover {
|
||||
text-decoration: underline;
|
||||
@ -443,3 +444,11 @@
|
||||
line-height: 28px;
|
||||
}
|
||||
}
|
||||
|
||||
.noMyRelays {
|
||||
color: var(--text-secondary);
|
||||
font-size: 14px;
|
||||
font-weight: 400;
|
||||
line-height: 20px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
@ -902,6 +902,11 @@ export const settings = {
|
||||
defaultMessage: 'My Relays',
|
||||
description: 'Title of the my relays section of the network settings sub-page',
|
||||
},
|
||||
noMyRelays: {
|
||||
id: 'settings.networks.noMyRelays',
|
||||
defaultMessage: 'Your Nostr account doesn\'t have any relays specified, so we connected you to a default set of relays. To configure your desired set of relays, please select them from the list below.',
|
||||
description: 'Caption informing the user that he has no relays configured',
|
||||
},
|
||||
recomended: {
|
||||
id: 'settings.network.recomended',
|
||||
defaultMessage: 'Recomended Relays',
|
||||
|
Loading…
Reference in New Issue
Block a user