-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-hooks): add insights (#382)
* feat(react-hooks): add insights note that it uses algolia/react-instantsearch#3476, so the version will need to be updated again before the docs are released This also doesn't use useInstantSearch yet, that can be done in the future * update version
- Loading branch information
Showing
11 changed files
with
9,211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# algolia-insights | ||
|
||
_This project was generated with [create-instantsearch-app](https://github.com/algolia/create-instantsearch-app) by [Algolia](https://algolia.com)._ | ||
|
||
## Get started | ||
|
||
To run this project locally, install the dependencies and run the local server: | ||
|
||
```sh | ||
npm install | ||
npm start | ||
``` | ||
|
||
Alternatively, you may use [Yarn](https://http://yarnpkg.com/): | ||
|
||
```sh | ||
yarn | ||
yarn start | ||
``` | ||
|
||
Open http://localhost:3000 to see your app. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "algolia-insights", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"algoliasearch": "^4.13.0", | ||
"instantsearch.css": "^7.4.5", | ||
"instantsearch.js": "^4.40.5", | ||
"react": "^18.1.0", | ||
"react-dom": "^18.1.0", | ||
"react-instantsearch-hooks-web": "^6.26.0", | ||
"react-scripts": "5.0.1" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"react-app", | ||
"react-app/jest" | ||
] | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions
40
React InstantSearch Hooks/algolia-insights/public/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<meta name="theme-color" content="#000000"> | ||
|
||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.png"> | ||
|
||
<!-- | ||
Do not use @7 in production, use a complete version like x.x.x, see website for latest version: | ||
https://community.algolia.com/react-instantsearch/Getting_started.html#load-the-algolia-theme | ||
--> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@7/themes/algolia-min.css"> | ||
|
||
<title>algolia-insights</title> | ||
<script> | ||
var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/[email protected]"; | ||
!function(e,a,t,n,s,i,c){e.AlgoliaAnalyticsObject=s,e[s]=e[s]||function(){ | ||
(e[s].queue=e[s].queue||[]).push(arguments)},i=a.createElement(t),c=a.getElementsByTagName(t)[0], | ||
i.async=1,i.src=n,c.parentNode.insertBefore(i,c) | ||
}(window,document,"script",ALGOLIA_INSIGHTS_SRC,"aa"); | ||
|
||
aa('init', { | ||
appId: 'B1G2GM9NG0', | ||
apiKey: 'aadef574be1f9252bb48d4ea09b5cfe5', | ||
}); | ||
</script> | ||
</head> | ||
|
||
<body> | ||
<noscript> | ||
You need to enable JavaScript to run this app. | ||
</noscript> | ||
|
||
<div id="root"></div> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
em { | ||
background: cyan; | ||
font-style: normal; | ||
} | ||
|
||
.header { | ||
display: flex; | ||
align-items: center; | ||
min-height: 50px; | ||
padding: 0.5rem 1rem; | ||
background-image: linear-gradient(to right, #8e43e7, #00aeff); | ||
color: #fff; | ||
margin-bottom: 1rem; | ||
} | ||
|
||
.header a { | ||
color: #fff; | ||
text-decoration: none; | ||
} | ||
|
||
.header-title { | ||
font-size: 1.2rem; | ||
font-weight: normal; | ||
} | ||
|
||
.header-title::after { | ||
content: ' ▸ '; | ||
padding: 0 0.5rem; | ||
} | ||
|
||
.header-subtitle { | ||
font-size: 1.2rem; | ||
} | ||
|
||
.container { | ||
max-width: 1200px; | ||
margin: 0 auto; | ||
padding: 1rem; | ||
} | ||
|
||
.search-panel { | ||
display: flex; | ||
} | ||
|
||
.search-panel__filters { | ||
flex: 1; | ||
} | ||
|
||
.search-panel__results { | ||
flex: 3; | ||
} | ||
|
||
.searchbox { | ||
margin-bottom: 2rem; | ||
} | ||
|
||
.pagination { | ||
margin: 2rem auto; | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
Highlight, | ||
Hits, | ||
InstantSearch, | ||
Pagination, | ||
RefinementList, | ||
SearchBox, | ||
} from 'react-instantsearch-hooks-web'; | ||
import algoliasearch from 'algoliasearch/lite'; | ||
|
||
import './App.css'; | ||
import { Insights } from './Insights'; | ||
|
||
const searchClient = algoliasearch( | ||
'B1G2GM9NG0', | ||
'aadef574be1f9252bb48d4ea09b5cfe5' | ||
); | ||
|
||
function App() { | ||
return ( | ||
<div className="container"> | ||
<InstantSearch searchClient={searchClient} indexName="demo_ecommerce"> | ||
<Insights /> | ||
<div className="search-panel"> | ||
<div className="search-panel__filters"> | ||
<RefinementList attribute="brand" /> | ||
</div> | ||
|
||
<div className="search-panel__results"> | ||
<SearchBox className="searchbox" placeholder="Search" /> | ||
<Hits hitComponent={Hit} /> | ||
|
||
<div className="pagination"> | ||
<Pagination /> | ||
</div> | ||
</div> | ||
</div> | ||
</InstantSearch> | ||
</div> | ||
); | ||
} | ||
|
||
function Hit({ hit, sendEvent }) { | ||
return ( | ||
<div> | ||
<Highlight attribute="name" hit={hit} /> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
sendEvent('click', hit, 'Product Added'); | ||
}} | ||
> | ||
Add to cart | ||
</button> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
sendEvent('conversion', hit, 'Product Ordered'); | ||
}} | ||
> | ||
Order | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
55 changes: 55 additions & 0 deletions
55
React InstantSearch Hooks/algolia-insights/src/Insights.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { createInsightsMiddleware } from 'instantsearch.js/es/middlewares'; | ||
import { useEffect } from 'react'; | ||
import { useConnector } from 'react-instantsearch-hooks-web'; | ||
|
||
const connectMiddleware = (renderFn, unmountFn) => (widgetParams) => ({ | ||
init(initOptions) { | ||
renderFn( | ||
{ | ||
...this.getWidgetRenderState(initOptions), | ||
instantSearchInstance: initOptions.instantSearchInstance, | ||
}, | ||
true | ||
); | ||
}, | ||
|
||
render(renderOptions) { | ||
const renderState = this.getWidgetRenderState(renderOptions); | ||
|
||
renderFn( | ||
{ | ||
...renderState, | ||
instantSearchInstance: renderOptions.instantSearchInstance, | ||
}, | ||
false | ||
); | ||
}, | ||
|
||
getWidgetRenderState(renderOptions) { | ||
return { | ||
use: (...args) => renderOptions.instantSearchInstance.use(...args), | ||
unuse: (...args) => renderOptions.instantSearchInstance.unuse(...args), | ||
widgetParams, | ||
}; | ||
}, | ||
|
||
dispose() { | ||
unmountFn(); | ||
}, | ||
}); | ||
|
||
export function Insights() { | ||
const { use, unuse } = useConnector(connectMiddleware); | ||
|
||
useEffect(() => { | ||
const middleware = createInsightsMiddleware({ | ||
insightsClient: window.aa, | ||
}); | ||
|
||
use(middleware); | ||
|
||
return () => unuse(middleware); | ||
}, [use, unuse]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
body, | ||
h1 { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
body { | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, | ||
Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import './index.css'; | ||
import App from './App'; | ||
|
||
render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
); |
Oops, something went wrong.