|
| 1 | +import { |
| 2 | + camelCaseToAllDown, |
| 3 | + capitalize, |
| 4 | + groupBy, |
| 5 | + type ScopeSupportFacet, |
| 6 | + type ScopeSupportFacetInfo, |
| 7 | + scopeSupportFacetInfos, |
| 8 | + type ScopeType, |
| 9 | + type SimpleScopeTypeType, |
| 10 | +} from "@cursorless/common"; |
| 11 | +import React, { useState } from "react"; |
| 12 | + |
| 13 | +interface Props { |
| 14 | + facets: ScopeSupportFacet[]; |
| 15 | + title: string; |
| 16 | + subtitle: string; |
| 17 | + description?: React.ReactNode; |
| 18 | + expanded?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export function ScopeSupportForLevel({ |
| 22 | + facets, |
| 23 | + title, |
| 24 | + subtitle, |
| 25 | + description, |
| 26 | + expanded: expandedProp, |
| 27 | +}: Props): JSX.Element | null { |
| 28 | + const [expanded, setExpanded] = useState(expandedProp ?? false); |
| 29 | + |
| 30 | + if (facets.length === 0) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + const renderBody = () => { |
| 35 | + if (!expanded) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + const facetInfos = facets.map( |
| 40 | + (facet): AugmentedFacetInfo => ({ |
| 41 | + facet, |
| 42 | + ...scopeSupportFacetInfos[facet], |
| 43 | + }), |
| 44 | + ); |
| 45 | + const scopeGroups: Map<string, AugmentedFacetInfo[]> = groupBy( |
| 46 | + facetInfos, |
| 47 | + (facetInfo) => serializeScopeType(facetInfo.scopeType), |
| 48 | + ); |
| 49 | + const scopeTypes = Array.from(scopeGroups.keys()).sort(); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="card-body"> |
| 53 | + {description && <p>{description}</p>} |
| 54 | + |
| 55 | + {scopeTypes.map((scopeType) => { |
| 56 | + const facetInfos = scopeGroups.get(scopeType) ?? []; |
| 57 | + return ( |
| 58 | + <div key={scopeType}> |
| 59 | + <h4>{prettifyScopeType(scopeType)}</h4> |
| 60 | + <ul> |
| 61 | + {facetInfos.map((facetInfo) => { |
| 62 | + return ( |
| 63 | + <li key={facetInfo.facet}> |
| 64 | + <span className="facet-name" title={facetInfo.facet}> |
| 65 | + {prettifyFacet(facetInfo.facet)} |
| 66 | + </span> |
| 67 | + : {facetInfo.description} |
| 68 | + </li> |
| 69 | + ); |
| 70 | + })} |
| 71 | + </ul> |
| 72 | + </div> |
| 73 | + ); |
| 74 | + })} |
| 75 | + </div> |
| 76 | + ); |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className="card"> |
| 81 | + <div className="card-header" onClick={() => setExpanded(!expanded)}> |
| 82 | + <h3>{title}</h3> |
| 83 | + {subtitle} |
| 84 | + </div> |
| 85 | + |
| 86 | + {renderBody()} |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +interface AugmentedFacetInfo extends ScopeSupportFacetInfo { |
| 92 | + facet: ScopeSupportFacet; |
| 93 | +} |
| 94 | + |
| 95 | +function prettifyScopeType(scopeType: string): string { |
| 96 | + return capitalize(camelCaseToAllDown(scopeType)); |
| 97 | +} |
| 98 | + |
| 99 | +function prettifyFacet(facet: ScopeSupportFacet): string { |
| 100 | + const parts = facet.split(".").map(camelCaseToAllDown); |
| 101 | + if (parts.length === 1) { |
| 102 | + return capitalize(parts[0]); |
| 103 | + } |
| 104 | + const isIteration = parts[parts.length - 1] === "iteration"; |
| 105 | + if (isIteration) { |
| 106 | + parts.pop(); |
| 107 | + } |
| 108 | + const name = capitalize(parts.slice(1).join(" ")); |
| 109 | + return isIteration ? `${name} (iteration)` : name; |
| 110 | +} |
| 111 | + |
| 112 | +function serializeScopeType( |
| 113 | + scopeType: SimpleScopeTypeType | ScopeType, |
| 114 | +): string { |
| 115 | + if (typeof scopeType === "string") { |
| 116 | + return scopeType; |
| 117 | + } |
| 118 | + return scopeType.type; |
| 119 | +} |
0 commit comments