|
| 1 | +import { scopedProjectKey, scopeProjectRef } from "@t3tools/client-runtime/environment"; |
1 | 2 | import { settlePromise } from "@t3tools/client-runtime/state/runtime"; |
| 3 | +import { ChevronDownIcon } from "lucide-react"; |
2 | 4 | import { useState } from "react"; |
3 | 5 |
|
| 6 | +import { getClientSettings } from "../../hooks/useSettings"; |
| 7 | +import { normalizeSearchText } from "../../lib/utils"; |
| 8 | +import { useProjects } from "../../state/entities"; |
| 9 | +import { useEnvironments } from "../../state/environments"; |
4 | 10 | import { |
5 | 11 | hasDesktopNotifications, |
6 | 12 | hasNotificationSound, |
7 | 13 | NOTIFICATION_MODE_LABELS, |
8 | 14 | unlockNotificationAudio, |
9 | 15 | } from "../../threadNotifications"; |
| 16 | +import { ProjectFavicon } from "../ProjectFavicon"; |
| 17 | +import { Button } from "../ui/button"; |
| 18 | +import { |
| 19 | + Menu, |
| 20 | + MenuCheckboxItem, |
| 21 | + MenuItem, |
| 22 | + MenuPopup, |
| 23 | + MenuSearchInput, |
| 24 | + MenuSeparator, |
| 25 | + MenuTrigger, |
| 26 | +} from "../ui/menu"; |
10 | 27 | import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; |
11 | | -import { SettingsRow } from "./settingsLayout"; |
| 28 | +import { SettingResetButton, SettingsRow } from "./settingsLayout"; |
12 | 29 | import { searchableSetting } from "./settingsSearch"; |
13 | 30 | import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings"; |
14 | 31 |
|
@@ -80,3 +97,110 @@ export function NotificationSettings() { |
80 | 97 | /> |
81 | 98 | ); |
82 | 99 | } |
| 100 | + |
| 101 | +export function NotificationProjectsSettings() { |
| 102 | + const selected = useScopedSettings((settings) => settings.notificationProjects); |
| 103 | + const updateSettings = useUpdateScopedSettings(); |
| 104 | + const projects = useProjects(); |
| 105 | + const { environments } = useEnvironments(); |
| 106 | + const [query, setQuery] = useState(""); |
| 107 | + const environmentLabels = new Map( |
| 108 | + environments.map((environment) => [environment.environmentId, environment.label]), |
| 109 | + ); |
| 110 | + const showEnvironment = new Set(projects.map((project) => project.environmentId)).size > 1; |
| 111 | + const normalizedQuery = normalizeSearchText(query); |
| 112 | + const options = projects |
| 113 | + .map((project) => ({ |
| 114 | + project, |
| 115 | + key: scopedProjectKey(scopeProjectRef(project.environmentId, project.id)), |
| 116 | + environmentLabel: showEnvironment |
| 117 | + ? (environmentLabels.get(project.environmentId) ?? null) |
| 118 | + : null, |
| 119 | + })) |
| 120 | + .filter( |
| 121 | + (option) => |
| 122 | + normalizedQuery === "" || |
| 123 | + normalizeSearchText(`${option.project.title} ${option.environmentLabel ?? ""}`).includes( |
| 124 | + normalizedQuery, |
| 125 | + ), |
| 126 | + ) |
| 127 | + .toSorted((left, right) => left.project.title.localeCompare(right.project.title)); |
| 128 | + const summary = |
| 129 | + selected.length === 0 |
| 130 | + ? "All projects" |
| 131 | + : `${selected.length} ${selected.length === 1 ? "project" : "projects"}`; |
| 132 | + const setProject = (key: string, checked: boolean) => { |
| 133 | + const current = getClientSettings().notificationProjects.filter((entry) => entry !== key); |
| 134 | + updateSettings({ notificationProjects: checked ? [...current, key] : current }); |
| 135 | + }; |
| 136 | + |
| 137 | + return ( |
| 138 | + <SettingsRow |
| 139 | + title={searchableSetting("notification-projects").title} |
| 140 | + description="Only alert for threads in these projects." |
| 141 | + resetAction={ |
| 142 | + selected.length === 0 ? null : ( |
| 143 | + <SettingResetButton |
| 144 | + label="notification projects" |
| 145 | + onClick={() => updateSettings({ notificationProjects: [] })} |
| 146 | + /> |
| 147 | + ) |
| 148 | + } |
| 149 | + control={ |
| 150 | + <Menu |
| 151 | + onOpenChange={(open) => { |
| 152 | + if (!open) setQuery(""); |
| 153 | + }} |
| 154 | + > |
| 155 | + <MenuTrigger |
| 156 | + render={ |
| 157 | + <Button |
| 158 | + variant="outline" |
| 159 | + size="sm" |
| 160 | + className="w-full justify-between sm:w-56" |
| 161 | + aria-label={`Notification projects: ${summary}`} |
| 162 | + /> |
| 163 | + } |
| 164 | + > |
| 165 | + {summary} |
| 166 | + <ChevronDownIcon aria-hidden /> |
| 167 | + </MenuTrigger> |
| 168 | + <MenuPopup align="end"> |
| 169 | + <MenuSearchInput |
| 170 | + autoFocus |
| 171 | + value={query} |
| 172 | + onChange={(event) => setQuery(event.currentTarget.value)} |
| 173 | + placeholder="Search projects" |
| 174 | + aria-label="Search projects" |
| 175 | + /> |
| 176 | + <MenuCheckboxItem |
| 177 | + checked={selected.length === 0} |
| 178 | + onCheckedChange={() => updateSettings({ notificationProjects: [] })} |
| 179 | + > |
| 180 | + All projects |
| 181 | + </MenuCheckboxItem> |
| 182 | + <MenuSeparator /> |
| 183 | + {options.length === 0 ? <MenuItem disabled>No projects found</MenuItem> : null} |
| 184 | + {options.map(({ project, key, environmentLabel }) => ( |
| 185 | + <MenuCheckboxItem |
| 186 | + key={key} |
| 187 | + checked={selected.includes(key)} |
| 188 | + onCheckedChange={(checked) => setProject(key, checked)} |
| 189 | + > |
| 190 | + <span className="flex min-w-0 items-center gap-2"> |
| 191 | + <ProjectFavicon project={project} className="size-3.5" /> |
| 192 | + <span className="min-w-0 flex-1 truncate">{project.title}</span> |
| 193 | + {environmentLabel ? ( |
| 194 | + <span className="shrink-0 text-xs text-muted-foreground"> |
| 195 | + {environmentLabel} |
| 196 | + </span> |
| 197 | + ) : null} |
| 198 | + </span> |
| 199 | + </MenuCheckboxItem> |
| 200 | + ))} |
| 201 | + </MenuPopup> |
| 202 | + </Menu> |
| 203 | + } |
| 204 | + /> |
| 205 | + ); |
| 206 | +} |
0 commit comments