Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useDashboardTimeRange hook #50

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
94 changes: 94 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Change Log

## 2.7.0 (IN PROGRESS)
## 2.7.0 (2024-07-10)

### Features

- Update to use auto-apply selectors (#49)
- Add useDashboardTimeRange hook (#50)

## 2.6.0 (2024-06-12)

Expand Down
1 change: 1 addition & 0 deletions packages/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- `useFormBuilder` allows to create declarative forms.
- `createUseDataHook` allows to create `useData` hook to get data through data source api.
- `useDashboardTimeRange` allows to use actual dashboard time range.

### Utils

Expand Down
3 changes: 2 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"dependencies": {
"@emotion/css": "^11.11.2",
"@grafana/data": "^11.1.0",
"@grafana/runtime": "^11.1.0",
"@grafana/ui": "^11.1.0",
"@volkovlabs/jest-selectors": "^1.4.1",
"classnames": "^2.5.1",
Expand Down Expand Up @@ -87,5 +88,5 @@
"typecheck": "tsc --emitDeclarationOnly false --noEmit"
},
"types": "dist/index.d.ts",
"version": "2.6.0"
"version": "2.7.0"
}
2 changes: 2 additions & 0 deletions packages/components/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default [
'@emotion/react',
'rc-tooltip',
'@grafana/data',
'@grafana/runtime',
'lodash',
'rc-slider/assets/index.css',
'@volkovlabs/jest-selectors',
Expand All @@ -44,6 +45,7 @@ export default [
'@emotion/react',
'rc-tooltip',
'@grafana/data',
'@grafana/runtime',
'lodash',
'rc-slider/assets/index.css',
'@volkovlabs/jest-selectors',
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useDashboardTimeRange';
export * from './useData';
export * from './useFormBuilder';
36 changes: 36 additions & 0 deletions packages/components/src/hooks/useDashboardTimeRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { EventBus, TimeRange } from '@grafana/data';
import { TimeRangeUpdatedEvent } from '@grafana/runtime';
import { Dispatch, SetStateAction, useEffect, useState } from 'react';

/**
* Use Dashboard Time Range
* @param timeRange
* @param eventBus
*/
export const useDashboardTimeRange = ({
initialTimeRange,
eventBus,
}: {
initialTimeRange: TimeRange;
eventBus: EventBus;
}): [TimeRange, Dispatch<SetStateAction<TimeRange>>] => {
const [value, setValue] = useState<TimeRange>(initialTimeRange);

/**
* Update on dashboard time range change
*/
useEffect(() => {
/**
* On Time Range Updated
*/
const subscriber = eventBus.getStream(TimeRangeUpdatedEvent).subscribe((event) => {
setValue(event.payload);
});

return () => {
subscriber.unsubscribe();
};
}, [eventBus]);

return [value, setValue];
};
2 changes: 1 addition & 1 deletion packages/components/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TEST_IDS } from './constants';

export * from './components';
export { createUseDataHook, useFormBuilder } from './hooks';
export { createUseDataHook, useDashboardTimeRange, useFormBuilder } from './hooks';
export * from './types';
export { CodeParameterItem, CodeParametersBuilder, FormBuilder } from './utils';

Expand Down
Loading