Skip to content

Commit

Permalink
chore(bcd): duplicate support {type,attrs} calculation inline
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Nov 4, 2024
1 parent b38a264 commit f40b76f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ import {
} from "./feature-row";
import { Headers } from "./headers";
import { Legend } from "./legend";
import { listFeatures } from "./utils";
import {
getCurrentSupport,
hasMore,
hasNoteworthyNotes,
listFeatures,
SimpleSupportStatementExtended,
SupportStatementExtended,
versionIsPreview,
} from "./utils";
import { useViewed } from "../../../hooks";
import { BCD_TABLE } from "../../../telemetry/constants";
import { useGleanClick } from "../../../telemetry/glean-context";
Expand Down Expand Up @@ -129,15 +137,87 @@ function FeatureListAccordion({
const feature = features[row];
const browser = browsers[column];
const support = feature.compat.support[browser];

function getCurrentSupportType(
support: SupportStatementExtended | undefined,
browser: BCD.BrowserStatement
) {
if (!support) {
return "unknown";
}
const currentSupport = getCurrentSupport(support)!;

return getSupportType(currentSupport, browser);
}

function getSupportType(
support: SimpleSupportStatementExtended,
browser: BCD.BrowserStatement
):
| "no"
| "yes"
| "partial"
| "preview"
| "removed"
| "removed-partial"
| "unknown" {
const {
flags,
version_added,
version_removed,
partial_implementation,
} = support;

if (version_added === null) {
return "unknown";
} else if (versionIsPreview(version_added, browser)) {
return "preview";
} else if (version_added) {
if (version_removed) {
if (partial_implementation) {
return "removed-partial";
} else {
return "removed";
}
} else if (flags && flags.length) {
return "no";
} else if (partial_implementation) {
return "partial";
} else {
return "yes";
}
} else {
return "no";
}
}

function getCurrentSupportAttributes(
support:
| BCD.SimpleSupportStatement
| BCD.SimpleSupportStatement[]
| undefined
): string[] {
const supportItem = getCurrentSupport(support);

if (!supportItem) {
return [];
}

return [
!!supportItem.prefix && "pre",
hasNoteworthyNotes(supportItem) && "note",
!!supportItem.alternative_name && "alt",
!!supportItem.flags && "flag",
hasMore(support) && "more",
].filter((value) => typeof value === "string");
}

const supportType = getCurrentSupportType(
support,
browserInfo[browser]
);
const { ...supportAttributes } =
getCurrentSupportAttributes(support);
const attrs = Object.keys(supportAttributes).filter(
(key) => supportAttributes[key]
);
const attrs = getCurrentSupportAttributes(support);

gleanClick(
`${BCD_TABLE}: click ${browser} ${query} -> ${feature.name} = ${supportType} [${attrs.join(",")}]`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type BCD from "@mdn/browser-compat-data/types";

// Extended for the fields, beyond the bcd types, that are extra-added
// exclusively in Yari.
interface SimpleSupportStatementExtended extends BCD.SimpleSupportStatement {
export interface SimpleSupportStatementExtended
extends BCD.SimpleSupportStatement {
// Known for some support statements where the browser *version* is known,
// as opposed to just "true" and if the version release date is known.
release_date?: string;
Expand Down

0 comments on commit f40b76f

Please sign in to comment.