Skip to content

Commit

Permalink
fix: move the instance URL out of settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexogamer committed Feb 16, 2025
1 parent d1b8d01 commit 0a1f1d4
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
8 changes: 0 additions & 8 deletions src/Generic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,6 @@ export const app = {
default: false,
type: 'boolean',
},

// instance URL
{
key: 'app.instance',
category: 'donotshow',
default: DEFAULT_API_URL,
type: 'string',
},
] as Setting[],
},
setTheme: (themeName: string) => {
Expand Down
3 changes: 2 additions & 1 deletion src/SideMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {client} from '@clerotri/lib/client';
import {DEFAULT_API_URL} from '@clerotri/lib/consts';
import {ChannelContext, SideMenuContext} from '@clerotri/lib/state';
import {storage} from '@clerotri/lib/storage';
import {getInstanceURL} from '@clerotri/lib/storage/utils';
import {commonValues, Theme, ThemeContext} from '@clerotri/lib/themes';
import {useBackHandler} from '@clerotri/lib/ui';

Expand Down Expand Up @@ -73,7 +74,7 @@ const SideMenu = () => {
<ServerList
onServerPress={(s: Server) => setCurrentServer(s)}
onServerLongPress={(s: Server) => app.openServerContextMenu(s)}
showDiscover={app.settings.get('app.instance') === DEFAULT_API_URL}
showDiscover={getInstanceURL() === DEFAULT_API_URL}
/>
</ScrollView>
<ChannelList currentServer={currentServer} />
Expand Down
2 changes: 2 additions & 0 deletions src/components/sheets/SettingsSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {app, setFunction} from '@clerotri/Generic';
import {client} from '@clerotri/lib/client';
import {OPEN_ISSUES} from '@clerotri/lib/consts';
import {storage} from '@clerotri/lib/storage';
import {getInstanceURL} from '@clerotri/lib/storage/utils';
import {ThemeContext} from '@clerotri/lib/themes';
import {SettingsSection} from '@clerotri/lib/types';
import {openUrl} from '@clerotri/lib/utils';
Expand Down Expand Up @@ -48,6 +49,7 @@ async function copyDebugInfo() {
},

appInfo: {
instance: getInstanceURL(),
userID: client.user?._id ?? 'ERR_ID_UNDEFINED',
settings: storage.getString('settings'),
version: app.version,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export async function loginWithSavedToken(status: string) {
await client.useExistingSession({token: res});
} catch (e: any) {
console.log(e);
!e.message?.startsWith('Read error') && e.message !== 'Network Error' &&
!e.message?.startsWith('Read error') &&
e.message !== 'Network Error' &&
client.user
? app.setLoggedOutScreen('loginPage')
: status === 'loggedIn'
Expand Down
14 changes: 6 additions & 8 deletions src/lib/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import {Client} from 'revolt.js';

import {app} from '@clerotri/Generic';
import {DEFAULT_API_URL} from '@clerotri/lib/consts';
import {storage} from '@clerotri/lib/storage';

function getAPIURL() {
console.log(`[APP] Initialised settings (${new Date().getTime()})`);
let url: string = '';
console.log('[AUTH] Getting API URL...');
const instance = app.settings.get('app.instance') as
| string
| null
| undefined;

const instance = storage.getString('instanceURL');

if (!instance) {
console.log(
'[AUTH] Unable to fetch app.instance; setting apiURL to default',
'[AUTH] Unable to fetch instanceURL; setting apiURL to default',
);
url = DEFAULT_API_URL;
} else {
console.log(`[AUTH] Fetched app.instance; setting apiURL to ${instance}`);
console.log(`[AUTH] Fetched instanceURL; setting apiURL to ${instance}`);
url = instance;
}
return url;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/storage/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {app} from '@clerotri/Generic';
import {DEFAULT_API_URL} from '@clerotri/lib/consts';
import {storage} from '@clerotri/lib/storage';
import {Setting} from '@clerotri/lib/types';

Expand All @@ -8,6 +9,9 @@ export function initialiseSettings() {
try {
const settings = JSON.parse(s) as {key: string; value: any}[];
settings.forEach(key => {
if (key.key === 'app.instance') {
storage.set('instanceURL', key.value);
}
let st: Setting | undefined;
for (const setting of app.settings.list) {
if (setting.key === key.key) {
Expand All @@ -26,3 +30,7 @@ export function initialiseSettings() {
}
}
}

export function getInstanceURL() {
return storage.getString('instanceURL') ?? DEFAULT_API_URL;
}
3 changes: 2 additions & 1 deletion src/pages/auth/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@clerotri/components/common/atoms';
import {loginRegular, loginWithToken} from '@clerotri/lib/auth';
import {OFFICIAL_INSTANCE_SIGNUP_URL} from '@clerotri/lib/consts';
import {getInstanceURL} from '@clerotri/lib/storage/utils';
import {commonValues, ThemeContext} from '@clerotri/lib/themes';
import {useBackHandler} from '@clerotri/lib/ui';
import {openUrl} from '@clerotri/lib/utils';
Expand Down Expand Up @@ -93,7 +94,7 @@ function LoginTypeSelector({
</Button>
<Text font={'Inter'} colour={currentTheme.foregroundSecondary}>
{t('app.login.instance_notice', {
url: app.settings.get('app.instance'),
url: getInstanceURL(),
})}
</Text>
</>
Expand Down
9 changes: 4 additions & 5 deletions src/pages/auth/LoginSettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import {useContext, useState} from 'react';
import {View} from 'react-native';

import {app} from '@clerotri/Generic';
import {
BackButton,
Button,
Input,
Text,
} from '@clerotri/components/common/atoms';
import {LoadingScreen} from '@clerotri/components/views/LoadingScreen';
import {storage} from '@clerotri/lib/storage';
import {getInstanceURL} from '@clerotri/lib/storage/utils';
import {commonValues, ThemeContext} from '@clerotri/lib/themes';
import {useBackHandler} from '@clerotri/lib/ui';

export const LoginSettingsPage = ({callback}: {callback: () => void}) => {
const {currentTheme} = useContext(ThemeContext);

const [instanceURL, setInstanceURL] = useState(
(app.settings.get('app.instance') as string) ?? '',
);
const [instanceURL, setInstanceURL] = useState(getInstanceURL());
const [testResponse, setTestResponse] = useState(null as string | null);

const [saved, setSaved] = useState(false);
Expand Down Expand Up @@ -112,7 +111,7 @@ export const LoginSettingsPage = ({callback}: {callback: () => void}) => {
const isValid = await testURL(instanceURL, true);
if (isValid) {
console.log(`[AUTH] Setting instance URL to ${instanceURL}`);
app.settings.set('app.instance', instanceURL);
storage.set('instanceURL', instanceURL);
setSaved(true);
}
}}>
Expand Down

0 comments on commit 0a1f1d4

Please sign in to comment.