feat: add per-button enable/disable toggle and custom labels per locale

- Add Toggle per section card to show/hide button in main menu
- Add collapsible locale label inputs (ru, en, ua, zh, fa) per section
- Normalize server response for backward compatibility with older API
- Update all 4 locale files with new translation keys
This commit is contained in:
Fringg
2026-02-12 23:42:58 +03:00
parent e586129c37
commit 1a0a5ff453
6 changed files with 197 additions and 40 deletions

View File

@@ -7,7 +7,9 @@ import {
DEFAULT_BUTTON_STYLES,
BUTTON_SECTIONS,
ButtonSection,
BOT_LOCALES,
} from '../../api/buttonStyles';
import { Toggle } from './Toggle';
type StyleValue = 'primary' | 'success' | 'danger' | 'default';
@@ -18,10 +20,19 @@ const STYLE_OPTIONS: { value: StyleValue; colorClass: string }[] = [
{ value: 'danger', colorClass: 'bg-red-500' },
];
function labelsEqual(a: Record<string, string>, b: Record<string, string>): boolean {
for (const locale of BOT_LOCALES) {
if ((a[locale] || '') !== (b[locale] || '')) return false;
}
return true;
}
function stylesEqual(a: ButtonStylesConfig, b: ButtonStylesConfig): boolean {
for (const section of BUTTON_SECTIONS) {
if (a[section].style !== b[section].style) return false;
if (a[section].icon_custom_emoji_id !== b[section].icon_custom_emoji_id) return false;
if (a[section].enabled !== b[section].enabled) return false;
if (!labelsEqual(a[section].labels, b[section].labels)) return false;
}
return true;
}
@@ -36,6 +47,7 @@ export function ButtonsTab() {
});
const [draftStyles, setDraftStyles] = useState<ButtonStylesConfig>(DEFAULT_BUTTON_STYLES);
const [expandedLabels, setExpandedLabels] = useState<Set<string>>(new Set());
const savedStylesRef = useRef<ButtonStylesConfig>(DEFAULT_BUTTON_STYLES);
const draftStylesRef = useRef(draftStyles);
draftStylesRef.current = draftStyles;
@@ -85,27 +97,76 @@ export function ButtonsTab() {
[],
);
const toggleEnabled = useCallback((section: ButtonSection) => {
setDraftStyles((prev) => ({
...prev,
[section]: {
...prev[section],
enabled: !prev[section].enabled,
},
}));
}, []);
const updateLabel = useCallback((section: ButtonSection, locale: string, value: string) => {
setDraftStyles((prev) => ({
...prev,
[section]: {
...prev[section],
labels: {
...prev[section].labels,
[locale]: value,
},
},
}));
}, []);
const toggleLabelsExpanded = useCallback((section: string) => {
setExpandedLabels((prev) => {
const next = new Set(prev);
if (next.has(section)) {
next.delete(section);
} else {
next.add(section);
}
return next;
});
}, []);
const handleCancel = useCallback(() => {
setDraftStyles(savedStylesRef.current);
}, []);
const handleSave = useCallback(() => {
// Build partial update — only send changed sections
const update: Record<string, Record<string, string>> = {};
const update: Record<string, Record<string, unknown>> = {};
for (const section of BUTTON_SECTIONS) {
const draft = draftStyles[section];
const saved = savedStylesRef.current[section];
if (
draft.style !== saved.style ||
draft.icon_custom_emoji_id !== saved.icon_custom_emoji_id
) {
update[section] = {};
if (draft.style !== saved.style) {
update[section].style = draft.style;
}
if (draft.icon_custom_emoji_id !== saved.icon_custom_emoji_id) {
update[section].icon_custom_emoji_id = draft.icon_custom_emoji_id;
const sectionUpdate: Record<string, unknown> = {};
let changed = false;
if (draft.style !== saved.style) {
sectionUpdate.style = draft.style;
changed = true;
}
if (draft.icon_custom_emoji_id !== saved.icon_custom_emoji_id) {
sectionUpdate.icon_custom_emoji_id = draft.icon_custom_emoji_id;
changed = true;
}
if (draft.enabled !== saved.enabled) {
sectionUpdate.enabled = draft.enabled;
changed = true;
}
if (!labelsEqual(draft.labels, saved.labels)) {
const cleanLabels: Record<string, string> = {};
for (const locale of BOT_LOCALES) {
cleanLabels[locale] = (draft.labels[locale] || '').trim();
}
sectionUpdate.labels = cleanLabels;
changed = true;
}
if (changed) {
update[section] = sectionUpdate;
}
}
if (Object.keys(update).length > 0) {
@@ -119,33 +180,50 @@ export function ButtonsTab() {
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
{BUTTON_SECTIONS.map((section) => {
const cfg = draftStyles[section];
const isExpanded = expandedLabels.has(section);
const hasCustomLabels = BOT_LOCALES.some((l) => (cfg.labels[l] || '').trim());
return (
<div
key={section}
className="rounded-2xl border border-dark-700/50 bg-dark-800/50 p-4 sm:p-5"
className={`rounded-2xl border bg-dark-800/50 p-4 transition-colors sm:p-5 ${
cfg.enabled ? 'border-dark-700/50' : 'border-dark-700/30 opacity-60'
}`}
>
{/* Header */}
<div className="mb-3 flex items-center justify-between">
<div>
<h4 className="text-sm font-semibold text-dark-100">
{t(`admin.buttons.sections.${section}`)}
</h4>
<div className="flex-1">
<div className="flex items-center gap-2">
<h4 className="text-sm font-semibold text-dark-100">
{t(`admin.buttons.sections.${section}`)}
</h4>
{!cfg.enabled && (
<span className="rounded bg-dark-600 px-1.5 py-0.5 text-[10px] font-medium text-dark-400">
{t('admin.buttons.hidden')}
</span>
)}
</div>
<p className="mt-0.5 text-xs text-dark-400">
{t(`admin.buttons.descriptions.${section}`)}
</p>
</div>
{/* Live preview chip */}
<div
className={`rounded-lg px-3 py-1.5 text-xs font-medium ${
cfg.style === 'default'
? 'bg-dark-600 text-dark-300'
: cfg.style === 'success'
? 'bg-green-500 text-white'
: cfg.style === 'danger'
? 'bg-red-500 text-white'
: 'bg-blue-500 text-white'
}`}
>
{t(`admin.buttons.styles.${cfg.style}`)}
<div className="flex items-center gap-3">
{/* Live preview chip */}
<div
className={`rounded-lg px-3 py-1.5 text-xs font-medium ${
cfg.style === 'default'
? 'bg-dark-600 text-dark-300'
: cfg.style === 'success'
? 'bg-green-500 text-white'
: cfg.style === 'danger'
? 'bg-red-500 text-white'
: 'bg-blue-500 text-white'
}`}
>
{t(`admin.buttons.styles.${cfg.style}`)}
</div>
{/* Enabled toggle */}
<Toggle checked={cfg.enabled} onChange={() => toggleEnabled(section)} />
</div>
</div>
@@ -173,7 +251,7 @@ export function ButtonsTab() {
</div>
{/* Emoji ID input */}
<div>
<div className="mb-3">
<label className="mb-1.5 block text-xs font-medium text-dark-300">
{t('admin.buttons.emojiId')}
</label>
@@ -185,6 +263,48 @@ export function ButtonsTab() {
className="w-full rounded-lg border border-dark-600 bg-dark-700/50 px-3 py-2 text-sm text-dark-100 placeholder-dark-500 transition-colors focus:border-accent-500 focus:outline-none"
/>
</div>
{/* Custom labels */}
<div>
<button
onClick={() => toggleLabelsExpanded(section)}
className="flex w-full items-center justify-between text-xs font-medium text-dark-300 transition-colors hover:text-dark-200"
>
<span className="flex items-center gap-1.5">
{t('admin.buttons.customLabels')}
{hasCustomLabels && <span className="h-1.5 w-1.5 rounded-full bg-accent-500" />}
</span>
<svg
className={`h-3.5 w-3.5 transition-transform ${isExpanded ? 'rotate-180' : ''}`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" />
</svg>
</button>
{isExpanded && (
<div className="mt-2 space-y-2">
{BOT_LOCALES.map((locale) => (
<div key={locale} className="flex items-center gap-2">
<span className="w-7 shrink-0 text-center text-[10px] font-semibold uppercase text-dark-500">
{locale}
</span>
<input
type="text"
value={cfg.labels[locale] || ''}
onChange={(e) => updateLabel(section, locale, e.target.value)}
placeholder={t('admin.buttons.labelPlaceholder')}
maxLength={100}
className="w-full rounded-lg border border-dark-600 bg-dark-700/50 px-3 py-1.5 text-sm text-dark-100 placeholder-dark-500 transition-colors focus:border-accent-500 focus:outline-none"
/>
</div>
))}
<p className="text-[10px] text-dark-500">{t('admin.buttons.labelsHint')}</p>
</div>
)}
</div>
</div>
);
})}
@@ -198,7 +318,7 @@ export function ButtonsTab() {
disabled={updateMutation.isPending}
className="rounded-xl bg-accent-500 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-accent-600 disabled:opacity-50"
>
{updateMutation.isPending ? t('common.saving', t('common.save')) : t('common.save')}
{updateMutation.isPending ? t('common.saving') : t('common.save')}
</button>
<button
onClick={handleCancel}