Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 0 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

# misc
**/.DS_Store
**/.env.local
**/.env.development.local
**/.env.test.local
**/.env.production.local

**/npm-debug.log*
**/yarn-debug.log*
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ yarn-error.log*
.vscode/*
.idea
stats.html
/public/moeflow-runtime-config.json
6 changes: 6 additions & 0 deletions public/moeflow-runtime-config.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"comment": "Runtime configuration overrides. See src/configs.tsx",
"moeflowCompanion": {
"gradioUrl": "http://localhost:7860"
}
}
2 changes: 1 addition & 1 deletion src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import axios, {
import qs from 'qs';
import { createElement } from 'react';
import { Icon } from '../components';
import { configs, runtimeConfig } from '@/configs';
import { runtimeConfig } from '@/configs';
import { createDebugLogger } from '@/utils/debug-logger';
import { getIntl } from '@/locales';
import store from '../store';
Expand Down
8 changes: 6 additions & 2 deletions src/configs.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { lazyThenable } from '@jokester/ts-commonutil/lib/concurrency/lazy-thenable';

interface RuntimeConfig {
export interface RuntimeConfig {
// base URL for API requests
baseURL: string;

moeflowCompanion?: {
gradioUrl?: string;
};
}

/**
Expand All @@ -16,7 +20,7 @@ export const runtimeConfig = lazyThenable<RuntimeConfig>(async () => {
const overriden: RuntimeConfig = await fetch('/moeflow-runtime-config.json')
.then((res) => res.json())
.catch(() => null);
const merged = {
const merged: RuntimeConfig = {
...{
// defaults
baseURL: process.env.REACT_APP_BASE_URL || '/api/',
Expand Down
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import App from './App';
import './fontAwesome'; // Font Awesome
import './index.css';
import store from './store';
import { setOSName, setPlatform } from './store/site/slice';
import { setOSName, setPlatform, setRuntimeConfig } from './store/site/slice';
import { setUserToken } from './store/user/slice';
import { getToken } from './utils/cookie';
import { OSName, Platform } from './interfaces';
import { runtimeConfig } from './configs';
import {
getDefaultHotKey,
hotKeyInitialState,
HotKeyState,
setHotKey,
} from './store/hotKey/slice';
import { loadHotKey } from './utils/storage';
import { createDebugLogger } from './utils/debug-logger';
const debugLogger = createDebugLogger('app');

// 时间插件
if (false && process.env.NODE_ENV === 'development') {
Expand All @@ -37,6 +40,7 @@ const platform = browser.getPlatformType() as Platform;
const osName = browser.getOSName(true) as OSName;
store.dispatch(setPlatform(platform));
store.dispatch(setOSName(osName));
store.dispatch(setRuntimeConfig(await runtimeConfig));
// 恢复自定义快捷键
for (const hotKeyName in hotKeyInitialState) {
const name = hotKeyName as keyof HotKeyState;
Expand All @@ -57,6 +61,7 @@ for (const hotKeyName in hotKeyInitialState) {
async function mountApp() {
const { intlMessages, locale, antdLocale, antdValidateMessages } =
await initI18n;
debugLogger('initial state', store.getState());
/**
* Set user token from cookie
*/
Expand Down
2 changes: 1 addition & 1 deletion src/store/project/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function* setCurrentProjectWorker(
configs: { cancelToken },
});
yield put(setCurrentProject(toLowerCamelCase(result.data)));
} catch (error) {
} catch (error: any) {
error.default();
} finally {
Comment thread
jokester marked this conversation as resolved.
if (yield cancelled()) {
Expand Down
4 changes: 2 additions & 2 deletions src/store/projectSet/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function* setCurrentProjectSetWorker(
) {
// 清空当前 projectSet
yield put(clearCurrentProjectSet());
const projectSets = yield select(
const projectSets: UserProjectSet[] = yield select(
(state: AppState) => state.projectSet.projectSets,
);
const projectSet = projectSets.find(
Expand All @@ -35,7 +35,7 @@ function* setCurrentProjectSetWorker(
configs: { cancelToken },
});
yield put(setCurrentProjectSet(toLowerCamelCase(result.data)));
} catch (error) {
} catch (error: any) {
error.default();
Comment on lines +38 to 39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Don’t widen to any; guard error.default() to prevent crash paths

Calling default on unknown errors can throw. Use unknown and guard.

-    } catch (error: any) {
-      error.default();
+    } catch (error: unknown) {
+      if (typeof (error as any)?.default === 'function') {
+        (error as any).default();
+      } else {
+        // TODO: route to centralized error handler
+        console.error(error);
+      }
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (error: any) {
error.default();
} catch (error: unknown) {
if (typeof (error as any)?.default === 'function') {
(error as any).default();
} else {
// TODO: route to centralized error handler
console.error(error);
}
}
🤖 Prompt for AI Agents
In src/store/projectSet/sagas.ts around lines 38 to 39, the catch currently
types the error as any and calls error.default() unguarded which can throw;
change the catch to use error: unknown (or keep unknown-compatible typing) and
before invoking default verify it exists and is callable (e.g., check that error
is non-null and that (error as any).default is a function) and only then call
it; otherwise handle the fallback (log or ignore) to avoid crash paths.

} finally {
if (yield cancelled()) {
Expand Down
17 changes: 12 additions & 5 deletions src/store/site/slice.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { OSName, Platform } from '../../interfaces';
import { OSName, Platform } from '@/interfaces';
import { RuntimeConfig } from '@/configs';

export interface SiteState {
osName?: OSName;
platform?: Platform;
osName: OSName;
platform: Platform;
newInvitationsCount: number;
relatedApplicationsCount: number;
runtimeConfig: RuntimeConfig;
}

const initialState: SiteState = {
osName: 'windows',
platform: 'desktop',
osName: null!,
platform: null!,
relatedApplicationsCount: 0,
newInvitationsCount: 0,
runtimeConfig: null!,
};
const slice = createSlice({
name: 'site',
Expand All @@ -30,6 +33,9 @@ const slice = createSlice({
setNewInvitationsCount(state, action: PayloadAction<number>) {
state.newInvitationsCount = action.payload;
},
setRuntimeConfig(state, action: PayloadAction<RuntimeConfig>) {
state.runtimeConfig = action.payload;
},
},
});

Expand All @@ -38,5 +44,6 @@ export const {
setOSName,
setRelatedApplicationsCount,
setNewInvitationsCount,
setRuntimeConfig,
} = slice.actions;
export default slice.reducer;
1 change: 1 addition & 0 deletions src/store/user/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Axios } from 'axios';
function* getUserInfoAsync(action: ReturnType<typeof setUserToken>) {
const token = action.payload.token;
const instance: Axios = yield api.getAxiosInstance();
// console.debug('instance', instance);
if (token === '') {
// 清除 Axios Authorization 头
delete instance.defaults.headers.common['Authorization'];
Expand Down
Loading