feat: Log 的数据源支持更新 (#2835)

This commit is contained in:
吴多益 2021-11-03 11:24:02 +08:00 committed by GitHub
parent 55d33d1962
commit 744c7aa60e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 3 deletions

View File

@ -82,6 +82,41 @@ public class StreamingResponseBodyController {
通过 `autoScroll` 可以关闭此功能 通过 `autoScroll` 可以关闭此功能
## source 支持变量
> 1.4.2 及以上版本
可以初始设置为空,这样初始不会加载,而等这个变量有值的时候再加载
```json
{
"type": "form",
"body": [
{
"label": "数据源",
"type": "select",
"name": "source",
"options": [
{
"label": "A",
"value": "http://localhost:3000/"
},
{
"label": "B",
"value": "http://localhost:4000/"
}
]
},
{
"type": "log",
"height": 300,
"placeholder": "请点击上面的数据源",
"source": "${source | raw}"
}
]
}
```
## 属性表 ## 属性表
| 属性名 | 类型 | 默认值 | 说明 | | 属性名 | 类型 | 默认值 | 说明 |

View File

@ -6,7 +6,7 @@ import {Renderer, RendererProps} from '../factory';
import {BaseSchema, SchemaTpl} from '../Schema'; import {BaseSchema, SchemaTpl} from '../Schema';
import Ansi from 'ansi-to-react'; import Ansi from 'ansi-to-react';
import {filter} from '../utils/tpl'; import {filter} from '../utils/tpl';
import {buildApi} from '../utils/api'; import {buildApi, isApiOutdated} from '../utils/api';
/** /**
* *
@ -14,7 +14,7 @@ import {buildApi} from '../utils/api';
*/ */
export interface LogSchema extends BaseSchema { export interface LogSchema extends BaseSchema {
/** /**
* link * log
*/ */
type: 'log'; type: 'log';
@ -100,10 +100,20 @@ export class Log extends React.Component<LogProps, LogState> {
} }
} }
componentDidUpdate() { componentDidUpdate(prevProps: LogProps) {
if (this.autoScroll && this.logRef && this.logRef.current) { if (this.autoScroll && this.logRef && this.logRef.current) {
this.logRef.current.scrollTop = this.logRef.current.scrollHeight; this.logRef.current.scrollTop = this.logRef.current.scrollHeight;
} }
if (
isApiOutdated(
prevProps.source,
this.props.source,
prevProps.data,
this.props.data
)
) {
this.loadLogs();
}
} }
// 如果向上滚动就停止自动滚动,除非滚到底部 // 如果向上滚动就停止自动滚动,除非滚到底部
@ -118,6 +128,9 @@ export class Log extends React.Component<LogProps, LogState> {
const {source, data, env, translate: __, encoding} = this.props; const {source, data, env, translate: __, encoding} = this.props;
// 因为这里返回结果是流式的,和普通 api 请求不一样,如果直接用 fetcher 经过 responseAdaptor 可能会导致出错,所以就直接 fetch 了 // 因为这里返回结果是流式的,和普通 api 请求不一样,如果直接用 fetcher 经过 responseAdaptor 可能会导致出错,所以就直接 fetch 了
const api = buildApi(source, data); const api = buildApi(source, data);
if (!api.url) {
return;
}
const res = await fetch(api.url); const res = await fetch(api.url);
if (res.status === 200) { if (res.status === 200) {
const body = res.body; const body = res.body;