Skip to content

Commit

Permalink
chore: Removes FilterByText internal state (RocketChat#33735)
Browse files Browse the repository at this point in the history
Co-authored-by: Tasso <[email protected]>
  • Loading branch information
dougfabris and tassoevan authored Oct 24, 2024
1 parent b0f721e commit bec7cf6
Show file tree
Hide file tree
Showing 29 changed files with 220 additions and 188 deletions.
21 changes: 7 additions & 14 deletions apps/meteor/client/components/FilterByText.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import { Box, Icon, TextInput, Margins } from '@rocket.chat/fuselage';
import { useAutoFocus, useMergedRefs } from '@rocket.chat/fuselage-hooks';
import type { ChangeEvent, FormEvent, HTMLAttributes } from 'react';
import React, { forwardRef, memo, useCallback, useState } from 'react';
import type { ChangeEvent, ComponentPropsWithoutRef, FormEvent } from 'react';
import React, { forwardRef, memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';

type FilterByTextProps = {
onChange: (filter: string) => void;
// TODO: consider changing the type of TextInput's `onChange` to (event: ChangeEvent<HTMLInputElement>) => void
type FilterByTextProps = Omit<ComponentPropsWithoutRef<typeof TextInput>, 'onChange'> & {
shouldAutoFocus?: boolean;
} & Omit<HTMLAttributes<HTMLInputElement>, 'is' | 'onChange'>;
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
};

const FilterByText = forwardRef<HTMLInputElement, FilterByTextProps>(function FilterByText(
{ placeholder, onChange: setFilter, shouldAutoFocus = false, children, ...props },
{ placeholder, shouldAutoFocus = false, children, ...props },
ref,
) {
const { t } = useTranslation();
const [text, setText] = useState('');
const autoFocusRef = useAutoFocus(shouldAutoFocus);
const mergedRefs = useMergedRefs(ref, autoFocusRef);

const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
setText(event.currentTarget.value);
setFilter(event.currentTarget.value);
};

const handleFormSubmit = useCallback((event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
}, []);
Expand All @@ -35,8 +30,6 @@ const FilterByText = forwardRef<HTMLInputElement, FilterByTextProps>(function Fi
placeholder={placeholder ?? t('Search')}
ref={mergedRefs}
addon={<Icon name='magnifier' size='x20' />}
onChange={handleInputChange}
value={text}
flexGrow={2}
minWidth='x220'
aria-label={placeholder ?? t('Search')}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Pagination, States, StatesIcon, StatesActions, StatesAction, StatesTitle } from '@rocket.chat/fuselage';
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks';
import { useTranslation, useEndpoint } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import React, { useMemo, useState } from 'react';
Expand All @@ -21,13 +22,16 @@ const BusinessHoursTable = () => {

const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();

const query = useMemo(
() => ({
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
name: text,
}),
[itemsPerPage, current, text],
const query = useDebouncedValue(
useMemo(
() => ({
name: text,
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[text, itemsPerPage, current],
),
500,
);

const getBusinessHours = useEndpoint('GET', '/v1/livechat/business-hours');
Expand All @@ -47,7 +51,7 @@ const BusinessHoursTable = () => {

return (
<>
<FilterByText onChange={setText} />
<FilterByText value={text} onChange={(event) => setText(event.target.value)} />
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
30 changes: 16 additions & 14 deletions apps/meteor/client/omnichannel/monitors/MonitorsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const MonitorsTable = () => {

const [text, setText] = useState('');
const [username, setUsername] = useState('');
const debouncedText = useDebouncedValue(text, 500);

const dispatchToastMessage = useToastMessageDispatch();

Expand All @@ -56,19 +55,20 @@ const MonitorsTable = () => {
const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = pagination;
const { sortBy, sortDirection, setSort } = sort;

const query = useMemo(
() => ({
text: debouncedText,
sort: `{ "${sortBy}": ${sortDirection === 'asc' ? 1 : -1} }`,
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[debouncedText, itemsPerPage, current, sortBy, sortDirection],
const query = useDebouncedValue(
useMemo(
() => ({
text,
sort: `{ "${sortBy}": ${sortDirection === 'asc' ? 1 : -1} }`,
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[text, itemsPerPage, current, sortBy, sortDirection],
),
500,
);

const { data, refetch, isLoading, isSuccess, isError } = useQuery(['omnichannel', 'monitors', debouncedText, pagination, sort], () =>
getMonitors(query),
);
const { data, refetch, isLoading, isSuccess, isError } = useQuery(['omnichannel', 'monitors', query], () => getMonitors(query));

const [defaultQuery] = useState(hashQueryKey([query]));
const queryHasChanged = defaultQuery !== hashQueryKey([query]);
Expand Down Expand Up @@ -144,7 +144,9 @@ const MonitorsTable = () => {
</FieldRow>
</Field>
</Box>
{((isSuccess && data?.monitors.length > 0) || queryHasChanged) && <FilterByText onChange={setText} />}
{((isSuccess && data?.monitors.length > 0) || queryHasChanged) && (
<FilterByText value={text} onChange={(event) => setText(event.target.value)} />
)}
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand All @@ -165,7 +167,7 @@ const MonitorsTable = () => {
)}
{isSuccess && data.monitors.length > 0 && (
<>
<GenericTable aria-busy={text !== debouncedText} aria-live='assertive' data-qa-id='manage-monitors-table'>
<GenericTable aria-busy={isLoading} aria-live='assertive' data-qa-id='manage-monitors-table'>
<GenericTableHeader>{headers}</GenericTableHeader>
<GenericTableBody>
{data.monitors?.map((monitor) => (
Expand Down
24 changes: 14 additions & 10 deletions apps/meteor/client/omnichannel/slaPolicies/SlaTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ const SlaTable = ({ reload }: { reload: MutableRefObject<() => void> }) => {
const router = useRouter();

const [filter, setFilter] = useState('');
const debouncedFilter = useDebouncedValue(filter, 500);

const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();
const { sortBy, sortDirection, setSort } = useSort<'name' | 'description' | 'dueTimeInMinutes'>('name');

const query = useMemo(
() => ({
text: debouncedFilter,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[debouncedFilter, itemsPerPage, current, sortBy, sortDirection],
const query = useDebouncedValue(
useMemo(
() => ({
text: filter,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[filter, itemsPerPage, current, sortBy, sortDirection],
),
500,
);

const getSlaData = useEndpoint('GET', '/v1/livechat/sla');
Expand Down Expand Up @@ -84,7 +86,9 @@ const SlaTable = ({ reload }: { reload: MutableRefObject<() => void> }) => {

return (
<>
{((isSuccess && data?.sla.length > 0) || queryHasChanged) && <FilterByText onChange={setFilter} />}
{((isSuccess && data?.sla.length > 0) || queryHasChanged) && (
<FilterByText value={filter} onChange={(event) => setFilter(event.target.value)} />
)}
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
26 changes: 15 additions & 11 deletions apps/meteor/client/omnichannel/tags/TagsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { useRemoveTag } from './useRemoveTag';
const TagsTable = () => {
const t = useTranslation();
const [filter, setFilter] = useState('');
const debouncedFilter = useDebouncedValue(filter, 500);
const router = useRouter();

const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();
Expand All @@ -32,15 +31,18 @@ const TagsTable = () => {
const handleAddNew = useMutableCallback(() => router.navigate('/omnichannel/tags/new'));
const handleDeleteTag = useRemoveTag();

const query = useMemo(
() => ({
viewAll: 'true' as const,
text: debouncedFilter,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[debouncedFilter, itemsPerPage, current, sortBy, sortDirection],
const query = useDebouncedValue(
useMemo(
() => ({
viewAll: 'true' as const,
text: filter,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[filter, itemsPerPage, current, sortBy, sortDirection],
),
500,
);

const getTags = useEndpoint('GET', '/v1/livechat/tags');
Expand Down Expand Up @@ -69,7 +71,9 @@ const TagsTable = () => {

return (
<>
{((isSuccess && data?.tags.length > 0) || queryHasChanged) && <FilterByText onChange={setFilter} />}
{((isSuccess && data?.tags.length > 0) || queryHasChanged) && (
<FilterByText value={filter} onChange={(event) => setFilter(event.target.value)} />
)}
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
26 changes: 15 additions & 11 deletions apps/meteor/client/omnichannel/units/UnitsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ import { useRemoveUnit } from './useRemoveUnit';
const UnitsTable = () => {
const { t } = useTranslation();
const [filter, setFilter] = useState('');
const debouncedFilter = useDebouncedValue(filter, 500);
const router = useRouter();

const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();
const { sortBy, sortDirection, setSort } = useSort<'name' | 'visibility'>('name');

const query = useMemo(
() => ({
text: debouncedFilter,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[debouncedFilter, itemsPerPage, current, sortBy, sortDirection],
const query = useDebouncedValue(
useMemo(
() => ({
text: filter,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
}),
[filter, itemsPerPage, current, sortBy, sortDirection],
),
500,
);

const getUnits = useEndpoint('GET', '/v1/livechat/units');
Expand Down Expand Up @@ -69,7 +71,9 @@ const UnitsTable = () => {

return (
<>
{((isSuccess && data?.units.length > 0) || queryHasChanged) && <FilterByText onChange={setFilter} />}
{((isSuccess && data?.units.length > 0) || queryHasChanged) && (
<FilterByText value={filter} onChange={(event) => setFilter(event.target.value)} />
)}
{isLoading && (
<GenericTable aria-busy>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand All @@ -92,7 +96,7 @@ const UnitsTable = () => {
)}
{isSuccess && data?.units.length > 0 && (
<>
<GenericTable aria-busy={filter !== debouncedFilter}>
<GenericTable aria-busy={isLoading}>
<GenericTableHeader>{headers}</GenericTableHeader>
<GenericTableBody>
{data.units.map(({ _id, name, visibility }) => (
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/views/admin/customEmoji/CustomEmoji.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const CustomEmoji = ({ onClick, reload }: CustomEmojiProps) => {

return (
<>
<FilterByText onChange={setText} />
<FilterByText value={text} onChange={(event) => setText(event.target.value)} />
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CustomSoundsTable = ({ reload, onClick }: CustomSoundsTableProps) => {
const { sortBy, sortDirection, setSort } = useSort<'name'>('name');
const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();

const [text, setParams] = useState('');
const [text, setText] = useState('');

const query = useDebouncedValue(
useMemo(
Expand Down Expand Up @@ -63,7 +63,7 @@ const CustomSoundsTable = ({ reload, onClick }: CustomSoundsTableProps) => {

return (
<>
<FilterByText onChange={setParams} />
<FilterByText value={text} onChange={(event) => setText(event.target.value)} />
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const CustomUserStatus = ({ reload, onClick }: CustomUserStatusProps): ReactElem

return (
<>
<FilterByText onChange={setText} />
<FilterByText value={text} onChange={(event) => setText(event.target.value)} />
{data.length === 0 && <GenericNoResult />}
{data && data.length > 0 && (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const DeviceManagementAdminTable = ({ reloadRef }: { reloadRef: MutableRefObject

return (
<>
<FilterByText placeholder={t('Search_Devices_Users')} onChange={setText} />
<FilterByText placeholder={t('Search_Devices_Users')} value={text} onChange={(event) => setText(event.target.value)} />
<DeviceManagementTable
data={data}
phase={phase}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const IntegrationsTable = ({ type }: { type?: string }) => {

return (
<>
<FilterByText placeholder={t('Search_Integrations')} onChange={setText} />
<FilterByText placeholder={t('Search_Integrations')} value={text} onChange={(event) => setText(event.target.value)} />
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const ModerationConsoleTable = () => {

return (
<>
<ModerationFilter setText={setText} setDateRange={setDateRange} />
<ModerationFilter text={text} setText={setText} setDateRange={setDateRange} />
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Pagination, States, StatesAction, StatesActions, StatesIcon, StatesTitl
import { useDebouncedValue, useMediaQuery, useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { useEndpoint, useRouter } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import GenericNoResults from '../../../../components/GenericNoResults';
Expand Down Expand Up @@ -35,16 +35,20 @@ const ModConsoleUsersTable = () => {
});
const { start, end } = dateRange;

const debouncedText = useDebouncedValue(text, 500);

const query = {
selector: debouncedText,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
count: itemsPerPage,
offset: current,
latest: end ? `${new Date(end).toISOString().slice(0, 10)}T23:59:59.999Z` : undefined,
oldest: start ? `${new Date(start).toISOString().slice(0, 10)}T00:00:00.000Z` : undefined,
};
const query = useDebouncedValue(
useMemo(
() => ({
selector: text,
sort: JSON.stringify({ [sortBy]: sortDirection === 'asc' ? 1 : -1 }),
count: itemsPerPage,
offset: current,
latest: end ? `${new Date(end).toISOString().slice(0, 10)}T23:59:59.999Z` : undefined,
oldest: start ? `${new Date(start).toISOString().slice(0, 10)}T00:00:00.000Z` : undefined,
}),
[current, end, itemsPerPage, sortBy, sortDirection, start, text],
),
500,
);

const getReports = useEndpoint('GET', '/v1/moderation.userReports');

Expand Down Expand Up @@ -98,8 +102,7 @@ const ModConsoleUsersTable = () => {

return (
<>
<ModerationFilter setText={setText} setDateRange={setDateRange} />

<ModerationFilter text={text} setText={setText} setDateRange={setDateRange} />
{isLoading && (
<GenericTable>
<GenericTableHeader>{headers}</GenericTableHeader>
Expand Down
Loading

0 comments on commit bec7cf6

Please sign in to comment.