Skip to content

Commit

Permalink
refactor: goal title component for easier maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaybadgujar102 committed Nov 12, 2024
1 parent 311d1e3 commit fc81050
Showing 1 changed file with 48 additions and 19 deletions.
67 changes: 48 additions & 19 deletions src/components/GoalsComponents/MyGoal/components/GoalTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,35 @@ interface GoalTitleProps {
isImpossible: boolean;
}

const UrlComponent = ({
url,
goalId,
urlIndex,
isCodeSnippet,
}: {
url: string;
goalId: string;
urlIndex: number;
isCodeSnippet: boolean;
}) => {
const TelHandlerComponent = useTelHandler(url);
const UrlHandlerComponent = useUrlHandler(url);
const displayText = summarizeUrl(url);

if (url.startsWith("tel:")) {
return <TelHandlerComponent key={`${goalId}-tel-${urlIndex}`} />;
}
if (isCodeSnippet) {
return <span key={`${goalId}-url-${urlIndex}`}>{displayText}</span>;
}
return <UrlHandlerComponent key={`${goalId}-url-${urlIndex}`} />;
};

const GoalTitle = ({ goal, isImpossible }: GoalTitleProps) => {
const { t } = useTranslation();
const { copyCode } = useGoalActions();
const { id, title } = goal;

const isCodeSnippet = isGoalCode(title);
const { urlsWithIndexes, replacedString } = replaceUrlsWithText(t(title));
const textParts = replacedString.split(/(zURL-\d+)/g);
Expand All @@ -24,28 +49,32 @@ const GoalTitle = ({ goal, isImpossible }: GoalTitleProps) => {
}
};

const renderTextPart = (part: string) => {
const cleanPart = removeBackTicks(part);
const match = cleanPart.match(/zURL-(\d+)/);

if (!match) {
return <span key={`${id}-text-${part}`}>{part}</span>;
}

const urlIndex = parseInt(match[1], 10);
const url = urlsWithIndexes[urlIndex];

return (
<UrlComponent
key={`${id}-url-${urlIndex}`}
url={url}
goalId={id}
urlIndex={urlIndex}
isCodeSnippet={isCodeSnippet}
/>
);
};

return (
<div aria-hidden className="goal-title" onClick={isCodeSnippet ? handleClick : undefined}>
{isImpossible && "! "}
{textParts.map((part) => {
const cleanPart = removeBackTicks(part);
const match = cleanPart.match(/zURL-(\d+)/);
if (match) {
const urlIndex = parseInt(match[1], 10);
const url = urlsWithIndexes[urlIndex];
const TelHandlerComponent = useTelHandler(url);
const UrlHandlerComponent = useUrlHandler(url);
const displayText = summarizeUrl(url);
if (url.startsWith("tel:")) {
return <TelHandlerComponent key={`${id}-tel-${urlIndex}`} />;
}
if (isCodeSnippet) {
return <span key={`${id}-url-${urlIndex}`}>{displayText}</span>;
}
return <UrlHandlerComponent key={`${id}-url-${urlIndex}`} />;
}
return <span key={`${id}-text-${part}`}>{part}</span>;
})}
{textParts.map(renderTextPart)}
</div>
);
};
Expand Down

0 comments on commit fc81050

Please sign in to comment.