@atlaskit/global-search
Version:
A cross-product search component (batteries included)
72 lines (60 loc) • 1.77 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import { getConfluencePrefetchedData, getJiraPrefetchedData } from '../api/prefetchResults';
export const GlobalSearchPreFetchContext = /*#__PURE__*/React.createContext(undefined);
export default class PrefetchedResultsProvider extends React.Component {
constructor(...args) {
super(...args);
_defineProperty(this, "state", {
prefetchedResults: undefined
});
_defineProperty(this, "getPrefetchedResults", (cloudId, userId) => {
const {
context,
baseUrl
} = this.props;
switch (context) {
case 'confluence':
return getConfluencePrefetchedData(cloudId, baseUrl);
case 'jira':
return getJiraPrefetchedData(cloudId, userId === null, baseUrl);
default:
throw new Error(`Prefetching is not supported for context: ${context} - did you set the PrefetchResultProvider context incorrectly?`);
}
});
_defineProperty(this, "doPrefetchOnce", () => {
const {
cloudId,
userId
} = this.props;
const {
prefetchedResults
} = this.state;
if (!cloudId) {
return;
}
if (!prefetchedResults) {
this.setState({
prefetchedResults: this.getPrefetchedResults(cloudId, userId)
});
}
});
}
componentDidUpdate() {
this.doPrefetchOnce();
}
componentDidMount() {
this.doPrefetchOnce();
}
render() {
const {
children
} = this.props;
const {
prefetchedResults
} = this.state;
return /*#__PURE__*/React.createElement(GlobalSearchPreFetchContext.Provider, {
value: prefetchedResults
}, children);
}
}