Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
16.6 KB
Raw Blame
"use client";

import { useMemo, useState } from "react";
import { ChevronsUpDown } from "@multica/ui/icons";
import { toast } from "sonner";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@multica/ui/components/ui/select";
import { Switch } from "@multica/ui/components/ui/switch";
import { Checkbox } from "@multica/ui/components/ui/checkbox";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@multica/ui/components/ui/popover";
import {
  Command,
  CommandEmpty,
  CommandInput,
  CommandItem,
  CommandList,
} from "@multica/ui/components/ui/command";
import type { SupportedLocale } from "@multica/core/i18n";
import { useAuthStore } from "@multica/core/auth";
import { useChatStore } from "@multica/core/chat";
import {
  useCommentComposerStore,
  type CommentImageLayout,
  type RunningAgentReply,
} from "@multica/core/issues/stores";
import {
  useIssueOpeningStore,
  type IssueOpenMode,
} from "@multica/core/issues/stores/issue-opening-store";
import {
  MANUAL_CREATE_FIELDS,
  QUICK_CREATE_FIELDS,
  useIssueCreateSettingsStore,
  type ManualCreateField,
  type QuickCreateField,
} from "@multica/core/issues/stores/issue-create-settings-store";
import { api } from "@multica/core/api";
import { browserTimezone, timezoneOptions } from "../../common/timezone-select";
import { useLanguageSwitch } from "../../common/use-language-switch";
import { SegmentedToggle } from "../../common/segmented-toggle";
import { useT } from "../../i18n";
import { QuickChatAgentRow } from "./quick-chat-agent-row";
import {
  SettingsCard,
  SettingsRow,
  SettingsSection,
  SettingsTab,
} from "./settings-layout";

/**
 * Preferences on one page. The settings here are stored in three different
 * places — the account (language, timezone), this device (composer, chat,
 * opening issues) and this device for the current workspace (create-dialog
 * fields) — so each section carries a scope badge instead of splitting them
 * into tabs. Theme, style and frame colour live on the Appearance page.
 * Changes apply immediately; only failures are announced.
 */
export function PreferencesTab() {
  const { t } = useT("settings");
  return (
    <SettingsTab title={t(($) => $.page.tabs.preferences)}>
      <SettingsSection
        title={t(($) => $.preferences.region_title)}
        scope="account"
        anchor="region"
      >
        <SettingsCard>
          <LanguageRow />
          <TimezoneRow />
        </SettingsCard>
      </SettingsSection>

      <SettingsSection
        title={t(($) => $.preferences.comments_title)}
        scope="device"
        anchor="comments"
      >
        <SettingsCard>
          <StickyCommentBarRow />
          <RunningAgentReplyRow />
          <CommentImageLayoutRow />
          <FloatingChatRow />
          <QuickChatAgentRow />
        </SettingsCard>
      </SettingsSection>

      <SettingsSection
        title={t(($) => $.issue.opening.title)}
        scope="device"
        anchor="issue-opening"
      >
        <SettingsCard>
          <IssueOpeningRow />
        </SettingsCard>
      </SettingsSection>

      <SettingsSection
        title={t(($) => $.preferences.issue_fields_title)}
        description={t(($) => $.issue.description)}
        scope="device-workspace"
        anchor="issue"
      >
        <IssueFieldsMatrix />
      </SettingsSection>
    </SettingsTab>
  );
}

function LanguageRow() {
  const { t } = useT("settings");
  const {
    options: languageOptions,
    currentLocale,
    changeLanguage: handleLanguageChange,
  } = useLanguageSwitch();

  return (
    <SettingsRow
      anchor="language"
      label={t(($) => $.preferences.language.title)}
      size="select"
    >
      <Select
        items={languageOptions}
        value={currentLocale}
        onValueChange={(next) => {
          if (next) void handleLanguageChange(next as SupportedLocale);
        }}
      >
        <SelectTrigger
          size="sm"
          className="w-full"
          aria-label={t(($) => $.preferences.language.title)}
        >
          <SelectValue>
            {languageOptions.find((option) => option.value === currentLocale)?.label}
          </SelectValue>
        </SelectTrigger>
        <SelectContent align="end">
          {languageOptions.map((option) => (
            <SelectItem key={option.value} value={option.value}>
              {option.label}
            </SelectItem>
          ))}
        </SelectContent>
      </Select>
    </SettingsRow>
  );
}

