Skip to content

Commit

Permalink
[SummarySenseCard] Add glosses and bg color (#3109)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Jul 25, 2024
1 parent 8be0003 commit 63563d9
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 83 deletions.
75 changes: 75 additions & 0 deletions src/components/WordCard/SensesTextSummary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { type ReactElement } from "react";

import { type Sense } from "api/models";
import { TypographyWithFont } from "utilities/fontComponents";

interface SensesTextSummaryProp {
definitionsOrGlosses: "definitions" | "glosses";
maxLengthPerSense?: number;
senses: Sense[];
}

export default function SensesTextSummary(
props: SensesTextSummaryProp
): ReactElement {
const ellipses = "[...]";
const interSenseSep = " | ";
const intraSenseSep = "; ";

const typographies: ReactElement[] = [];

props.senses.forEach((sense) => {
let texts: string[];
switch (props.definitionsOrGlosses) {
case "definitions":
texts = sense.definitions.map((d) => d.text.trim());
break;
case "glosses":
texts = sense.glosses.map((g) => g.def.trim());
break;
}
let text = texts.filter((t) => t).join(intraSenseSep);
if (!text) {
return;
}

if (props.maxLengthPerSense) {
const maxLength = Math.max(props.maxLengthPerSense, ellipses.length + 1);
if (text.length > maxLength) {
text = `${text.substring(0, maxLength - ellipses.length)}${ellipses}`;
}
}

// Add a sense separator if this isn't the first.
if (typographies.length) {
typographies.push(
<TypographyWithFont analysis display="inline" key={`${sense.guid}-sep`}>
{interSenseSep}
</TypographyWithFont>
);
}

// Use the analysis language of the first non-empty definition/gloss, if any.
let lang: string | undefined;
switch (props.definitionsOrGlosses) {
case "definitions":
lang = sense.definitions.find((d) => d.text.trim())?.language;
break;
case "glosses":
lang = sense.glosses.find((g) => g.def.trim())?.language;
break;
}
typographies.push(
<TypographyWithFont
analysis
display="inline"
key={`${sense.guid}-text`}
lang={lang}
>
{text}
</TypographyWithFont>
);
});

return <div>{typographies}</div>;
}
15 changes: 14 additions & 1 deletion src/components/WordCard/SummarySenseCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { useTranslation } from "react-i18next";

import { GramCatGroup, Sense } from "api/models";
import { PartOfSpeechButton } from "components/Buttons";
import SensesTextSummary from "components/WordCard/SensesTextSummary";
import { groupGramInfo } from "utilities/wordUtilities";

interface SummarySenseCardProps {
backgroundColor?: string;
senses: Sense[];
}

Expand All @@ -26,7 +28,12 @@ export default function SummarySenseCard(
const domIds = [...new Set(semDoms.map((d) => d.id))].sort();

return (
<Card style={{ backgroundColor: "white", marginBottom: 10 }}>
<Card
style={{
backgroundColor: props.backgroundColor || "white",
marginBottom: 10,
}}
>
<CardContent style={{ position: "relative" }}>
{/* Parts of speech */}
{groupedGramInfo.map((info) => (
Expand All @@ -43,6 +50,12 @@ export default function SummarySenseCard(
{t("wordCard.senseCount", { val: props.senses.length })}
</Typography>

{/* Glosses */}
<SensesTextSummary
definitionsOrGlosses="glosses"
senses={props.senses}
/>

{/* Semantic domain numbers */}
<Grid container spacing={1}>
{domIds.map((id) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,14 @@
import { type ReactElement } from "react";

import SensesTextSummary from "components/WordCard/SensesTextSummary";
import { type CellProps } from "goals/ReviewEntries/ReviewEntriesTable/Cells/CellTypes";
import { TypographyWithFont } from "utilities/fontComponents";

export default function DefinitionsCell(props: CellProps): ReactElement {
const MAX_LENGTH = 75;
const typographies: ReactElement[] = [];

props.word.senses.forEach((sense) => {
let text = sense.definitions
.map((d) => d.text.trim())
.filter((t) => t)
.join("; ");
if (!text) {
return;
}

if (text.length > MAX_LENGTH) {
text = `${text.substring(0, MAX_LENGTH)}[...]`;
}

// Add a sense separator if this isn't the first.
if (typographies.length) {
typographies.push(
<TypographyWithFont analysis display="inline" key={`${sense.guid}-sep`}>
{" | "}
</TypographyWithFont>
);
}

// Use the analysis language of the first non-empty definition, if any.
const lang = sense.definitions.find((d) => d.text.trim())?.language;
typographies.push(
<TypographyWithFont
analysis
display="inline"
key={`${sense.guid}-text`}
lang={lang}
>
{text}
</TypographyWithFont>
);
});

return <div>{typographies}</div>;
return (
<SensesTextSummary
definitionsOrGlosses="definitions"
maxLengthPerSense={75}
senses={props.word.senses}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export default function EditSensesCardContent(
</>
) : (
<SummarySenseCard
backgroundColor={
changes.some((change) => change) ? yellow[100] : undefined
}
senses={props.newSenses.filter(
(s) => s.accessibility !== Status.Deleted
)}
Expand Down
49 changes: 8 additions & 41 deletions src/goals/ReviewEntries/ReviewEntriesTable/Cells/GlossesCell.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,14 @@
import { type ReactElement } from "react";

import SensesTextSummary from "components/WordCard/SensesTextSummary";
import { type CellProps } from "goals/ReviewEntries/ReviewEntriesTable/Cells/CellTypes";
import { TypographyWithFont } from "utilities/fontComponents";

export default function GlossesCell(props: CellProps): ReactElement {
const MAX_LENGTH = 50;
const typographies: ReactElement[] = [];

props.word.senses.forEach((sense) => {
let text = sense.glosses
.map((g) => g.def.trim())
.filter((t) => t)
.join("; ");
if (!text) {
return;
}

if (text.length > MAX_LENGTH) {
text = `${text.substring(0, MAX_LENGTH)}[...]`;
}

// Add a sense separator if this isn't the first.
if (typographies.length) {
typographies.push(
<TypographyWithFont analysis display="inline" key={`${sense.guid}-sep`}>
{" | "}
</TypographyWithFont>
);
}

// Use the analysis language of the first non-empty gloss, if any.
const lang = sense.glosses.find((g) => g.def.trim())?.language;
typographies.push(
<TypographyWithFont
analysis
display="inline"
key={`${sense.guid}-text`}
lang={lang}
>
{text}
</TypographyWithFont>
);
});

return <div>{typographies}</div>;
return (
<SensesTextSummary
definitionsOrGlosses="glosses"
maxLengthPerSense={50}
senses={props.word.senses}
/>
);
}

0 comments on commit 63563d9

Please sign in to comment.