Skip to content

Commit

Permalink
Extract protected-reasons-text functions
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Dec 16, 2024
1 parent 5251099 commit 0ef9081
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 128 deletions.
63 changes: 3 additions & 60 deletions src/goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/DropWord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { type ReactElement } from "react";
import { Droppable } from "react-beautiful-dnd";
import { useTranslation } from "react-i18next";

import { type Flag, type ProtectReason, ReasonType } from "api/models";
import { type Flag } from "api/models";
import {
FlagButton,
IconButtonWithTooltip,
Expand All @@ -20,6 +20,7 @@ import {
import MultilineTooltipTitle from "components/MultilineTooltipTitle";
import { AudioSummary } from "components/WordCard";
import DragSense from "goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/DragSense";
import { protectReasonsText } from "goals/MergeDuplicates/MergeDupsStep/protectReasonUtils";
import { type MergeTreeWord } from "goals/MergeDuplicates/MergeDupsTreeTypes";
import {
flagWord,
Expand Down Expand Up @@ -164,68 +165,10 @@ export function DropWordCardHeader(
<div />
);

const reasonText = (reason: ProtectReason): string => {
// Backend/Helper/LiftHelper.cs > GetProtectedReasons(LiftEntry entry)
switch (reason.type) {
case ReasonType.Annotations:
return t("mergeDups.protectReason.annotations");
case ReasonType.Etymologies:
return t("mergeDups.protectReason.etymologies");
case ReasonType.Field:
return t("mergeDups.protectReason.field", { val: reason.value });
case ReasonType.NoteWithType:
return t("mergeDups.protectReason.noteWithType", { val: reason.value });
case ReasonType.Notes:
return t("mergeDups.protectReason.notesWord");
case ReasonType.Relations:
return t("mergeDups.protectReason.relations");
case ReasonType.Trait:
return reason.value ?? "(unknown trait)";
case ReasonType.TraitDialectLabels:
return t("mergeDups.protectReason.traitDialectLabels", {
val: reason.value,
});
case ReasonType.TraitDoNotPublishIn:
return t("mergeDups.protectReason.traitDoNotPublishIn", {
val: reason.value,
});
case ReasonType.TraitDoNotUseForParsing:
return t("mergeDups.protectReason.traitDoNotUseForParsing", {
val: reason.value,
});
case ReasonType.TraitEntryType:
return t("mergeDups.protectReason.traitEntryType", {
val: reason.value,
});
case ReasonType.TraitExcludeAsHeadword:
return t("mergeDups.protectReason.traitExcludeAsHeadword");
case ReasonType.TraitMinorEntryCondition:
return t("mergeDups.protectReason.traitMinorEntryCondition", {
val: reason.value,
});
case ReasonType.TraitMorphType:
return t("mergeDups.protectReason.traitMorphType", {
val: reason.value,
});
case ReasonType.TraitPublishIn:
return t("mergeDups.protectReason.traitPublishIn", {
val: reason.value,
});
case ReasonType.Variants:
return t("mergeDups.protectReason.variants");
default:
throw new Error();
}
};

const tooltipTexts = [t("mergeDups.helpText.protectedWord")];
const reasons = words[props.wordId]?.protectReasons;
if (reasons?.length) {
tooltipTexts.push(
t("mergeDups.helpText.protectedData", {
val: reasons.map(reasonText).join("; "),
})
);
tooltipTexts.push(protectReasonsText(t, reasons, []));
}
tooltipTexts.push(t("mergeDups.helpText.protectedWordInfo"));

Expand Down
71 changes: 3 additions & 68 deletions src/goals/MergeDuplicates/MergeDupsStep/SenseCardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ import { CardContent, IconButton } from "@mui/material";
import { type ReactElement } from "react";
import { useTranslation } from "react-i18next";

import {
GramCatGroup,
type ProtectReason,
ReasonType,
type Sense,
Status,
} from "api/models";
import { GramCatGroup, type Sense, Status } from "api/models";
import { IconButtonWithTooltip, PartOfSpeechButton } from "components/Buttons";
import MultilineTooltipTitle from "components/MultilineTooltipTitle";
import DomainChipsGrid from "components/WordCard/DomainChipsGrid";
import SenseCardText from "components/WordCard/SenseCardText";
import { protectReasonsText } from "goals/MergeDuplicates/MergeDupsStep/protectReasonUtils";
import { combineSenses } from "goals/MergeDuplicates/Redux/reducerUtilities";

interface SenseCardContentProps {
Expand Down Expand Up @@ -41,72 +36,12 @@ export default function SenseCardContent(
a.id.localeCompare(b.id)
);

const reasonText = (reason: ProtectReason): string => {
// Backend/Helper/LiftHelper.cs > GetProtectedReasons(LiftSense sense)
switch (reason.type) {
case ReasonType.Annotations:
return t("mergeDups.protectReason.annotations");
case ReasonType.Examples:
return t("mergeDups.protectReason.examples");
case ReasonType.Field:
return t("mergeDups.protectReason.field", { val: reason.value });
case ReasonType.GramInfoTrait:
return t("mergeDups.protectReason.gramInfoTrait", {
val: reason.value,
});
case ReasonType.Illustrations:
return t("mergeDups.protectReason.illustrations");
case ReasonType.Notes:
return t("mergeDups.protectReason.notesSense");
case ReasonType.Relations:
return t("mergeDups.protectReason.relations");
case ReasonType.Reversals:
return t("mergeDups.protectReason.reversal", { val: reason.value });
case ReasonType.Subsenses:
return t("mergeDups.protectReason.subsenses");
case ReasonType.Trait:
return reason.value ?? "(unknown trait)";
case ReasonType.TraitAnthroCode:
return t("mergeDups.protectReason.traitAnthroCode", {
val: reason.value,
});
case ReasonType.TraitDomainType:
return t("mergeDups.protectReason.traitDomainType", {
val: reason.value,
});
case ReasonType.TraitDoNotPublishIn:
return t("mergeDups.protectReason.traitDoNotPublishIn", {
val: reason.value,
});
case ReasonType.TraitPublishIn:
return t("mergeDups.protectReason.traitPublishIn", {
val: reason.value,
});
case ReasonType.TraitSenseType:
return t("mergeDups.protectReason.traitSenseType", {
val: reason.value,
});
case ReasonType.TraitStatus:
return t("mergeDups.protectReason.traitStatus", { val: reason.value });
case ReasonType.TraitUsageType:
return t("mergeDups.protectReason.traitUsageType", {
val: reason.value,
});
default:
throw new Error();
}
};

const protectedWarning =
!props.sidebar && sense.accessibility === Status.Protected;
const tooltipTexts = [t("mergeDups.helpText.protectedSense")];
const reasons = sense.protectReasons;
if (reasons?.length) {
tooltipTexts.push(
t("mergeDups.helpText.protectedData", {
val: reasons.map(reasonText).join("; "),
})
);
tooltipTexts.push(protectReasonsText(t, [], reasons));
}
tooltipTexts.push(t("mergeDups.helpText.protectedSenseInfo"));

Expand Down
132 changes: 132 additions & 0 deletions src/goals/MergeDuplicates/MergeDupsStep/protectReasonUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { TFunction } from "i18next";

import { ProtectReason, ReasonType } from "api/models";

const sep = "; ";

export function protectReasonsText(
t: TFunction<"translation", undefined>,
wordReasons: ProtectReason[] = [],
senseReasons: ProtectReason[] = []
): string {
const wordTexts = wordReasons.map((r) => wordReasonText(t, r));
const senseTexts = senseReasons.map((r) => senseReasonText(t, r));
const val = [...wordTexts, ...senseTexts].join(sep);
return t("mergeDups.helpText.protectedData", { val });
}

/** Cases match Backend/Helper/LiftHelper.cs > GetProtectedReasons(LiftSense sense) */
function senseReasonText(
t: TFunction<"translation", undefined>,
reason: ProtectReason
): string {
switch (reason.type) {
case ReasonType.Annotations:
return t("mergeDups.protectReason.annotations");
case ReasonType.Examples:
return t("mergeDups.protectReason.examples");
case ReasonType.Field:
return t("mergeDups.protectReason.field", { val: reason.value });
case ReasonType.GramInfoTrait:
return t("mergeDups.protectReason.gramInfoTrait", {
val: reason.value,
});
case ReasonType.Illustrations:
return t("mergeDups.protectReason.illustrations");
case ReasonType.Notes:
return t("mergeDups.protectReason.notesSense");
case ReasonType.Relations:
return t("mergeDups.protectReason.relations");
case ReasonType.Reversals:
return t("mergeDups.protectReason.reversal", { val: reason.value });
case ReasonType.Subsenses:
return t("mergeDups.protectReason.subsenses");
case ReasonType.Trait:
return reason.value ?? "(unknown trait)";
case ReasonType.TraitAnthroCode:
return t("mergeDups.protectReason.traitAnthroCode", {
val: reason.value,
});
case ReasonType.TraitDomainType:
return t("mergeDups.protectReason.traitDomainType", {
val: reason.value,
});
case ReasonType.TraitDoNotPublishIn:
return t("mergeDups.protectReason.traitDoNotPublishIn", {
val: reason.value,
});
case ReasonType.TraitPublishIn:
return t("mergeDups.protectReason.traitPublishIn", {
val: reason.value,
});
case ReasonType.TraitSenseType:
return t("mergeDups.protectReason.traitSenseType", {
val: reason.value,
});
case ReasonType.TraitStatus:
return t("mergeDups.protectReason.traitStatus", { val: reason.value });
case ReasonType.TraitUsageType:
return t("mergeDups.protectReason.traitUsageType", {
val: reason.value,
});
default:
throw new Error();
}
}

/** Cases match Backend/Helper/LiftHelper.cs > GetProtectedReasons(LiftEntry entry) */
function wordReasonText(
t: TFunction<"translation", undefined>,
reason: ProtectReason
): string {
switch (reason.type) {
case ReasonType.Annotations:
return t("mergeDups.protectReason.annotations");
case ReasonType.Etymologies:
return t("mergeDups.protectReason.etymologies");
case ReasonType.Field:
return t("mergeDups.protectReason.field", { val: reason.value });
case ReasonType.NoteWithType:
return t("mergeDups.protectReason.noteWithType", { val: reason.value });
case ReasonType.Notes:
return t("mergeDups.protectReason.notesWord");
case ReasonType.Relations:
return t("mergeDups.protectReason.relations");
case ReasonType.Trait:
return reason.value ?? "(unknown trait)";
case ReasonType.TraitDialectLabels:
return t("mergeDups.protectReason.traitDialectLabels", {
val: reason.value,
});
case ReasonType.TraitDoNotPublishIn:
return t("mergeDups.protectReason.traitDoNotPublishIn", {
val: reason.value,
});
case ReasonType.TraitDoNotUseForParsing:
return t("mergeDups.protectReason.traitDoNotUseForParsing", {
val: reason.value,
});
case ReasonType.TraitEntryType:
return t("mergeDups.protectReason.traitEntryType", {
val: reason.value,
});
case ReasonType.TraitExcludeAsHeadword:
return t("mergeDups.protectReason.traitExcludeAsHeadword");
case ReasonType.TraitMinorEntryCondition:
return t("mergeDups.protectReason.traitMinorEntryCondition", {
val: reason.value,
});
case ReasonType.TraitMorphType:
return t("mergeDups.protectReason.traitMorphType", {
val: reason.value,
});
case ReasonType.TraitPublishIn:
return t("mergeDups.protectReason.traitPublishIn", {
val: reason.value,
});
case ReasonType.Variants:
return t("mergeDups.protectReason.variants");
default:
throw new Error();
}
}

0 comments on commit 0ef9081

Please sign in to comment.