function StickyCommentBarRow() {
  const { t } = useT("settings");
  const sticky = useCommentComposerStore((s) => s.sticky);
  const toggleSticky = useCommentComposerStore((s) => s.toggleSticky);

  return (
    <SettingsRow
      anchor="sticky-comment-bar"
      label={t(($) => $.preferences.sticky_comment_bar.title)}
    >
      <Switch
        checked={sticky}
        onCheckedChange={() => toggleSticky()}
        aria-label={t(($) => $.preferences.sticky_comment_bar.title)}
      />
    </SettingsRow>
  );
}

function RunningAgentReplyRow() {
  const { t } = useT("settings");
  const value = useCommentComposerStore((s) => s.runningAgentReply);
  const setValue = useCommentComposerStore((s) => s.setRunningAgentReply);
  const options: { value: RunningAgentReply; label: string }[] = [
    { value: "steer", label: t(($) => $.preferences.running_agent_reply.steer) },
    { value: "after_run", label: t(($) => $.preferences.running_agent_reply.after_run) },
  ];

  return (
    <SettingsRow
      anchor="running-agent-reply"
      label={t(($) => $.preferences.running_agent_reply.title)}
      description={t(($) => $.preferences.running_agent_reply.hint)}
      size="select"
    >
      <Select
        items={options}
        value={value}
        onValueChange={(next) => {
          if (next && next !== value) setValue(next as RunningAgentReply);
        }}
      >
        <SelectTrigger
          size="sm"
          className="w-full"
          aria-label={t(($) => $.preferences.running_agent_reply.title)}
        >
          <SelectValue>
            {options.find((option) => option.value === value)?.label}
          </SelectValue>
        </SelectTrigger>
        <SelectContent align="end">
          {options.map((option) => (
            <SelectItem key={option.value} value={option.value}>
              {option.label}
            </SelectItem>
          ))}
        </SelectContent>
      </Select>
    </SettingsRow>
  );
}

function CommentImageLayoutRow() {
  const { t } = useT("settings");
  const value = useCommentComposerStore((s) => s.imageLayout);
  const setValue = useCommentComposerStore((s) => s.setImageLayout);
  const label = t(($) => $.preferences.comment_images.title);
  return (
    <SettingsRow
      anchor="comment-images"
      label={label}
      description={t(($) => $.preferences.comment_images.hint)}
    >
      <div role="group" aria-label={label}>
        <SegmentedToggle<CommentImageLayout>
          value={value}
          onChange={setValue}
          buttonClassName="px-3 py-1 text-label"
          options={[
            ["thumbnails", t(($) => $.preferences.comment_images.thumbnails)],
            ["full", t(($) => $.preferences.comment_images.full)],
          ]}
        />
      </div>
    </SettingsRow>
  );
}

/**
 * What a plain click on an issue card or row opens across the issue views.
 * The hint carries the one non-obvious part: Shift+Click opens the other one,
 * so both stay one click away whichever is chosen.
 */
function IssueOpeningRow() {
  const { t } = useT("settings");
  const value = useIssueOpeningStore((s) => s.openMode);
  const setValue = useIssueOpeningStore((s) => s.setOpenMode);
  const label = t(($) => $.issue.opening.click);
  return (
    <SettingsRow
      anchor="issue-opening"
      label={label}
      description={t(($) => $.issue.opening.hint)}
    >
      <div role="group" aria-label={label}>
        <SegmentedToggle<IssueOpenMode>
          value={value}
          onChange={setValue}
          buttonClassName="px-3 py-1 text-label"
          options={[
            ["page", t(($) => $.issue.opening.page)],
            ["peek", t(($) => $.issue.opening.peek)],
          ]}
        />
      </div>
    </SettingsRow>
  );
}

