Skip to content

Commit

Permalink
[8.13] [Cases] Use the default value of observables on the `useA…
Browse files Browse the repository at this point in the history
…pplication` hook (#177980) (#177996)

# Backport

This will backport the following commits from `main` to `8.13`:
- [[Cases] Use the default value of observables on the
`useApplication` hook
(#177980)](#177980)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Christos
Nasikas","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-03-05T11:28:05Z","message":"[Cases]
Use the default value of observables on the `useApplication` hook
(#177980)\n\n## Summary\r\n\r\nThis PR uses the default values of the
`currentAppId# Backport

This will backport the following commits from `main` to `8.13`:
- [[Cases] Use the default value of observables on the
&#x60;useApplication&#x60; hook
(#177980)](#177980)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT  and\r\n`applications# Backport

This will backport the following commits from `main` to `8.13`:
- [[Cases] Use the default value of observables on the
&#x60;useApplication&#x60; hook
(#177980)](#177980)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT if they exist.\r\n\r\n### For maintainers\r\n\r\n- [x] This
was checked for breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"6dd9a88f013dd369c5b7e88ac20d661e8bdbcf18","branchLabelMapping":{"^v8.14.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","Feature:Cases","v8.13.0","v8.14.0"],"title":"[Cases]
Use the default value of observables on the `useApplication`
hook","number":177980,"url":"https://github.com/elastic/kibana/pull/177980","mergeCommit":{"message":"[Cases]
Use the default value of observables on the `useApplication` hook
(#177980)\n\n## Summary\r\n\r\nThis PR uses the default values of the
`currentAppId# Backport

This will backport the following commits from `main` to `8.13`:
- [[Cases] Use the default value of observables on the
&#x60;useApplication&#x60; hook
(#177980)](#177980)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT  and\r\n`applications# Backport

This will backport the following commits from `main` to `8.13`:
- [[Cases] Use the default value of observables on the
&#x60;useApplication&#x60; hook
(#177980)](#177980)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT if they exist.\r\n\r\n### For maintainers\r\n\r\n- [x] This
was checked for breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"6dd9a88f013dd369c5b7e88ac20d661e8bdbcf18"}},"sourceBranch":"main","suggestedTargetBranches":["8.13"],"targetPullRequestStates":[{"branch":"8.13","label":"v8.13.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.14.0","branchLabelMappingKey":"^v8.14.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/177980","number":177980,"mergeCommit":{"message":"[Cases]
Use the default value of observables on the `useApplication` hook
(#177980)\n\n## Summary\r\n\r\nThis PR uses the default values of the
`currentAppId# Backport

This will backport the following commits from `main` to `8.13`:
- [[Cases] Use the default value of observables on the
&#x60;useApplication&#x60; hook
(#177980)](#177980)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT  and\r\n`applications# Backport

This will backport the following commits from `main` to `8.13`:
- [[Cases] Use the default value of observables on the
&#x60;useApplication&#x60; hook
(#177980)](#177980)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT if they exist.\r\n\r\n### For maintainers\r\n\r\n- [x] This
was checked for breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"6dd9a88f013dd369c5b7e88ac20d661e8bdbcf18"}}]}]
BACKPORT-->

Co-authored-by: Christos Nasikas <[email protected]>
  • Loading branch information
kibanamachine and cnasikas authored Mar 5, 2024
1 parent 2dec016 commit 5d0a60e
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,36 @@
* 2.0.
*/
import useObservable from 'react-use/lib/useObservable';
import type { BehaviorSubject, Observable } from 'rxjs';
import { useKibana } from '../../common/lib/kibana';

interface UseApplicationReturn {
appId: string | undefined;
appTitle: string | undefined;
}

const isBehaviorSubjectObservable = <T,>(
observable: Observable<T>
): observable is BehaviorSubject<T> => 'value' in observable;

/**
* Checks if the observable is stateful and, in case, sets up `useObservable` with an initial value
*/
const useStatefulObservable = <T,>(observable: Observable<T>) => {
let initialValue: T | undefined;

if (isBehaviorSubjectObservable(observable)) {
initialValue = observable.value;
}

return useObservable(observable, initialValue);
};

export const useApplication = (): UseApplicationReturn => {
const { currentAppId$, applications$ } = useKibana().services.application;
// retrieve the most recent value from the BehaviorSubject
const appId = useObservable(currentAppId$);
const applications = useObservable(applications$);
const appId = useStatefulObservable(currentAppId$);
const applications = useStatefulObservable(applications$);

const appTitle = appId
? applications?.get(appId)?.category?.label ?? applications?.get(appId)?.title
Expand Down

0 comments on commit 5d0a60e

Please sign in to comment.