"use client";
import { useMemo } from 'react';
import { useTranslations } from 'next-intl';
import { Check, Folder } from 'lucide-react';
import { useSettingsStore, type ToolbarPosition, type MailLayout } from '@/stores/settings-store';
import { SettingsSection, SettingItem, RadioGroup, ToggleSwitch } from './settings-section';
import { cn } from '@/lib/utils';
import { usePolicyStore } from '@/stores/policy-store';
import { useAccountStore } from '@/stores/account-store';
import { useEmailStore } from '@/stores/email-store';
const MAIL_LAYOUT_PREVIEW_ROWS = [
{ sender: 'Alice', subject: 'Quarterly roadmap', preview: 'The draft is ready for review.', selected: false },
{ sender: 'Nadia', subject: 'Design sync', preview: 'Pushed updated mocks and notes.', selected: true },
{ sender: 'Billing', subject: 'Invoice 1042', preview: 'Your receipt is attached.', selected: false },
];
const MAIL_LAYOUT_PREVIEW_ROWS_FOCUS = [
...MAIL_LAYOUT_PREVIEW_ROWS,
{ sender: 'Sam', subject: 'Lunch?', preview: '', selected: false },
{ sender: 'Newsletter', subject: 'Weekly digest', preview: '', selected: false },
];
function MailLayoutPreview({
value,
t,
}: {
value: MailLayout;
t: (key: string) => string;
}) {
return (
{t(`mail_layout.${value}`)}
{t(`mail_layout.${value}_description`)}
{value === 'split' && (
<>
{MAIL_LAYOUT_PREVIEW_ROWS.map((row) => (
{row.sender}
{row.subject}
))}
>
)}
{value === 'focus' && (
{MAIL_LAYOUT_PREVIEW_ROWS_FOCUS.map((row) => (
{row.sender}
{row.subject}
))}
)}
{value === 'horizontal' && (
{MAIL_LAYOUT_PREVIEW_ROWS.map((row) => (
{row.sender}
{row.subject}
))}
)}
);
}
export function LayoutSettings() {
const t = useTranslations('settings.appearance');
const tEmail = useTranslations('settings.email_behavior');
const { toolbarPosition, showToolbarLabels, hideAccountSwitcher, showRailAccountList, enableUnifiedMailbox, includeGroupInUnified, enableAllMailView, allMailFolderIds, colorfulSidebarIcons, mailLayout, proInterface, updateSetting } = useSettingsStore();
const { isSettingLocked, isSettingHidden, isFeatureEnabled } = usePolicyStore();
const accounts = useAccountStore(s => s.accounts);
const mailboxes = useEmailStore(s => s.mailboxes);
const hasGroupInboxes = useMemo(() => mailboxes.some(m => m.isShared), [mailboxes]);
const allMailViewAllowed = isFeatureEnabled('allMailViewEnabled');
// Own (non-shared) folders and the current All Mail selection. `null` =
// never configured, which defaults to all non-special (no-role) folders.
const ownMailboxes = useMemo(() => mailboxes.filter(m => !m.isShared), [mailboxes]);
const allMailSelected = new Set(
allMailFolderIds === null
? ownMailboxes.filter(m => !m.role).map(m => m.id)
: allMailFolderIds
);
const toggleAllMailFolder = (id: string) => {
const next = new Set(allMailSelected);
if (next.has(id)) next.delete(id);
else next.add(id);
updateSetting('allMailFolderIds', ownMailboxes.filter(m => next.has(m.id)).map(m => m.id));
};
return (
{!isSettingHidden('mailLayout') && (
updateSetting('mailLayout', value as MailLayout)}
options={[
{ value: 'split', label: tEmail('mail_layout.split') },
{ value: 'focus', label: tEmail('mail_layout.focus') },
{ value: 'horizontal', label: tEmail('mail_layout.horizontal') },
]}
/>
)}
updateSetting('toolbarPosition', value as ToolbarPosition)}
options={[
{ value: 'top', label: t('toolbar_position.top') },
{ value: 'below-subject', label: t('toolbar_position.below_subject') },
]}
/>
updateSetting('showToolbarLabels', checked)}
/>
updateSetting('hideAccountSwitcher', checked)}
/>
updateSetting('showRailAccountList', checked)}
/>
updateSetting('colorfulSidebarIcons', checked)}
/>
{(accounts.length > 1 || hasGroupInboxes) && !isSettingHidden('enableUnifiedMailbox') && (
updateSetting('enableUnifiedMailbox', v)}
/>
)}
{enableUnifiedMailbox && hasGroupInboxes && !isSettingHidden('includeGroupInUnified') && (
updateSetting('includeGroupInUnified', v)}
/>
)}
{allMailViewAllowed && !isSettingHidden('enableAllMailView') && (
updateSetting('enableAllMailView', v)}
/>
)}
{allMailViewAllowed && enableAllMailView && (
{t('all_mail.folders_label')}
{t('all_mail.folders_description')}
{ownMailboxes.length === 0 ? (
{t('all_mail.no_folders')}
) : (
{ownMailboxes.map((mb) => {
const checked = allMailSelected.has(mb.id);
return (
);
})}
)}
)}
updateSetting('proInterface', v)}
/>
);
}