/**
 * When off, the FAB / overlay never mount and Chat is reachable only from its
 * dedicated tab. A persisted client setting, so it applies immediately.
 */
function FloatingChatRow() {
  const { t } = useT("settings");
  const enabled = useChatStore((s) => s.floatingChatEnabled);
  const setEnabled = useChatStore((s) => s.setFloatingChatEnabled);
  return (
    <SettingsRow
      anchor="chat"
      label={t(($) => $.chat.floating_label)}
    >
      <Switch
        checked={enabled}
        onCheckedChange={(checked) => setEnabled(checked)}
        aria-label={t(($) => $.chat.floating_label)}
      />
    </SettingsRow>
  );
}

/**
 * Which fields each create-issue dialog keeps on its toolbar, as one field ×
 * dialog grid. A field toggled off stays reachable from the dialog's ⋯
 * overflow and re-surfaces while it holds a value, so hiding is never
 * destructive. Quick create supports fewer fields; those cells show a dash.
 */
function IssueFieldsMatrix() {
  const { t } = useT("settings");
  const quickFields = useIssueCreateSettingsStore((s) => s.quickCreateFields);
  const setQuickVisible = useIssueCreateSettingsStore(
    (s) => s.setQuickCreateFieldVisible,
  );
  const manualFields = useIssueCreateSettingsStore((s) => s.manualCreateFields);
  const setManualVisible = useIssueCreateSettingsStore(
    (s) => s.setManualCreateFieldVisible,
  );
  const quickLabel = t(($) => $.preferences.issue_fields.quick);
  const manualLabel = t(($) => $.preferences.issue_fields.manual);

  return (
    <SettingsCard>
      <table className="w-full text-body">
        <thead>
          <tr className="border-b border-surface-border bg-muted/20 text-caption font-medium text-muted-foreground">
            <th scope="col" className="px-4 py-2 text-left font-medium">
              {t(($) => $.preferences.issue_fields.field)}
            </th>
            <th scope="col" className="w-32 px-4 py-2 text-center font-medium">
              {quickLabel}
            </th>
            <th scope="col" className="w-32 px-4 py-2 text-center font-medium">
              {manualLabel}
            </th>
          </tr>
        </thead>
        <tbody className="divide-y divide-surface-border">
          {MANUAL_CREATE_FIELDS.map((field: ManualCreateField) => {
            const fieldLabel = t(($) => $.issue.fields[field]);
            const quickSupported = (QUICK_CREATE_FIELDS as readonly string[]).includes(field);
            return (
              <tr key={field}>
                <th scope="row" className="px-4 py-2.5 text-left font-normal">
                  {fieldLabel}
                </th>
                <td className="px-4 py-2.5 text-center">
                  {quickSupported ? (
                    <Checkbox
                      checked={quickFields.includes(field as QuickCreateField)}
                      onCheckedChange={(checked) =>
                        setQuickVisible(field as QuickCreateField, checked === true)
                      }
                      aria-label={`${fieldLabel} · ${quickLabel}`}
                    />
                  ) : (
                    <span
                      className="text-faint-foreground"
                      aria-label={t(($) => $.preferences.issue_fields.unsupported)}
                      title={t(($) => $.preferences.issue_fields.unsupported)}
                    >
                      —
                    </span>
                  )}
                </td>
                <td className="px-4 py-2.5 text-center">
                  <Checkbox
                    checked={manualFields.includes(field)}
                    onCheckedChange={(checked) =>
                      setManualVisible(field, checked === true)
                    }
                    aria-label={`${fieldLabel} · ${manualLabel}`}
                  />
                </td>
              </tr>
            );
          })}
        </tbody>
      </table>
    </SettingsCard>
  );
}

// Base UI rejects "" as an item value, so route the "no preference" state
// through this sentinel and translate at the wire boundary.
const BROWSER_TZ_VALUE = "__browser__";

/** "UTC+8", "UTC-3:30" — the offset right now, for scanning the list. */
function utcOffset(tz: string): string {
  try {
    const part = new Intl.DateTimeFormat("en-US", {
      timeZone: tz,
      timeZoneName: "shortOffset",
    })
      .formatToParts(new Date())
      .find((p) => p.type === "timeZoneName")?.value;
    return part ? part.replace(/^GMT/, "UTC").replace(/^UTC$/, "UTC+0") : "";
  } catch {
    return "";
  }
}

