Skip to content

Commit

Permalink
feat: add Faceting & Pagination components.
Browse files Browse the repository at this point in the history
  • Loading branch information
riccox committed Mar 11, 2023
1 parent ca775f2 commit e7b9f6e
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 8 deletions.
69 changes: 69 additions & 0 deletions src/components/Settings/config/detail/faceting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useMutation, useQuery } from '@tanstack/react-query';
import clsx from 'clsx';
import { Faceting as TFaceting } from 'meilisearch';
import { FC, useEffect, useMemo } from 'react';
import { IndexSettingConfigComponentProps } from '../..';
import _ from 'lodash';

export const Faceting: FC<IndexSettingConfigComponentProps> = ({ client, className, host, toggleLoading }) => {
const query = useQuery({
queryKey: ['getFaceting', host, client.uid],
refetchInterval: 4500,
async queryFn(ctx) {
return (await client.getFaceting()) as TFaceting;
},
});

const mutation = useMutation({
mutationKey: ['updateFaceting', host, client.uid],
async mutationFn(variables: TFaceting) {
console.debug('🚀 ~ file: faceting.tsx:19 ~ mutationFn ~ variables:', variables);
if (_.isEmpty(variables)) {
// empty to reset
return await client.resetFaceting();
}
return await client.updateFaceting(variables);
},
});

useEffect(() => {
const isLoading = query.isLoading || query.isFetching || mutation.isLoading;
toggleLoading(isLoading);
}, [mutation.isLoading, query.isFetching, query.isLoading, toggleLoading]);

return useMemo(
() => (
<div className={clsx(className)}>
<h2 className="font-semibold">Faceting</h2>
<span className="text-sm flex flex-wrap gap-2">
<p>
With Meilisearch, you can create faceted search interfaces. This setting allows you to define the maximum
number of values returned by the facets search parameter.
</p>
<a
className="link info text-info-800"
href="https://docs.meilisearch.com/learn/advanced/filtering_and_faceted_search.html"
target={'_blank'}
rel="noreferrer"
>
Learn more
</a>
</span>

<h3>Max values per facet</h3>
<input
defaultValue={query.data?.maxValuesPerFacet || 0}
className="input outline primary"
type="number"
onChange={(ev) => {
mutation.mutate({
...query.data,
maxValuesPerFacet: parseInt(ev.currentTarget.value),
});
}}
/>
</div>
),
[className, mutation, query.data]
);
};
74 changes: 74 additions & 0 deletions src/components/Settings/config/detail/pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useMutation, useQuery } from '@tanstack/react-query';
import clsx from 'clsx';
import { PaginationSettings as TPagination } from 'meilisearch';
import { FC, useEffect, useMemo } from 'react';
import { IndexSettingConfigComponentProps } from '../..';
import _ from 'lodash';

export const Pagination: FC<IndexSettingConfigComponentProps> = ({ client, className, host, toggleLoading }) => {
const query = useQuery({
queryKey: ['getPagination', host, client.uid],
refetchInterval: 4500,
async queryFn(ctx) {
return (await client.getPagination()) as TPagination;
},
});

const mutation = useMutation({
mutationKey: ['updatePagination', host, client.uid],
async mutationFn(variables: TPagination) {
console.debug('🚀 ~ file: pagination.tsx:19 ~ mutationFn ~ variables:', variables);
if (_.isEmpty(variables)) {
// empty to reset
return await client.resetPagination();
}
return await client.updatePagination(variables);
},
});

useEffect(() => {
const isLoading = query.isLoading || query.isFetching || mutation.isLoading;
toggleLoading(isLoading);
}, [mutation.isLoading, query.isFetching, query.isLoading, toggleLoading]);

return useMemo(
() => (
<div className={clsx(className)}>
<h2 className="font-semibold">Pagination</h2>
<span className="text-sm flex flex-wrap gap-2">
<p>
To protect your database from malicious scraping, Meilisearch has a default limit of 1000 results per
search. This setting allows you to configure the maximum number of results returned per search.
<br />
'maxTotalHits' takes priority over search parameters such as limit, offset, hitsPerPage, and page.
<br />
For example, if you set maxTotalHits to 100, you will not be able to access search results beyond 100 no
matter the value configured for offset.
</p>
<a
className="link info text-info-800"
href="https://docs.meilisearch.com/learn/advanced/pagination.html"
target={'_blank'}
rel="noreferrer"
>
Learn more
</a>
</span>

<h3>Max total hits</h3>
<input
defaultValue={query.data?.maxTotalHits || 0}
className="input outline primary"
type="number"
onChange={(ev) => {
mutation.mutate({
...query.data,
maxTotalHits: parseInt(ev.currentTarget.value),
});
}}
/>
</div>
),
[className, mutation, query.data]
);
};
26 changes: 18 additions & 8 deletions src/components/Settings/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { RankingRules } from './detail/rankingRules';
import { StopWords } from './detail/stopWords';
import { Synonyms } from './detail/synonyms';
import { TypoTolerance } from './detail/typoTolerance';
import { Faceting } from './detail/faceting';
import { Pagination } from './detail/pagination';

const tabs = [
'Filterable Attributes',
Expand Down Expand Up @@ -68,41 +70,49 @@ export const IndexConfiguration: FC<IndexSettingComponentProps> = ({ host, clien
</div>
<div className="flex-1 flex">
<FilterableAttributes
className={clsx(selectTab !== 0 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 0 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<DistinctAttribute
className={clsx(selectTab !== 1 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 1 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<SortableAttributes
className={clsx(selectTab !== 2 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 2 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<SearchableAttributes
className={clsx(selectTab !== 3 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 3 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<DisplayedAttributes
className={clsx(selectTab !== 4 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 4 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<RankingRules
className={clsx(selectTab !== 5 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 5 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<StopWords
className={clsx(selectTab !== 6 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 6 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<Synonyms
className={clsx(selectTab !== 7 && 'hidden', 'flex-1 flex flex-col gap-2 p-2')}
className={clsx(selectTab !== 7 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<TypoTolerance
className={clsx(selectTab !== 8 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<Faceting
className={clsx(selectTab !== 9 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
<Pagination
className={clsx(selectTab !== 10 && 'hidden', 'flex-1 flex flex-col gap-2 p-2 overflow-scroll')}
{...{ client, host, toggleLoading }}
/>
</div>
</div>
<Editor className={clsx(inputType !== 'json' && 'hidden')} {...{ host, client, toggleLoading }} />
Expand Down

0 comments on commit e7b9f6e

Please sign in to comment.