Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(hooks): display experimental warning (#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Oct 12, 2021
1 parent 1d1d5cf commit 623577c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,34 @@ describe('InstantSearch', () => {

expect(searchContext!.started).toEqual(false);
});

describe('experimental warning', () => {
test('displays an experimental warning', () => {
const searchClient = createSearchClient();

expect(() => {
render(
<InstantSearch indexName="indexName" searchClient={searchClient} />
);
}).toWarnDev(
'[InstantSearch] This version is experimental and not production-ready.\n\n' +
'Please report any bugs at https://github.com/algolia/react-instantsearch/issues/new\n\n' +
'(To disable this warning, pass `suppressExperimentalWarning` to <InstantSearch />.)'
);
});

test('hides the experimental warning with suppressExperimentalWarning', () => {
const searchClient = createSearchClient();

expect(() => {
render(
<InstantSearch
indexName="indexName"
searchClient={searchClient}
suppressExperimentalWarning={true}
/>
);
}).not.toWarnDev();
});
});
});
28 changes: 25 additions & 3 deletions packages/react-instantsearch-hooks/src/useInstantSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,39 @@ import { useEffect, useMemo, version as ReactVersion } from 'react';
import { useForceUpdate } from '../useForceUpdate';

import { useStableValue } from './useStableValue';
import { warn } from './utils';
import version from './version';

import type { InstantSearchOptions } from 'instantsearch.js';

export type UseInstantSearchProps = InstantSearchOptions;

export function useInstantSearch(props: UseInstantSearchProps) {
export type UseInstantSearchProps = InstantSearchOptions & {
/**
* Removes the console warning about the experimental version.
*
* Note that this warning is only displayed in development mode.
*
* @default false
*/
suppressExperimentalWarning?: boolean;
};

export function useInstantSearch({
suppressExperimentalWarning = false,
...props
}: UseInstantSearchProps) {
const stableProps = useStableValue(props);
const search = useMemo(() => instantsearch(stableProps), [stableProps]);
const forceUpdate = useForceUpdate();

useEffect(() => {
warn(
suppressExperimentalWarning,
'This version is experimental and not production-ready.\n\n' +
'Please report any bugs at https://github.com/algolia/react-instantsearch/issues/new\n\n' +
'(To disable this warning, pass `suppressExperimentalWarning` to <InstantSearch />.)'
);
}, [suppressExperimentalWarning]);

useEffect(() => {
if (typeof stableProps.searchClient.addAlgoliaAgent === 'function') {
stableProps.searchClient.addAlgoliaAgent(`react (${ReactVersion})`);
Expand Down
14 changes: 13 additions & 1 deletion ship.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const packages = [
'packages/react-instantsearch-core',
'packages/react-instantsearch-dom-maps',
'packages/react-instantsearch-dom',
'packages/react-instantsearch-hooks',
'packages/react-instantsearch-native',
'packages/react-instantsearch',
];
Expand All @@ -17,7 +18,7 @@ module.exports = {
packagesToPublish: packages,
},
versionUpdated: ({ version, exec, dir }) => {
// write to `packages/react-instantsearch-core/src/core/version.js`
// Update version in `react-instantsearch-core`
fs.writeFileSync(
path.resolve(
dir,
Expand All @@ -29,6 +30,17 @@ module.exports = {
),
`export default '${version}';\n`
);
// Update version in `react-instantsearch-hooks`
fs.writeFileSync(
path.resolve(
dir,
'packages',
'react-instantsearch-hooks',
'src',
'version.ts'
),
`export default '${version}';\n`
);

// update version in top level package
exec(`mversion ${version}`);
Expand Down

0 comments on commit 623577c

Please sign in to comment.