function TimezoneRow() {
  const { t } = useT("settings");
  const user = useAuthStore((s) => s.user);
  const setUser = useAuthStore((s) => s.setUser);
  const [open, setOpen] = useState(false);
  const stored = user?.timezone ?? null;
  const browser = browserTimezone();
  const value = stored ?? BROWSER_TZ_VALUE;

  // The full IANA list (~600 zones) so a user needing a non-curated zone is
  // not stuck with the common ones — which is why this is searchable.
  const options = useMemo(
    () =>
      timezoneOptions(stored ?? browser).map((tz) => ({
        tz,
        offset: utcOffset(tz),
      })),
    [stored, browser],
  );

  const handleChange = async (next: string) => {
    setOpen(false);
    if (next === value) return;
    const payload = next === BROWSER_TZ_VALUE ? "" : next;
    try {
      const updated = await api.updateMe({ timezone: payload });
      setUser(updated);
    } catch (err) {
      toast.error(
        err instanceof Error && err.message
          ? err.message
          : t(($) => $.preferences.timezone.sync_failed),
      );
    }
  };

  const current = stored ?? browser;
  const suffix = stored ? "" : t(($) => $.preferences.timezone.browser_suffix);

  return (
    <SettingsRow
      anchor="timezone"
      label={t(($) => $.preferences.timezone.title)}
      description={t(($) => $.preferences.timezone.hint)}
      size="select-wide"
    >
      <Popover open={open} onOpenChange={setOpen}>
        <PopoverTrigger
          render={
            <button
              type="button"
              aria-label={t(($) => $.preferences.timezone.title)}
              className="flex h-7 w-full items-center gap-1.5 rounded-md border border-input bg-transparent pl-2.5 pr-2 text-left text-body outline-none transition-colors focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 dark:bg-input/30 dark:hover:bg-input/50"
            />
          }
        >
          <span className="min-w-0 flex-1 truncate font-mono text-caption">
            {current}
            {suffix}
          </span>
          <span className="shrink-0 text-caption text-muted-foreground">
            {utcOffset(current)}
          </span>
          <ChevronsUpDown aria-hidden="true" className="size-4 shrink-0 text-muted-foreground" />
        </PopoverTrigger>
        <PopoverContent align="end" className="w-80 p-0">
          {/* Plain substring matching: fuzzy ranking puts "Antarctica/Vostok"
              next to "Asia/Tokyo" for "tok", which reads as a wrong result. */}
          <Command
            filter={(itemValue, search) =>
              itemValue.toLocaleLowerCase().includes(search.trim().toLocaleLowerCase()) ? 1 : 0
            }
          >
            <CommandInput placeholder={t(($) => $.preferences.timezone.search)} />
            <CommandList>
              <CommandEmpty>{t(($) => $.preferences.timezone.empty)}</CommandEmpty>
              <CommandItem
                value={`${browser} ${t(($) => $.preferences.timezone.browser_suffix)}`}
                data-checked={value === BROWSER_TZ_VALUE}
                onSelect={() => void handleChange(BROWSER_TZ_VALUE)}
              >
                <span className="min-w-0 flex-1 truncate font-mono text-caption">
                  {browser}
                  {t(($) => $.preferences.timezone.browser_suffix)}
                </span>
                <span className="text-caption text-muted-foreground">{utcOffset(browser)}</span>
              </CommandItem>
              {options.map(({ tz, offset }) => (
                <CommandItem
                  key={tz}
                  value={`${tz} ${offset}`}
                  data-checked={value === tz}
                  onSelect={() => void handleChange(tz)}
                >
                  <span className="min-w-0 flex-1 truncate font-mono text-caption">{tz}</span>
                  <span className="text-caption text-muted-foreground">{offset}</span>
                </CommandItem>
              ))}
            </CommandList>
          </Command>
        </PopoverContent>
      </Popover>
    </SettingsRow>
  );
}