Skip to content

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

Go
7.0 KB
Raw Blame
/**
 * Notification preferences subscreen. Inbox groups and system delivery
 * settings are backed by an optimistic PATCH /api/notification-preferences.
 *
 * Copy mirrors packages/views/settings/components/notifications-tab.tsx.
 * Group labels describe the same server-side semantics as web; divergent
 * labels would violate behavioral parity (apps/mobile/CLAUDE.md).
 */
import { ActivityIndicator, ScrollView, View } from "react-native";
import { useQuery } from "@tanstack/react-query";
import type {
  NotificationGroupKey,
  NotificationPreferences,
} from "@multica/core/types";
import { notificationPreferenceValue } from "@multica/core/notification-preferences/patch";
import { Text } from "@/components/ui/text";
import { Switch } from "@/components/ui/switch";
import { Separator } from "@/components/ui/separator";
import { useWorkspaceStore } from "@/data/workspace-store";
import { notificationPreferenceOptions } from "@/data/queries/notification-preferences";
import { useUpdateNotificationPreferences } from "@/data/mutations/notification-preferences";
import { useT } from "@/lib/i18n";

const INBOX_GROUPS: {
  key: Exclude<NotificationGroupKey, "system_notifications" | "chat_replies" | "squad_handoffs">;
  labelKey: string;
  descriptionKey?: string;
}[] = [
  {
    key: "assignments",
    labelKey: "groups.assignments",
    descriptionKey: "groups.assignments_description",
  },
  {
    key: "status_changes",
    labelKey: "groups.status_changes",
  },
  {
    key: "comments",
    labelKey: "groups.comments",
    descriptionKey: "groups.comments_description",
  },
  {
    key: "mentions",
    labelKey: "groups.mentions",
    descriptionKey: "groups.mentions_description",
  },
  {
    key: "reactions",
    labelKey: "groups.reactions",
    descriptionKey: "groups.reactions_description",
  },
  {
    key: "updates",
    labelKey: "groups.updates",
    descriptionKey: "groups.updates_description",
  },
  {
    key: "new_issues",
    labelKey: "groups.new_issues",
    descriptionKey: "groups.new_issues_description",
  },
  {
    key: "agent_activity",
    labelKey: "groups.agent_activity",
    descriptionKey: "groups.agent_activity_description",
  },
];

export default function NotificationsSettingsScreen() {
  const wsId = useWorkspaceStore((s) => s.currentWorkspaceId);
  const { t } = useT("settings");
  const { data, isLoading, error } = useQuery(
    notificationPreferenceOptions(wsId),
  );
  const mutation = useUpdateNotificationPreferences();

  const preferences: NotificationPreferences = data?.preferences ?? {};

  const onToggle = (key: NotificationGroupKey, enabled: boolean) => {
    const next: NotificationPreferences = {
      ...preferences,
      [key]: enabled ? "all" : "muted",
    };
    mutation.mutate(next);
  };

  const systemEnabled = preferences.system_notifications !== "muted";

  if (isLoading) {
    return (
      <View className="flex-1 items-center justify-center bg-background">
        <ActivityIndicator />
      </View>
    );
  }

  if (error) {
    return (
      <View className="flex-1 items-center justify-center bg-background px-6">
        <Text className="text-sm text-destructive text-center">
          {t("errors.load_failed")}
        </Text>
      </View>
    );
  }

  return (
    <ScrollView
      className="flex-1 bg-background"
      contentContainerClassName="px-4 py-4 gap-6"
    >
      <Section
        title={t("groups.inbox_notifications")}
      >
        {INBOX_GROUPS.map((group, idx) => {
          const enabled =
            notificationPreferenceValue(preferences, group.key) !== "muted";
          const isLast = idx === INBOX_GROUPS.length - 1;
          return (
            <View key={group.key}>
              <View className="flex-row items-center px-4 py-3 gap-3">
                <View className="flex-1">
                  <Text className="text-base font-medium text-foreground">
                    {t(group.labelKey)}
                  </Text>
                  {group.descriptionKey ? (
                    <Text className="text-xs text-muted-foreground mt-0.5">
                      {t(group.descriptionKey)}
                    </Text>
                  ) : null}
                </View>
                <Switch
                  checked={enabled}
                  onCheckedChange={(checked) => onToggle(group.key, checked)}
                />
              </View>
              {group.key === "comments" && enabled && (
                <>
                  <Separator />
                  <View className="flex-row items-center pl-8 pr-4 py-3 gap-3">
                    <View className="flex-1">
                      <Text className="text-base font-medium text-foreground">
                        {t("groups.squad_handoffs")}
                      </Text>
                      <Text className="text-xs text-muted-foreground mt-0.5">
                        {t("groups.squad_handoffs_description")}
                      </Text>
                    </View>
                    <Switch
                      checked={
                        notificationPreferenceValue(preferences, "squad_handoffs") !== "muted"
                      }
                      onCheckedChange={(checked) => onToggle("squad_handoffs", checked)}
                    />
                  </View>
                </>
              )}
              {!isLast ? <Separator /> : null}
            </View>
          );
        })}
      </Section>

      <Section title={t("groups.system")}>
        <View className="flex-row items-center px-4 py-3 gap-3">
          <View className="flex-1">
            <Text className="text-base font-medium text-foreground">
              {t("groups.system_notifications")}
            </Text>
          </View>
          <Switch
            checked={systemEnabled}
            onCheckedChange={(checked) => onToggle("system_notifications", checked)}
          />
        </View>
        {systemEnabled && (
          <>
            <Separator />
            <View className="flex-row items-center pl-8 pr-4 py-3 gap-3">
              <View className="flex-1">
                <Text className="text-base font-medium text-foreground">
                  {t("groups.chat_replies")}
                </Text>
              </View>
              <Switch
                checked={preferences.chat_replies !== "muted"}
                onCheckedChange={(checked) => onToggle("chat_replies", checked)}
              />
            </View>
          </>
        )}
      </Section>
    </ScrollView>
  );
}

function Section({
  title,
  description,
  children,
}: {
  title: string;
  description?: string;
  children: React.ReactNode;
}) {
  return (
    <View className="gap-2">
      <View className="px-1">
        <Text className="text-xs uppercase tracking-wider text-muted-foreground">
          {title}
        </Text>
        {description ? (
          <Text className="text-xs text-muted-foreground mt-1">
            {description}
          </Text>
        ) : null}
      </View>
      <View className="rounded-md border border-border bg-card overflow-hidden">
        {children}
      </View>
    </View>
  );
}