mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-10-24 11:35:54 +08:00
feat(core-blocks): CB-2094 data source states indication
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* CloudBeaver - Cloud Database Manager
|
||||
* Copyright (C) 2020-2022 DBeaver Corp and others
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0.
|
||||
* you may not use this file except in compliance with the License.
|
||||
*/
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { useObservableRef } from '@cloudbeaver/core-blocks';
|
||||
|
||||
import type { ISqlDataSource } from './SqlDataSource/ISqlDataSource';
|
||||
|
||||
|
||||
export function useDataSource(dataSource?: ISqlDataSource) {
|
||||
|
||||
const refObj = useObservableRef(() => ({
|
||||
async load(refresh?: boolean) {
|
||||
if (!this.dataSource || this.dataSource.isLoading()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (refresh) {
|
||||
this.dataSource.markOutdated();
|
||||
}
|
||||
|
||||
await this.dataSource.load();
|
||||
},
|
||||
}), { }, {
|
||||
dataSource,
|
||||
});
|
||||
|
||||
// TODO: getComputed skips update somehow ...
|
||||
const outdated = dataSource && (
|
||||
(dataSource.isOutdated() || !dataSource.isLoaded())
|
||||
&& !dataSource.isLoading()
|
||||
);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (!outdated) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!dataSource.exception || (Array.isArray(dataSource.exception) && !dataSource.exception.some(Boolean))) {
|
||||
refObj.load();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user