# Code coverage report for pages/Settings.jsx

# All files / pages Settings.jsx

81.63% Statements 40/49
78.18% Branches 43/55
50% Functions 7/14
83.33% Lines 40/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347

    1x

    30x
    30x
    30x
    30x
    30x
    30x
    30x
    30x
    30x
    30x

    30x

    30x
    15x
    7x
    7x
    7x
    7x
    7x
    7x

    30x
    23x

    15x

    15x

    30x

    1x
    1x

    30x
    1x

    1x
    1x

    1x
    1x

    1x

    30x

    30x
    8x

    22x

    1x

    352x

     import { useState, useEffect } from "react"
    import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
    import { ArrowLeft, Bell, MessageCircle, Globe, Save, Copy, Check, User, Sun, Moon, Palette } from "lucide-react"
    import { Link } from "react-router-dom"
    import { profileApi } from "../api/profile"
    import { useTheme } from "../contexts/ThemeContext"
    import Navigation from "../components/Navigation"

    const TIMEZONES = [
      { value: "Europe/Moscow", label: "Москва (UTC+3)" },
      { value: "Europe/Kaliningrad", label: "Калининград (UTC+2)" },
      { value: "Europe/Samara", label: "Самара (UTC+4)" },
      { value: "Asia/Yekaterinburg", label: "Екатеринбург (UTC+5)" },
      { value: "Asia/Omsk", label: "Омск (UTC+6)" },
      { value: "Asia/Krasnoyarsk", label: "Красноярск (UTC+7)" },
      { value: "Asia/Irkutsk", label: "Иркутск (UTC+8)" },
      { value: "Asia/Yakutsk", label: "Якутск (UTC+9)" },
      { value: "Asia/Vladivostok", label: "Владивосток (UTC+10)" },
      { value: "Asia/Magadan", label: "Магадан (UTC+11)" },
      { value: "Asia/Kamchatka", label: "Камчатка (UTC+12)" },
      { value: "Asia/Tokyo", label: "Токио (UTC+9)" },
      { value: "Europe/London", label: "Лондон (UTC+0)" },
      { value: "Europe/Berlin", label: "Берлин (UTC+1)" },
      { value: "America/New_York", label: "Нью-Йорк (UTC-5)" },
      { value: "America/Los_Angeles", label: "Лос-Анджелес (UTC-8)" },
    ]

    export default function Settings() {
      const queryClient = useQueryClient()
      const { theme, toggleTheme } = useTheme()
      const [copied, setCopied] = useState(false)
      const [username, setUsername] = useState("")
      const [chatId, setChatId] = useState("")
      const [notificationsEnabled, setNotificationsEnabled] = useState(true)
      const [timezone, setTimezone] = useState("Europe/Moscow")
      const [morningTime, setMorningTime] = useState("09:00")
      const [eveningTime, setEveningTime] = useState("21:00")
      const [hasChanges, setHasChanges] = useState(false)

      const { data: profile, isLoading } = useQuery({
        queryKey: ["profile"],
        queryFn: profileApi.get,
      })

      useEffect(() => {
        if (profile) {
          setUsername(profile.username || "")
          setChatId(profile.telegram_chat_id?.toString() || "")
          setNotificationsEnabled(profile.notifications_enabled ?? true)
          setTimezone(profile.timezone || "Europe/Moscow")
          setMorningTime(profile.morning_reminder_time || "09:00")
          setEveningTime(profile.evening_reminder_time || "21:00")
        }
      }, [profile])

      useEffect(() => {
        if (profile) {
          const changed =
            username !== (profile.username || "") ||
            (chatId !== (profile.telegram_chat_id?.toString() || "")) ||
            notificationsEnabled !== (profile.notifications_enabled ?? true) ||
            timezone !== (profile.timezone || "Europe/Moscow") ||
            morningTime !== (profile.morning_reminder_time || "09:00") ||
            eveningTime !== (profile.evening_reminder_time || "21:00")
          setHasChanges(changed)
        }
      }, [username, chatId, notificationsEnabled, timezone, morningTime, eveningTime, profile])

      const mutation = useMutation({
        mutationFn: profileApi.update,
        onSuccess: () => {
          queryClient.invalidateQueries({ queryKey: ["profile"] })
          setHasChanges(false)
        },
      })

      const handleSave = () => {
        const data = {
          notifications_enabled: notificationsEnabled,
          timezone: timezone,
          morning_reminder_time: morningTime,
          evening_reminder_time: eveningTime,
        }

        Eif (username && username !== profile?.username) {
          data.username = username
        }

        Eif (chatId) {
          data.telegram_chat_id = parseInt(chatId, 10)
        }

        mutation.mutate(data)
      }

      const copyInstruction = () => {
        navigator.clipboard.writeText("@pulse_tracking_bot")
        setCopied(true)
        setTimeout(() => setCopied(false), 2000)
      }

      if (isLoading) {
        return (
          <div className="min-h-screen bg-surface-50 dark:bg-gray-950 flex items-center justify-center">
            <div className="w-10 h-10 border-4 border-primary-500 border-t-transparent rounded-full animate-spin" />
          </div>
        )
      }

      return (
        <div className="min-h-screen bg-surface-50 dark:bg-gray-950 pb-24 transition-colors duration-300">
          {/* Header */}
          <header className="bg-white/80 dark:bg-gray-900/80 backdrop-blur-xl sticky top-0 z-40 border-b border-gray-100 dark:border-gray-800">
            <div className="max-w-lg mx-auto px-4 py-4 flex items-center gap-3">
              <Link to="/" className="p-2 -ml-2 text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white rounded-xl hover:bg-gray-100 dark:hover:bg-gray-800">
                <ArrowLeft size={20} />
              </Link>
              <h1 className="text-xl font-bold dark:text-white">Настройки</h1>
            </div>
          </header>

          <main className="max-w-lg mx-auto px-4 py-6 space-y-6">

            {/* Theme Section */}
            <section className="bg-white dark:bg-gray-900 rounded-2xl p-4 shadow-sm dark:shadow-none border border-transparent dark:border-gray-800">
              <div className="flex items-center gap-3 mb-4">
                <div className="w-10 h-10 rounded-xl bg-violet-100 dark:bg-violet-900/30 flex items-center justify-center">
                  <Palette className="text-violet-600 dark:text-violet-400" size={20} />
                </div>
                <div>
                  <h2 className="font-semibold dark:text-white">Оформление</h2>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Выбери тему приложения</p>
                </div>
              </div>

              <button
                onClick={toggleTheme}
                className="w-full flex items-center justify-between p-4 bg-gray-50 dark:bg-gray-800 rounded-xl transition-all hover:bg-gray-100 dark:hover:bg-gray-700"
              >
                <div className="flex items-center gap-3">
                  {theme === "dark" ? (
                    <Moon className="text-primary-500" size={22} />
                  ) : (
                    <Sun className="text-amber-500" size={22} />
                  )}
                  <span className="font-medium text-gray-900 dark:text-white">
                    {theme === "dark" ? "Тёмная тема" : "Светлая тема"}
                  </span>
                </div>
                <div className="relative">
                  <div className={`w-14 h-8 rounded-full transition-colors duration-300 ${theme === "dark" ? "bg-primary-500" : "bg-gray-300"}`}>
                    <div className={`absolute top-1 w-6 h-6 bg-white rounded-full shadow-md transition-transform duration-300 flex items-center justify-center ${theme === "dark" ? "translate-x-7" : "translate-x-1"}`}>
                      {theme === "dark" ? (
                        <Moon className="text-primary-600" size={14} />
                      ) : (
                        <Sun className="text-amber-500" size={14} />
                      )}
                    </div>
                  </div>
                </div>
              </button>
            </section>

            {/* Profile Section */}
            <section className="bg-white dark:bg-gray-900 rounded-2xl p-4 shadow-sm dark:shadow-none border border-transparent dark:border-gray-800">
              <div className="flex items-center gap-3 mb-4">
                <div className="w-10 h-10 rounded-xl bg-green-100 dark:bg-green-900/30 flex items-center justify-center">
                  <User className="text-green-600 dark:text-green-400" size={20} />
                </div>
                <div>
                  <h2 className="font-semibold dark:text-white">Профиль</h2>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Основная информация</p>
                </div>
              </div>

              <div>
                <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1.5">
                  Имя пользователя
                </label>
                <input
                  type="text"
                  value={username}
                  onChange={(e) => setUsername(e.target.value)}
                  placeholder="Ваше имя"
                  className="input"
                />
              </div>
            </section>

            {/* Telegram Section */}
            <section className="bg-white dark:bg-gray-900 rounded-2xl p-4 shadow-sm dark:shadow-none border border-transparent dark:border-gray-800">
              <div className="flex items-center gap-3 mb-4">
                <div className="w-10 h-10 rounded-xl bg-blue-100 dark:bg-blue-900/30 flex items-center justify-center">
                  <MessageCircle className="text-blue-600 dark:text-blue-400" size={20} />
                </div>
                <div>
                  <h2 className="font-semibold dark:text-white">Telegram</h2>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Получай уведомления в Telegram</p>
                </div>
              </div>

              <div className="space-y-4">
                <div className="p-3 bg-blue-50 dark:bg-blue-900/20 rounded-xl">
                  <p className="text-sm text-blue-800 dark:text-blue-300 mb-2">
                    1. Напиши <code className="bg-blue-100 dark:bg-blue-800 px-1 rounded">/start</code> боту в Telegram
                  </p>
                  <button
                    onClick={copyInstruction}
                    className="flex items-center gap-2 text-sm font-medium text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300"
                  >
                    {copied ? <Check size={16} /> : <Copy size={16} />}
                    {copied ? "Скопировано!" : "@pulse_tracking_bot"}
                  </button>
                  <p className="text-sm text-blue-800 dark:text-blue-300 mt-2">
                    2. Скопируй Chat ID из ответа бота и вставь ниже
                  </p>
                </div>

                <div>
                  <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1.5">
                    Chat ID
                  </label>
                  <input
                    type="text"
                    value={chatId}
                    onChange={(e) => setChatId(e.target.value.replace(/\D/g, ""))}
                    placeholder="Например: 123456789"
                    className="input"
                  />
                </div>
              </div>
            </section>

            {/* Notifications Section */}
            <section className="bg-white dark:bg-gray-900 rounded-2xl p-4 shadow-sm dark:shadow-none border border-transparent dark:border-gray-800">
              <div className="flex items-center gap-3 mb-4">
                <div className="w-10 h-10 rounded-xl bg-orange-100 dark:bg-orange-900/30 flex items-center justify-center">
                  <Bell className="text-orange-600 dark:text-orange-400" size={20} />
                </div>
                <div>
                  <h2 className="font-semibold dark:text-white">Уведомления</h2>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Настрой ежедневные уведомления</p>
                </div>
              </div>

              <div className="space-y-4">
                <label className="flex items-center justify-between p-3 bg-gray-50 dark:bg-gray-800 rounded-xl cursor-pointer">
                  <span className="text-sm font-medium dark:text-white">Включить уведомления</span>
                  <div className="relative">
                    <input
                      type="checkbox"
                      checked={notificationsEnabled}
                      onChange={(e) => setNotificationsEnabled(e.target.checked)}
                      className="sr-only peer"
                    />
                    <div className="w-11 h-6 bg-gray-300 dark:bg-gray-600 rounded-full peer peer-checked:bg-primary-500 transition-colors"></div>
                    <div className="absolute left-1 top-1 w-4 h-4 bg-white rounded-full peer-checked:translate-x-5 transition-transform"></div>
                  </div>
                </label>

                {notificationsEnabled && (
                  <>
                    <div className="flex items-center gap-3 p-3 bg-yellow-50 dark:bg-yellow-900/20 rounded-xl">
                      <Sun className="text-yellow-600 dark:text-yellow-400" size={20} />
                      <div className="flex-1">
                        <label className="block text-sm font-medium text-gray-700 dark:text-gray-300">
                          Утреннее уведомление
                        </label>
                        <p className="text-xs text-gray-500 dark:text-gray-400">Задачи и привычки на сегодня</p>
                      </div>
                      <input
                        type="time"
                        value={morningTime}
                        onChange={(e) => setMorningTime(e.target.value)}
                        className="px-3 py-1.5 border border-gray-200 dark:border-gray-700 dark:bg-gray-800 dark:text-white rounded-lg text-sm"
                      />
                    </div>

                    <div className="flex items-center gap-3 p-3 bg-indigo-50 dark:bg-indigo-900/20 rounded-xl">
                      <Moon className="text-indigo-600 dark:text-indigo-400" size={20} />
                      <div className="flex-1">
                        <label className="block text-sm font-medium text-gray-700 dark:text-gray-300">
                          Вечернее уведомление
                        </label>
                        <p className="text-xs text-gray-500 dark:text-gray-400">Итоги дня: выполнено / осталось</p>
                      </div>
                      <input
                        type="time"
                        value={eveningTime}
                        onChange={(e) => setEveningTime(e.target.value)}
                        className="px-3 py-1.5 border border-gray-200 dark:border-gray-700 dark:bg-gray-800 dark:text-white rounded-lg text-sm"
                      />
                    </div>
                  </>
                )}
              </div>
            </section>

            {/* Timezone Section */}
            <section className="bg-white dark:bg-gray-900 rounded-2xl p-4 shadow-sm dark:shadow-none border border-transparent dark:border-gray-800">
              <div className="flex items-center gap-3 mb-4">
                <div className="w-10 h-10 rounded-xl bg-purple-100 dark:bg-purple-900/30 flex items-center justify-center">
                  <Globe className="text-purple-600 dark:text-purple-400" size={20} />
                </div>
                <div>
                  <h2 className="font-semibold dark:text-white">Часовой пояс</h2>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Для корректных напоминаний</p>
                </div>
              </div>

              <select
                value={timezone}
                onChange={(e) => setTimezone(e.target.value)}
                className="input"
              >
                {TIMEZONES.map((tz) => (
                  <option key={tz.value} value={tz.value}>
                    {tz.label}
                  </option>
                ))}
              </select>
            </section>

            {/* Save Button */}
            {hasChanges && (
              <button
                onClick={handleSave}
                disabled={mutation.isPending}
                className="btn btn-primary w-full flex items-center justify-center gap-2"
              >
                <Save size={18} />
                {mutation.isPending ? "Сохраняем..." : "Сохранить изменения"}
              </button>
            )}

            {mutation.isSuccess && !hasChanges && (
              <div className="p-3 rounded-xl bg-green-50 dark:bg-green-900/20 text-green-700 dark:text-green-400 text-sm text-center">
                ✅ Настройки сохранены
              </div>
            )}
          </main>

          <Navigation />
        </div>
      )
    }