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

fix!: ensure select category value label is string and type key as unknown (#298) #299

Merged
merged 8 commits into from
Dec 18, 2024
2 changes: 1 addition & 1 deletion src/apis/azul/common/filterTransformer.ts
NoopDog marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function transformTermFacets(
categoryValues.push({
count: 0,
key: term,
label: term ?? LABEL.UNSPECIFIED,
label: String(term ?? LABEL.UNSPECIFIED),
selected: false, // Selected state updated in filter hook
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/apis/azul/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ParamValue } from "./filterTransformer";
* @param value - Filter value.
* @returns filter parameter value.
*/
export function getFilterParameterValue(value: string): ParamValue {
export function getFilterParameterValue(value: unknown): ParamValue {
if (value === "Unspecified") {
return null;
}
Expand All @@ -15,5 +15,7 @@ export function getFilterParameterValue(value: string): ParamValue {
if (value === "false") {
return false;
}
return value;
if (typeof value === "string" || typeof value === "boolean") return value;
if (value === null || value === undefined) return null;
return String(value);
}
6 changes: 3 additions & 3 deletions src/common/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface CategoryTag {
/**
* Category values to be used as keys. For example, "Homo sapiens" or "10X 3' v2 sequencing".
*/
export type CategoryValueKey = string;
export type CategoryValueKey = unknown;

/**
* Set of selected category values.
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface SelectCategory {
*/
export interface SelectCategoryValue {
count: number;
key: CategoryKey;
key: CategoryValueKey;
label: string; // Allows for displaying null values as "Unspecified"
selected: boolean;
}
Expand All @@ -73,7 +73,7 @@ export interface SelectCategoryValueView {
*/
export interface SelectCategoryView {
isDisabled?: boolean;
key: CategoryValueKey;
key: CategoryKey;
frano-m marked this conversation as resolved.
Show resolved Hide resolved
label: string;
values: SelectCategoryValueView[];
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Filter/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function getSortMatchesFn(
let match = matchString(value.label || "");
if (match) {
matches.push({ labelRanges: match.ranges, score: match.score, value });
} else {
} else if (typeof value.key === "string") {
frano-m marked this conversation as resolved.
Show resolved Hide resolved
match = matchString(value.key || "");
if (match) matches.push({ score: match.score, value });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { FilterMenuSearchMatch } from "../../common/entities";
import { List as FilterList } from "../FilterList/filterList.styles";
import VariableSizeListItem from "../VariableSizeListItem/variableSizeListItem";

export type ItemSizeByItemKey = Map<string, number>;
export type ItemSizeByItemKey = Map<unknown, number>;

export interface VariableSizeListProps {
categoryKey: CategoryKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Props {
categorySection?: string;
matchedItem: FilterMenuSearchMatch;
onFilter: OnFilterFn;
onUpdateItemSizeByItemKey: (itemKey: string, itemSize: number) => void;
onUpdateItemSizeByItemKey: (itemKey: unknown, itemSize: number) => void;
style: CSSProperties;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function buildCategoryViews<T extends RowData>(
const values = [...getFacetedUniqueValues()].map(([value, count]) => ({
count,
key: value,
label: value,
label: String(value ?? ""),
selected: false, // Selected state updated in reducer.
}));
categoryViews.push({
Expand Down
2 changes: 1 addition & 1 deletion src/config/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export interface TrackFilterAppliedPayload {
searchTerm: string;
section: string;
selected: boolean;
value: string;
value: unknown;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useCategoryFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ function sortCategoryValueViews(
cvv0: SelectCategoryValueView,
cvv1: SelectCategoryValueView
): number {
return COLLATOR_CASE_INSENSITIVE.compare(cvv0.label, cvv1.label);
return !cvv0.label
? 1
: !cvv1.label
? -1
: COLLATOR_CASE_INSENSITIVE.compare(cvv0.label, cvv1.label);
frano-m marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useFileManifest/common/utils.ts
NoopDog marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function bindFacets(
function bindFacetTerms(
facetName: string,
responseTerms: AzulTerm[],
searchTermNames: string[]
searchTermNames: unknown[]
): Term[] {
return responseTerms.reduce((accum: Term[], responseTerm) => {
// Default term name to "Unspecified" if term name is null.
Expand Down Expand Up @@ -203,7 +203,7 @@ export function isFacetTermSelected(
function listFacetSearchTermNames(
facetName: string,
searchTermsBySearchKey: SelectedSearchTermsBySearchKey
): string[] {
): unknown[] {
return [...(searchTermsBySearchKey.get(facetName) ?? [])];
}

Expand Down
6 changes: 2 additions & 4 deletions src/providers/fileManifestState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function fileManifestReducer(
const filters = buildNextFilterState(
state.filters,
payload.categoryKey,
getFilterParameterValue(payload.selectedValue) as unknown as string, // TODO CategoryValueKey may be boolean or null.
getFilterParameterValue(payload.selectedValue),
payload.selected
);
// Get file summary filters.
Expand Down Expand Up @@ -322,9 +322,7 @@ function buildNextFileSummaryFilterState(
* @returns all terms for the given category.
*/
function getFileFacetTerms(fileFacet: FileFacet): SelectedFilterValue {
return fileFacet.terms.map(
(term) => getFilterParameterValue(term.name) as unknown as string // TODO CategoryValueKey may be boolean or null.
);
return fileFacet.terms.map((term) => getFilterParameterValue(term.name));
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/viewModelBuilders/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ export function mapCategoryKeyLabel(
}

/**
* Sanitizes a string for display i.e. any empty, null or undefined value is sanitized to "Unspecified".
* @param str - String to sanitize.
* Sanitizes a value to a string for display i.e. any empty, null or undefined value is sanitized to "Unspecified".
* @param value - Value to sanitize.
* @returns the string or sanitized string value.
*/
export function sanitizeString(str: string): string {
if (str === "" || str === null || str === undefined) {
export function sanitizeString(value: unknown): string {
frano-m marked this conversation as resolved.
Show resolved Hide resolved
if (value === "" || value === null || value === undefined) {
return "Unspecified";
} else {
return str;
return String(value);
}
}

/**
* Sanitizes a string array for display i.e. any string element within the string array that is an empty, null or
* Sanitizes array elements to strings for display i.e. any element within the string array that is an empty, null or
* undefined value is sanitized to "Unspecified".
* @param strArray - String array to sanitize.
* @param array - Array to sanitize.
* @returns the string array, sanitized.
*/
export function sanitizeStringArray(strArray: string[]): string[] {
if (!strArray || strArray.length === 0) {
export function sanitizeStringArray(array: unknown[]): string[] {
if (!array || array.length === 0) {
return ["Unspecified"];
}
return strArray.map(sanitizeString);
return array.map(sanitizeString);
}
2 changes: 1 addition & 1 deletion src/views/ExploreView/exploreView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const ExploreView = (props: ExploreViewProps): JSX.Element => {
if (selected) {
track(EVENT_NAME.FILTER_SELECTED, {
[EVENT_PARAM.FILTER_NAME]: categoryKey,
[EVENT_PARAM.FILTER_VALUE]: selectedCategoryValue,
[EVENT_PARAM.FILTER_VALUE]: String(selectedCategoryValue),
frano-m marked this conversation as resolved.
Show resolved Hide resolved
});
}
};
Expand Down
Loading