Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2665 fix proposal #2678

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/components/hooks/useFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import useMessages from './useMessages';
import { BROWSERS, OS_NAMES } from 'lib/constants';
import useLocale from './useLocale';
import useCountryNames from './useCountryNames';
import useLanguageNames from './useLanguageNames';
import regions from '../../../public/iso-3166-2.json';

export function useFormat() {
const { formatMessage, labels } = useMessages();
const { locale } = useLocale();
const countryNames = useCountryNames(locale);
const languageNames = useLanguageNames(locale);

const formatOS = (value: string): string => {
return OS_NAMES[value] || value;
Expand All @@ -34,6 +36,10 @@ export function useFormat() {
return countryNames[country] ? `${value}, ${countryNames[country]}` : value;
};

const formatLanguage = (value: string): string => {
return languageNames[value?.split('-')[0]] || value;
};

const formatValue = (value: string, type: string, data?: { [key: string]: any }): string => {
switch (type) {
case 'os':
Expand All @@ -48,12 +54,23 @@ export function useFormat() {
return formatRegion(value);
case 'city':
return formatCity(value, data?.country);
case 'language':
return formatLanguage(value);
default:
return value;
}
};

return { formatOS, formatBrowser, formatDevice, formatCountry, formatRegion, formatValue };
return {
formatOS,
formatBrowser,
formatDevice,
formatCountry,
formatRegion,
formatCity,
formatLanguage,
formatValue,
};
}

export default useFormat;
14 changes: 4 additions & 10 deletions src/components/metrics/CitiesTable.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import MetricsTable, { MetricsTableProps } from './MetricsTable';
import { emptyFilter } from 'lib/filters';
import FilterLink from 'components/common/FilterLink';
import { useLocale } from 'components/hooks';
import { useMessages } from 'components/hooks';
import { useCountryNames } from 'components/hooks';
import { useFormat } from 'components/hooks';

export function CitiesTable(props: MetricsTableProps) {
const { locale } = useLocale();
const { formatMessage, labels } = useMessages();
const countryNames = useCountryNames(locale);

const renderLabel = (city: string, country: string) => {
const countryName = countryNames[country];
return countryName ? `${city}, ${countryName}` : city;
};
const { formatCity } = useFormat();

const renderLink = ({ x: city, country }) => {
return (
<FilterLink id="city" value={city} label={renderLabel(city, country)}>
<FilterLink id="city" value={city} label={formatCity(city, country)}>
{country && (
<img
src={`${process.env.basePath}/images/flags/${country?.toLowerCase() || 'xx'}.png`}
Expand All @@ -36,6 +29,7 @@ export function CitiesTable(props: MetricsTableProps) {
metric={formatMessage(labels.visitors)}
dataFilter={emptyFilter}
renderLabel={renderLink}
searchFormattedValues={true}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/metrics/CountriesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function CountriesTable({
metric={formatMessage(labels.visitors)}
renderLabel={renderLink}
onDataLoad={handleDataLoad}
searchFormattedValues={true}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/metrics/DevicesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function DevicesTable(props: MetricsTableProps) {
type="device"
metric={formatMessage(labels.visitors)}
renderLabel={renderLink}
searchFormattedValues={true}
/>
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/metrics/LanguagesTable.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import MetricsTable, { MetricsTableProps } from './MetricsTable';
import { percentFilter } from 'lib/filters';
import { useLanguageNames } from 'components/hooks';
import { useLocale } from 'components/hooks';
import { useMessages } from 'components/hooks';
import { useFormat } from 'components/hooks';

export function LanguagesTable({
onDataLoad,
...props
}: { onDataLoad: (data: any) => void } & MetricsTableProps) {
const { formatMessage, labels } = useMessages();
const { locale } = useLocale();
const languageNames = useLanguageNames(locale);
const { formatLanguage } = useFormat();

const renderLabel = ({ x }) => {
return <div className={locale}>{languageNames[x?.split('-')[0]] ?? x}</div>;
return <div className={locale}>{formatLanguage(x)}</div>;
};

return (
Expand All @@ -24,6 +24,7 @@ export function LanguagesTable({
metric={formatMessage(labels.visitors)}
onDataLoad={data => onDataLoad?.(percentFilter(data))}
renderLabel={renderLabel}
searchFormattedValues={true}
/>
);
}
Expand Down
12 changes: 11 additions & 1 deletion src/components/metrics/MetricsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface MetricsTableProps extends ListTableProps {
onDataLoad?: (data: any) => void;
onSearch?: (search: string) => void;
allowSearch?: boolean;
searchFormattedValues?: boolean;
children?: ReactNode;
}

Expand All @@ -40,6 +41,7 @@ export function MetricsTable({
onDataLoad,
delay = null,
allowSearch = false,
searchFormattedValues = false,
children,
...props
}: MetricsTableProps) {
Expand Down Expand Up @@ -69,7 +71,7 @@ export function MetricsTable({
region,
city,
limit,
search,
search: (searchFormattedValues) ? undefined : search,
},
{ retryDelay: delay || DEFAULT_ANIMATION_DURATION, onDataLoad },
);
Expand All @@ -88,6 +90,14 @@ export function MetricsTable({
}
}

if (searchFormattedValues && search) {
items = items.filter(({ x, ...data }) => {
const value = formatValue(x, type, data);

return value?.toLowerCase().includes(search.toLowerCase());
});
}

items = percentFilter(items);

return items;
Expand Down
1 change: 1 addition & 0 deletions src/components/metrics/RegionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function RegionsTable(props: MetricsTableProps) {
metric={formatMessage(labels.visitors)}
dataFilter={emptyFilter}
renderLabel={renderLink}
searchFormattedValues={true}
/>
);
}
Expand Down