Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MergeDups] Extract protected-reasons-text functions #3489

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, { word: 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, { sense: reasons }));
}
tooltipTexts.push(t("mergeDups.helpText.protectedSenseInfo"));

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

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

const sep = "; ";

interface WordSenseReasons {
sense?: ProtectReason[];
word?: ProtectReason[];
}

export function protectReasonsText(
t: TFunction,
reasons: WordSenseReasons
): string {
const wordTexts = reasons.word?.map((r) => wordReasonText(t, r));
const senseTexts = reasons.sense?.map((r) => senseReasonText(t, r));
const allTexts = [...(wordTexts ?? []), ...(senseTexts ?? [])];
return t("mergeDups.helpText.protectedData", { val: allTexts.join(sep) });
}

/** Cases match Backend/Helper/LiftHelper.cs > GetProtectedReasons(LiftSense sense) */
function senseReasonText(t: TFunction, 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, 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();
}
}
Loading