Skip to content

Commit

Permalink
docs-comments: add debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
8coon committed Dec 1, 2022
1 parent ce58074 commit 473a1af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
35 changes: 28 additions & 7 deletions docs/src/pages/Tokens.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {useAdaptivity} from '@vkontakte/vkui';
import React, {FC, useState} from 'react';
import React, {FC, useMemo, useState} from 'react';

import {
TokensActions,
TokensContent,
TokensHeader,
} from '@/components/pages/Tokens';
import tokensData from '@/public/static/data/tokensData.json';
import {useDebounce} from '@/shared/hooks/useDebounce';
import {ChipOption, Tokens as TokensType, ValueType} from '@/shared/types';

const themes = Object.keys(tokensData);
Expand All @@ -29,18 +30,34 @@ function transformTags(tokens: TokensType) {
.filter((key, index, arr) => arr.indexOf(key) === index);
}

function findThemeTags(themeNames: string[]): string[] {
const tags: Record<string, boolean> = {};

for (const themeName of themeNames) {
const themeTags = transformTags(tokensData[themeName]);

for (const themeTag of themeTags) {
tags[themeTag] = true;
}
}

const tagsList = Object.keys(tags);
tagsList.sort();

return tagsList;
}

const Tokens: FC = () => {
const {viewWidth} = useAdaptivity();
const isTablet = viewWidth > 3;

const [themeTags] = useState<Array<string>>(
transformTags(tokensData[themes[0]]),
);
const themeTags = useMemo(() => findThemeTags(themes), [themes]);
const [selectedTags, setSelectedTags] = useState<Array<ChipOption>>([]);
const [selectedTheme, setSelectedTheme] = useState<string>(themes[0]);
const [selectedValueType, setSelectedValueType] =
useState<ValueType>('regular');
const [searchValue, setSearchValue] = useState('');
const searchValueDebounced = useDebounce(searchValue, 500);

const searchChangeHandler = (
event: React.ChangeEvent<HTMLInputElement>,
Expand Down Expand Up @@ -82,11 +99,15 @@ const Tokens: FC = () => {
/>
<TokensContent
tokens={tokensData[selectedTheme]}
selectedTags={selectedTags.map((tagOption) =>
String(tagOption.value),
selectedTags={React.useMemo(
() =>
selectedTags.map((tagOption) =>
String(tagOption.value),
),
[selectedTags.join(' ')],
)}
selectedValueType={selectedValueType}
searchValue={searchValue}
searchValue={searchValueDebounced}
/>
</div>
);
Expand Down
17 changes: 17 additions & 0 deletions docs/src/shared/hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

export function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = React.useState(value);

React.useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value]);

return debouncedValue;
}

0 comments on commit 473a1af

Please sign in to comment.