Skip to content

Commit

Permalink
Fix update coordinators urls on network change
Browse files Browse the repository at this point in the history
  • Loading branch information
Reckless-Satoshi committed Dec 30, 2023
1 parent 278c222 commit bfb97e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/RobotAvatar/robohash.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ self.addEventListener('message', async (event) => {
const avatarB64 = await async_generate_robohash(hash, size == 'small' ? 80 : 256);
const imageUrl = `data:image/png;base64,${avatarB64}`;
const t1 = performance.now();
console.log(`Worker ${workerIndex} :: Time to generate avatar: ${t1 - t0} ms`);
console.log(`Avatar generated in: ${t1 - t0} ms`);
// Send the result back to the main thread
self.postMessage({ cacheKey, imageUrl });
});
6 changes: 5 additions & 1 deletion frontend/src/components/SettingsForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ import {
} from '@mui/icons-material';
import { systemClient } from '../../services/System';
import SwapCalls from '@mui/icons-material/SwapCalls';
import { FederationContext, UseFederationStoreType } from '../../contexts/FederationContext';

interface SettingsFormProps {
dense?: boolean;
}

const SettingsForm = ({ dense = false }: SettingsFormProps): JSX.Element => {
const { fav, setFav, settings, setSettings } = useContext<UseAppStoreType>(AppContext);
const { fav, setFav, origin, hostUrl, settings, setSettings } =
useContext<UseAppStoreType>(AppContext);
const { federation } = useContext<UseFederationStoreType>(FederationContext);
const theme = useTheme();
const { t } = useTranslation();
const fontSizes = [
Expand Down Expand Up @@ -223,6 +226,7 @@ const SettingsForm = ({ dense = false }: SettingsFormProps): JSX.Element => {
value={settings.network}
onChange={(e, network) => {
setSettings({ ...settings, network });
federation.updateUrls(origin, { ...settings, network }, hostUrl);
systemClient.setItem('settings_network', network);
}}
>
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/models/Coordinator.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,20 @@ export class Coordinator {
onStarted: (shortAlias: string) => void = () => {},
): Promise<void> => {
if (this.enabled !== true) return;
void this.updateUrl(settings, origin, hostUrl);
void this.update(() => {
onStarted(this.shortAlias);
});
};

updateUrl = async (settings: Settings, origin: Origin, hostUrl: string): Promise<void> => {
if (settings.selfhostedClient && this.shortAlias !== 'local') {
this.url = hostUrl;
this.basePath = `/${settings.network}/${this.shortAlias}`;
} else {
this.url = String(this[settings.network][origin]);
this.basePath = '';
}
void this.update(() => {
onStarted(this.shortAlias);
});
};

update = async (onUpdate: (shortAlias: string) => void = () => {}): Promise<void> => {
Expand All @@ -170,6 +173,7 @@ export class Coordinator {

loadBook = (onDataLoad: () => void = () => {}): void => {
if (!this.enabled) return;
if (this.url === '') return;
if (this.loadingBook) return;

this.loadingBook = true;
Expand All @@ -196,6 +200,7 @@ export class Coordinator {

loadLimits = (onDataLoad: () => void = () => {}): void => {
if (!this.enabled) return;
if (this.url === '') return;
if (this.loadingLimits) return;

this.loadingLimits = true;
Expand Down Expand Up @@ -224,6 +229,7 @@ export class Coordinator {

loadInfo = (onDataLoad: () => void = () => {}): void => {
if (!this.enabled) return;
if (this.url === '') return;
if (this.loadingInfo) return;

this.loadingInfo = true;
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/models/Federation.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export class Federation {
}
};

// On Testnet/Mainnet change
updateUrls = async (origin: Origin, settings: Settings, hostUrl: string): Promise<void> => {
this.loading = true;
for (const coor of Object.values(this.coordinators)) {
await coor.updateUrl(settings, origin, hostUrl);
}
};

update = async (): Promise<void> => {
this.loading = false;
for (const coor of Object.values(this.coordinators)) {
Expand Down

0 comments on commit bfb97e0

Please sign in to comment.