Skip to content

Commit

Permalink
refactor: move out status count closer to where it is used
Browse files Browse the repository at this point in the history
  • Loading branch information
shalanah committed Mar 27, 2024
1 parent 8fc1838 commit 0d49345
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
File renamed without changes.
18 changes: 16 additions & 2 deletions src/components/filterModalContentSpecs.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from 'react';
import { Checkbox } from './checkbox';
import useCanIUseContext, { SpecTypes } from '../hooks/useCanIUseContext';
import useCanIUseContext from '../hooks/useCanIUseContext';
import { Badge } from './badge';
import styled from 'styled-components';
import { FiltersType } from '../hooks/useFilters';

export type SpecTypes = keyof FiltersType['statuses'];

type StatusCounts = {
[K in SpecTypes]: number;
};

const Submit = styled.button`
display: block;
Expand All @@ -27,8 +34,15 @@ export const FilterModalContentSpecs = ({
}: {
onClose?: () => void;
}) => {
const { statusCounts, statuses, filters, setFilters, filteredData } =
const { filteredByBrowserOnly, statuses, filters, setFilters, filteredData } =
useCanIUseContext();
const statusCounts: StatusCounts = filteredByBrowserOnly.reduce(
(acc: { [k: string]: number }, v: { status: 'string' } & any) => {
acc[v.status] += 1;
return acc;
},
Object.fromEntries(Object.keys(statuses || {}).map((k) => [k, 0]))
);
const nonEmptyStatusFilters = Object.fromEntries(
Object.entries(filters.statuses).filter(([k, _]) => {
return statusCounts[k as SpecTypes] > 0;
Expand Down
13 changes: 0 additions & 13 deletions src/hooks/useCanIUseContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@ import { useCanIUseData } from './useCanIUseData';
import { useFilters, type FiltersType } from './useFilters';
// import { parseMdnData } from '../utils/parseMdnData';

export type SpecTypes = keyof FiltersType['statuses'];

type StatusCounts = {
[K in SpecTypes]: number;
};

interface CanIUseContextInterface {
loading: boolean;
hasError: boolean;
iOSMissingFeatures: any;
activeIndex: number;
updateHash: (newHash: string) => void;
statusCounts: StatusCounts;
statuses: { [k: string]: string } | undefined;
filters: FiltersType;
setFilters: Dispatch<SetStateAction<FiltersType>>;
Expand Down Expand Up @@ -134,11 +127,6 @@ export const CanIUseContextProvider = ({
}
};

const statusCounts: StatusCounts = filteredByBrowserOnly.reduce((acc, v) => {
acc[v.status] += 1;
return acc;
}, Object.fromEntries(Object.entries(canIUseData?.statuses || {}).map(([k, v]) => [k, 0])));

// MDN DATA: TODO: Later
// If Safari brings up that caniuse data isn't up-to-date...
// Maybe they should work on that --- who do they really have to blame? That's part of their job, right? Right?
Expand All @@ -163,7 +151,6 @@ export const CanIUseContextProvider = ({
value={{
search,
setSearch,
statusCounts,
loading,
hasError,
iOSMissingFeatures,
Expand Down

0 comments on commit 0d49345

Please sign in to comment.