From 0d493454da3861681aca0987487fa57b256bcb90 Mon Sep 17 00:00:00 2001 From: shalanah Date: Wed, 27 Mar 2024 13:00:16 -0500 Subject: [PATCH] refactor: move out status count closer to where it is used --- ...-f.js => fallback-nNGagMvPbhVT9W3vYr8C2.js} | 0 src/components/filterModalContentSpecs.tsx | 18 ++++++++++++++++-- src/hooks/useCanIUseContext.tsx | 13 ------------- 3 files changed, 16 insertions(+), 15 deletions(-) rename public/{fallback-7DXoc7Qr5wMFmyUSbW8-f.js => fallback-nNGagMvPbhVT9W3vYr8C2.js} (100%) diff --git a/public/fallback-7DXoc7Qr5wMFmyUSbW8-f.js b/public/fallback-nNGagMvPbhVT9W3vYr8C2.js similarity index 100% rename from public/fallback-7DXoc7Qr5wMFmyUSbW8-f.js rename to public/fallback-nNGagMvPbhVT9W3vYr8C2.js diff --git a/src/components/filterModalContentSpecs.tsx b/src/components/filterModalContentSpecs.tsx index df50deb..6b335d1 100644 --- a/src/components/filterModalContentSpecs.tsx +++ b/src/components/filterModalContentSpecs.tsx @@ -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; @@ -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; diff --git a/src/hooks/useCanIUseContext.tsx b/src/hooks/useCanIUseContext.tsx index 01fa134..a42c110 100644 --- a/src/hooks/useCanIUseContext.tsx +++ b/src/hooks/useCanIUseContext.tsx @@ -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>; @@ -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? @@ -163,7 +151,6 @@ export const CanIUseContextProvider = ({ value={{ search, setSearch, - statusCounts, loading, hasError, iOSMissingFeatures,