Skip to content

Commit 777261c

Browse files
HaroenvCompuIves
authored andcommitted
chore(homepage): make sure sandbox count is updated (codesandbox#1173)
* chore(home): remove algolia instantiation not used, so not necessary :) * chore(homepage): make sure sandbox count is updatedDid a change in the (otherwise unused) search function. It was "promisifying", but the default client & index.search are already returning promisesAlso changed to make sure the lite version for `algoliasearch` is used
1 parent 0f9605c commit 777261c

File tree

4 files changed

+79
-38
lines changed

4 files changed

+79
-38
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { Fragment, Component } from 'react';
2+
import { search } from '../../../utils/algolia';
3+
4+
class SandboxCount extends Component {
5+
state = {
6+
sandboxes: undefined,
7+
};
8+
static defaultProps = {
9+
fallback: 800000,
10+
};
11+
12+
componentDidMount() {
13+
search({
14+
attributesToRetrieve: [],
15+
attributesToHighlight: [],
16+
hitsPerPage: 1,
17+
}).then(({ nbHits }) =>
18+
this.setState({
19+
sandboxes: nbHits,
20+
})
21+
);
22+
}
23+
24+
render() {
25+
const { fallback } = this.props;
26+
const { sandboxes } = this.state;
27+
const number = sandboxes || fallback;
28+
return <Fragment>{number.toLocaleString('en-US')}</Fragment>;
29+
}
30+
}
31+
32+
export default SandboxCount;

packages/homepage/src/screens/home/NPMFeature/SearchInput/index.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import React from 'react';
22
import styled from 'styled-components';
33

4-
import algoliasearch from 'algoliasearch';
5-
import {
6-
ALGOLIA_API_KEY,
7-
ALGOLIA_APPLICATION_ID,
8-
ALGOLIA_DEFAULT_INDEX,
9-
} from 'common/utils/config';
10-
114
import Input from './Input';
125
import Hit from './Hit';
136

@@ -31,27 +24,18 @@ export default class SearchInput extends React.PureComponent {
3124
};
3225

3326
componentDidMount() {
34-
this.client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY);
35-
this.index = this.client.initIndex(ALGOLIA_DEFAULT_INDEX);
36-
3727
this.searchQuery('');
3828
}
3929

4030
searchQuery = (query: string) => {
41-
searchFacets('npm_dependencies.dependency', query).then(res => {
42-
const { facetHits } = res;
43-
44-
facetHits.sort((a, b) => {
45-
if (a.value === query) {
46-
return -1;
47-
} else if (b.value === query) {
48-
return 1;
49-
}
50-
51-
return 0;
31+
searchFacets({
32+
facet: 'npm_dependencies.dependency',
33+
query,
34+
hitsPerPage: 3,
35+
}).then(({ facetHits }) => {
36+
this.setState({
37+
hits: facetHits,
5238
});
53-
54-
this.setState({ hits: facetHits.slice(0, 3) });
5539
});
5640
};
5741

@@ -63,7 +47,9 @@ export default class SearchInput extends React.PureComponent {
6347
<div>Dependency</div>
6448
<div>Sandbox Count</div>
6549
</Legend>
66-
{this.state.hits.map((hit, i) => <Hit key={i} hit={hit} />)}
50+
{this.state.hits.map((hit, i) => (
51+
<Hit key={i} hit={hit} />
52+
))}
6753
<a
6854
href="https://www.algolia.com/?utm_source=algoliaclient&utm_medium=website&utm_content=codesandbox.io&utm_campaign=poweredby"
6955
target="_blank"

packages/homepage/src/screens/home/NPMFeature/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import SearchIcon from 'react-icons/lib/md/search';
1010
import NPMIcon from './NPMIcon';
1111
import SearchInput from './SearchInput';
1212
import EmbedAnimation from './EmbedAnimation';
13+
import SandboxCount from './SandboxCount';
1314
import EmbedIcon from './EmbedIcon';
1415

1516
import media from '../../../utils/media';
@@ -146,7 +147,7 @@ export default () => (
146147
Want to know how a library works? You can easily browse through
147148
the{' '}
148149
<Link href="https://codesandbox.io/search" target="_blank">
149-
80,000+ created sandboxes
150+
<SandboxCount />+ created sandboxes
150151
</Link>{' '}
151152
on CodeSandbox. We want this to be a platform where everyone can
152153
easily learn and share.
Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import algoliasearch from 'algoliasearch';
1+
import algoliasearch from 'algoliasearch/lite';
22
import {
33
ALGOLIA_API_KEY,
44
ALGOLIA_APPLICATION_ID,
@@ -8,19 +8,41 @@ import {
88
const client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY);
99
const index = client.initIndex(ALGOLIA_DEFAULT_INDEX);
1010

11-
export function searchFacets(facet, query) {
12-
return index.searchForFacetValues({ facetName: facet, facetQuery: query });
11+
export function searchFacets({
12+
facet,
13+
query,
14+
hitsPerPage,
15+
}: {
16+
facet: string,
17+
query: string,
18+
hitsPerPage?: number,
19+
}) {
20+
return index.searchForFacetValues({
21+
facetName: facet,
22+
facetQuery: query,
23+
maxFacetHits: hitsPerPage,
24+
typoTolerance: 'strict',
25+
});
1326
}
1427

15-
export function search(query: string, filter: Object) {
16-
return new Promise((resolve, reject) => {
17-
index.search({ facetFilters: filter, query }, (err, res) => {
18-
if (err) {
19-
reject(err);
20-
return;
21-
}
22-
23-
resolve(res);
24-
});
28+
export function search({
29+
query,
30+
attributesToRetrieve,
31+
attributesToHighlight,
32+
hitsPerPage,
33+
searchParameters,
34+
}: {
35+
query?: string,
36+
attributesToRetrieve?: string[],
37+
attributesToHighlight?: string[],
38+
hitsPerPage?: number,
39+
searchParameters: Object,
40+
}) {
41+
return index.search({
42+
query,
43+
attributesToRetrieve,
44+
attributesToHighlight,
45+
hitsPerPage,
46+
...searchParameters,
2547
});
2648
}

0 commit comments

Comments
 (0)