You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.7 KiB
111 lines
3.7 KiB
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { Ic } from "@/icons";
|
|
import HelpModal from "./help-modal";
|
|
import type { QuestionField } from "@/lib/schema-adapter";
|
|
import { useI18n } from "@/translations/provider";
|
|
|
|
type QuestionTitleProps = {
|
|
question: QuestionField;
|
|
className?: string;
|
|
};
|
|
|
|
export function QuestionTitle({ question, className }: QuestionTitleProps) {
|
|
const { dictionary: t, locale } = useI18n();
|
|
const [isHelpOpen, setIsHelpOpen] = useState(false);
|
|
|
|
if (!question) return null;
|
|
|
|
let suffix = "";
|
|
if (question.ui_config?.suffix) {
|
|
suffix = ` ${question.ui_config.suffix}`;
|
|
} else if (question.ui_config?.suffix_i18n) {
|
|
suffix = ` ${question.ui_config.suffix_i18n[locale] || question.ui_config.suffix_i18n.en || ""}`;
|
|
} else if (question.ui_config?.showApproximateSuffix) {
|
|
suffix = locale === "fa" ? " (تقریبی)" : " (approximately)";
|
|
} else if (question.ui_config?.showYearsSuffix) {
|
|
suffix = locale === "fa" ? " (سال)" : " (Years)";
|
|
}
|
|
|
|
let rawTitle = question.title || "";
|
|
if (suffix && rawTitle.trim().endsWith(suffix.trim())) {
|
|
rawTitle = rawTitle.trim().slice(0, -suffix.trim().length).trim();
|
|
}
|
|
|
|
const titleText = rawTitle + suffix;
|
|
const words = titleText.trim().split(/\s+/).filter(Boolean);
|
|
const lastWord = words.length > 0 ? words[words.length - 1] : "";
|
|
const remainingTitle = words.length > 1 ? words.slice(0, -1).join(" ") : "";
|
|
|
|
const isPrivate = Boolean(
|
|
question.private ||
|
|
(question as any)?.is_private ||
|
|
question.ui_config?.private,
|
|
);
|
|
|
|
return (
|
|
<span className="block w-full">
|
|
{isPrivate ? (
|
|
<span className="mb-4 mx-auto flex w-fit items-center gap-1.5 rounded-full bg-[#F8D7DA] px-3 py-1 text-center text-[10px] font-bold text-[#D9383A]">
|
|
<Ic
|
|
name="eyeOff"
|
|
aria-hidden="true"
|
|
className="size-3.5 shrink-0 text-[#D9383A]"
|
|
/>
|
|
<span>
|
|
{t["Private (Advisors Only)"] ?? "Private (Advisors Only)"}
|
|
</span>
|
|
</span>
|
|
) : null}
|
|
|
|
<span
|
|
className={[
|
|
"block text-[15px] font-bold text-[#181818] leading-snug",
|
|
className,
|
|
]
|
|
.filter(Boolean)
|
|
.join(" ")}
|
|
>
|
|
<span>
|
|
{remainingTitle}
|
|
{remainingTitle ? " " : ""}
|
|
<span className="inline-flex items-center gap-1.5 whitespace-nowrap align-baseline">
|
|
<span>{lastWord}</span>
|
|
{question.required ? (
|
|
<span className="text-[#F2465F] font-bold">*</span>
|
|
) : null}
|
|
{question.tooltip ? (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={(e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
setIsHelpOpen(true);
|
|
}}
|
|
className="inline-flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-[#F2465F] text-[12px] font-serif italic text-white shadow-xs cursor-pointer hover:scale-110 active:scale-95 transition-transform"
|
|
aria-label="Help"
|
|
>
|
|
i
|
|
</button>
|
|
<HelpModal
|
|
isOpen={isHelpOpen}
|
|
onClose={() => setIsHelpOpen(false)}
|
|
description={question.tooltip}
|
|
/>
|
|
</>
|
|
) : null}
|
|
</span>
|
|
</span>
|
|
{question.description ? (
|
|
<span className="text-[12.5px] font-normal text-[#667085] mt-1.5 block leading-normal">
|
|
{question.description}
|
|
</span>
|
|
) : null}
|
|
</span>
|
|
</span>
|
|
);
|
|
}
|
|
|
|
export default QuestionTitle;
|