From 1efb8c334209c0d869b4b4c7a30374f8949ff4ac Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Mon, 28 Sep 2026 16:10:13 -0300 Subject: [PATCH] feat(mobile): filter the thread list by provider profile Web can scope the sidebar to one company's provider profile; mobile listed every company's threads with no way to narrow them. - The Home list and the iPad sidebar get a Profile filter next to Environment and Project: All profiles, each profile, and Unassigned when some project has none. It is hidden when no environment defines a profile. - It narrows the Project options to the profile, clears a project selection outside it, and composes with the environment and project filters for threads and queued tasks. - The choice persists in mobile preferences; a profile that no longer exists shows everything. - Read-only: mobile does not create, edit or assign profiles. Profile resolution comes from the shared client runtime, so it matches web. - Android and the iPad sidebar menu show each profile's color; iOS system menus cannot tint individual items. Co-Authored-By: Claude Opus 5.5 (1M context) --- apps/mobile/src/components/AppSymbol.tsx | 2 + .../components/MaterialMenuPopup.android.tsx | 19 +++-- .../src/features/home/HomeHeader.android.tsx | 24 +++++- apps/mobile/src/features/home/HomeHeader.tsx | 26 +++++- .../src/features/home/HomeHeader.types.ts | 6 ++ .../src/features/home/HomeRouteScreen.tsx | 14 +++- apps/mobile/src/features/home/HomeScreen.tsx | 42 ++++++++-- .../home/home-list-filter-menu.test.ts | 84 ++++++++++++++++++- .../features/home/home-list-filter-menu.ts | 79 +++++++++++++++++ .../features/home/home-profile-scope.test.ts | 32 +++++++ .../src/features/home/home-profile-scope.ts | 28 +++++++ .../src/features/home/homeThreadList.test.ts | 76 ++++++++++++++++- .../src/features/home/homeThreadList.ts | 24 +++++- .../features/home/use-home-profile-scope.ts | 62 ++++++++++++++ .../threads/ThreadNavigationSidebar.tsx | 82 ++++++++++++++---- apps/mobile/src/lib/storage.test.ts | 10 +++ .../src/persistence/mobile-preferences.ts | 6 ++ docs/user/project-settings.md | 3 +- docs/user/thread-sidebar.md | 3 +- 19 files changed, 583 insertions(+), 39 deletions(-) create mode 100644 apps/mobile/src/features/home/home-profile-scope.test.ts create mode 100644 apps/mobile/src/features/home/home-profile-scope.ts create mode 100644 apps/mobile/src/features/home/use-home-profile-scope.ts diff --git a/apps/mobile/src/components/AppSymbol.tsx b/apps/mobile/src/components/AppSymbol.tsx index eef05ee94f407..72c5f621bc800 100644 --- a/apps/mobile/src/components/AppSymbol.tsx +++ b/apps/mobile/src/components/AppSymbol.tsx @@ -34,6 +34,7 @@ import IconChevronLeft from "@tabler/icons-react-native/IconChevronLeft"; import IconChevronRight from "@tabler/icons-react-native/IconChevronRight"; import IconChevronUp from "@tabler/icons-react-native/IconChevronUp"; import IconCircle from "@tabler/icons-react-native/IconCircle"; +import IconCircleFilled from "@tabler/icons-react-native/IconCircleFilled"; import IconCircleCheck from "@tabler/icons-react-native/IconCircleCheck"; import IconCircleXFilled from "@tabler/icons-react-native/IconCircleXFilled"; import IconTicket from "@tabler/icons-react-native/IconTicket"; @@ -139,6 +140,7 @@ const ANDROID_ICON_BY_SF_SYMBOL = { checkmark: IconCheck, "checkmark.circle": IconCircleCheck, circle: IconCircle, + "circle.fill": IconCircleFilled, clock: IconClock, timer: IconClock, ticket: IconTicket, diff --git a/apps/mobile/src/components/MaterialMenuPopup.android.tsx b/apps/mobile/src/components/MaterialMenuPopup.android.tsx index 4f9fe3d1ada4e..c611791f7ba3c 100644 --- a/apps/mobile/src/components/MaterialMenuPopup.android.tsx +++ b/apps/mobile/src/components/MaterialMenuPopup.android.tsx @@ -20,6 +20,8 @@ function MenuIcon(props: { readonly name: AppSymbolName; readonly destructive?: boolean; readonly disabled?: boolean; + /** A MenuAction `imageColor`, e.g. a provider profile's color dot. */ + readonly color?: string; }) { const { iconSize } = useAndroidControlSizing(); return ( @@ -32,13 +34,15 @@ function MenuIcon(props: { name={props.name} size={iconSize} type="monochrome" - tintColorClassName={ - props.disabled - ? "accent-icon-subtle" - : props.destructive - ? "accent-danger-foreground" - : "accent-foreground" - } + {...(props.color !== undefined && !props.disabled + ? { tintColor: props.color } + : { + tintColorClassName: props.disabled + ? "accent-icon-subtle" + : props.destructive + ? "accent-danger-foreground" + : "accent-foreground", + })} /> @@ -88,6 +92,7 @@ export function MaterialMenuPopup(props: MaterialMenuPopupProps) { diff --git a/apps/mobile/src/features/home/HomeHeader.android.tsx b/apps/mobile/src/features/home/HomeHeader.android.tsx index 040174b25861b..c99341c32b138 100644 --- a/apps/mobile/src/features/home/HomeHeader.android.tsx +++ b/apps/mobile/src/features/home/HomeHeader.android.tsx @@ -1,6 +1,7 @@ import type { MenuAction } from "@react-native-menu/menu"; import { useCallback, useMemo } from "react"; import { NativeStackScreenOptions } from "../../native/StackHeader"; +import { buildHomeProfileMenuAction, resolveHomeProfileMenuEvent } from "./home-list-filter-menu"; import { MaterialThreadListToolbar } from "./MaterialThreadListToolbar"; import type { HomeHeaderProps } from "./HomeHeader.types"; @@ -15,9 +16,16 @@ export function HomeHeader(props: HomeHeaderProps) { // the filter menu only carries the filters and the "customized" icon state // keys off those alone. const hasCustomListOptions = - props.selectedEnvironmentId !== null || props.selectedProjectKey !== null; + props.selectedEnvironmentId !== null || + props.selectedProjectKey !== null || + props.selectedProfileScope !== "all"; + const profileMenuAction = useMemo( + () => buildHomeProfileMenuAction(props.profiles, props.selectedProfileScope), + [props.profiles, props.selectedProfileScope], + ); const menuActions = useMemo( () => [ + ...(profileMenuAction === null ? [] : [profileMenuAction]), { id: "environment", title: "Environment", @@ -55,11 +63,23 @@ export function HomeHeader(props: HomeHeaderProps) { }, ] satisfies MenuAction[])), ], - [props.environments, props.projects, props.selectedEnvironmentId, props.selectedProjectKey], + [ + profileMenuAction, + props.environments, + props.projects, + props.selectedEnvironmentId, + props.selectedProjectKey, + ], ); const handleMenuAction = useCallback( (event: { nativeEvent: { event: string } }) => { const id = event.nativeEvent.event; + const profileScope = resolveHomeProfileMenuEvent(id, props.profiles); + if (profileScope !== null) { + props.onProfileScopeChange(profileScope); + return; + } + if (id === "environment:all") { props.onEnvironmentChange(null); return; diff --git a/apps/mobile/src/features/home/HomeHeader.tsx b/apps/mobile/src/features/home/HomeHeader.tsx index e64601b9a4585..82001ec502182 100644 --- a/apps/mobile/src/features/home/HomeHeader.tsx +++ b/apps/mobile/src/features/home/HomeHeader.tsx @@ -20,7 +20,9 @@ export function HomeHeader(props: HomeHeaderProps) { // the filter menu only carries the filters and the "customized" icon state // keys off those alone. const hasCustomListOptions = - props.selectedEnvironmentId !== null || props.selectedProjectKey !== null; + props.selectedEnvironmentId !== null || + props.selectedProjectKey !== null || + props.selectedProfileScope !== "all"; const focusSearch = useCallback(() => { searchBarRef.current?.focus(); return searchBarRef.current !== null; @@ -106,6 +108,28 @@ export function HomeHeader(props: HomeHeaderProps) { title="Thread list options" separateBackground > + {props.profiles.length > 0 ? ( + + Profile + props.onProfileScopeChange("all")} + subtitle="Show threads from every profile" + > + All profiles + + {props.profiles.map((profile) => ( + props.onProfileScopeChange(profile.scope)} + > + {profile.label} + + ))} + + ) : null} + Environment ; readonly projects: ReadonlyArray; + /** Empty hides the profile filter. */ + readonly profiles: ReadonlyArray; readonly searchQuery: string; readonly selectedEnvironmentId: EnvironmentId | null; readonly selectedProjectKey: string | null; + readonly selectedProfileScope: ProfileScope; readonly onSearchQueryChange: (query: string) => void; readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void; readonly onProjectChange: (projectKey: string | null) => void; + readonly onProfileScopeChange: (scope: ProfileScope) => void; readonly onOpenEnvironments: () => void; readonly onOpenSettings: () => void; readonly onOpenNotes: () => void; diff --git a/apps/mobile/src/features/home/HomeRouteScreen.tsx b/apps/mobile/src/features/home/HomeRouteScreen.tsx index 7267f054a3b21..aa12395e1b152 100644 --- a/apps/mobile/src/features/home/HomeRouteScreen.tsx +++ b/apps/mobile/src/features/home/HomeRouteScreen.tsx @@ -19,6 +19,7 @@ import { HomeHeader } from "./HomeHeader"; import { useHomeListOptions } from "./home-list-options"; import { useHomeThreadSelection } from "./home-thread-navigation"; import { buildHomeProjectScopes } from "./homeThreadList"; +import { useHomeProfileScope } from "./use-home-profile-scope"; import { usePendingTaskListActions } from "./usePendingTaskListActions"; import { useThreadListActions } from "./useThreadListActions"; import { getConnectionAwareBrandHeaderOptions } from "./WorkspaceConnectionTitle"; @@ -80,18 +81,23 @@ export function HomeRouteScreen() { const { options: listOptions, setSelectedEnvironmentId } = useHomeListOptions(availableEnvironmentIds); const selectedEnvironmentId = listOptions.selectedEnvironmentId; + const profileScope = useHomeProfileScope(projects); + const profileProjectKeys = profileScope.scopedProjectKeys; const [selectedProjectKey, setSelectedProjectKey] = useState(null); + // Project choices stay inside the chosen profile; the effect below drops a + // selection the profile or environment no longer contains. const projectFilterOptions = useMemo( () => buildHomeProjectScopes({ projects, environmentId: selectedEnvironmentId, + profileProjectKeys, projectGroupingMode: listOptions.projectGroupingMode, }).map((scope) => ({ key: scope.key, label: scope.title, })), - [listOptions.projectGroupingMode, projects, selectedEnvironmentId], + [listOptions.projectGroupingMode, profileProjectKeys, projects, selectedEnvironmentId], ); useEffect(() => { if ( @@ -169,11 +175,14 @@ export function HomeRouteScreen() { navigation.navigate("SettingsSheet", { screen: "SettingsContent", @@ -250,6 +259,9 @@ export function HomeRouteScreen() { projectGroupingMode={listOptions.projectGroupingMode} projects={projects} projectSortOrder={listOptions.projectSortOrder} + profileProjectKeys={profileProjectKeys} + selectedProfileLabel={profileScope.selectedLabel} + selectedProfileScope={profileScope.scope} savedConnectionsById={savedConnectionsById} searchQuery={searchQuery} selectedEnvironmentId={selectedEnvironmentId} diff --git a/apps/mobile/src/features/home/HomeScreen.tsx b/apps/mobile/src/features/home/HomeScreen.tsx index 75be079f0de88..1c9c72a99b868 100644 --- a/apps/mobile/src/features/home/HomeScreen.tsx +++ b/apps/mobile/src/features/home/HomeScreen.tsx @@ -63,6 +63,7 @@ import { useThreadListV2ShelfPreferences } from "../threads/use-thread-list-v2-s import type { HomeListFilterMenuEnvironment } from "./home-list-filter-menu"; import { buildHomeProjectScopes, + resolveHomeListProjectRefs, sortHomeProjectScopes, type HomeProjectSortOrder, } from "./homeThreadList"; @@ -84,6 +85,12 @@ interface HomeScreenProps { readonly searchQuery: string; readonly selectedEnvironmentId: EnvironmentId | null; readonly selectedProjectKey: string | null; + /** Project keys the profile filter shows, or null for every project. */ + readonly profileProjectKeys: ReadonlySet | null; + /** The chosen profile's name (or "Unassigned"), null when showing every profile. */ + readonly selectedProfileLabel: string | null; + /** The chosen profile scope; keys list resets, since names can repeat. */ + readonly selectedProfileScope: string; readonly projectSortOrder: HomeProjectSortOrder; readonly projectGroupingMode: SidebarProjectGroupingMode; readonly onSearchQueryChange: (query: string) => void; @@ -325,9 +332,15 @@ export function HomeScreen(props: HomeScreenProps) { buildHomeProjectScopes({ projects: props.projects, environmentId: props.selectedEnvironmentId, + profileProjectKeys: props.profileProjectKeys, projectGroupingMode: props.projectGroupingMode, }), - [props.projectGroupingMode, props.projects, props.selectedEnvironmentId], + [ + props.profileProjectKeys, + props.projectGroupingMode, + props.projects, + props.selectedEnvironmentId, + ], ); const hasSearchQuery = props.searchQuery.trim().length > 0; const projectByKey = useMemo(() => { @@ -386,16 +399,25 @@ export function HomeScreen(props: HomeScreenProps) { ), [v2ScopeProjects], ); + const v2ProjectRefs = useMemo( + () => + resolveHomeListProjectRefs({ + selectedScope: v2ScopedProjectGroup, + scopes: projectScopes, + profileProjectKeys: props.profileProjectKeys, + }), + [projectScopes, props.profileProjectKeys, v2ScopedProjectGroup], + ); const v2ScopedProjectKeys = useMemo( () => - v2ScopedProjectGroup === null + v2ProjectRefs === null ? null : new Set( - v2ScopedProjectGroup.projectRefs.map((projectRef) => + v2ProjectRefs.map((projectRef) => scopedProjectKey(projectRef.environmentId, projectRef.projectId), ), ), - [v2ScopedProjectGroup], + [v2ProjectRefs], ); // Thread List v2 (beta): one flat list in creation order, no grouping. // Settled threads collapse into a recency tail below the card block. @@ -456,7 +478,7 @@ export function HomeScreen(props: HomeScreenProps) { const [settledVisibleCount, setSettledVisibleCount] = useState( THREAD_LIST_V2_SETTLED_INITIAL_COUNT, ); - const settledResetKey = `${props.selectedEnvironmentId ?? "all"}:${v2ProjectScopeKey ?? "all"}:${props.searchQuery.trim()}`; + const settledResetKey = `${props.selectedEnvironmentId ?? "all"}:${v2ProjectScopeKey ?? "all"}:${props.selectedProfileScope}:${props.searchQuery.trim()}`; const lastSettledResetKeyRef = useRef(settledResetKey); if (lastSettledResetKeyRef.current !== settledResetKey) { lastSettledResetKeyRef.current = settledResetKey; @@ -613,7 +635,7 @@ export function HomeScreen(props: HomeScreenProps) { pendingOrder, threads: props.threads.filter((thread) => thread.archivedAt === null), environmentId: props.selectedEnvironmentId, - projectRefs: v2ScopedProjectGroup === null ? null : v2ScopedProjectGroup.projectRefs, + projectRefs: v2ProjectRefs, searchQuery: props.searchQuery, matchedThreadKeys, settlementEnvironmentIds, @@ -639,7 +661,7 @@ export function HomeScreen(props: HomeScreenProps) { props.selectedEnvironmentId, props.threads, matchedThreadKeys, - v2ScopedProjectGroup, + v2ProjectRefs, ]); // Re-partition the moment the earliest snooze expires (clamped to the // signed-32-bit setTimeout range; far-future wakes re-arm at the clamp). @@ -966,6 +988,12 @@ export function HomeScreen(props: HomeScreenProps) { detail="Choose another project or create a new task." variant={Platform.OS === "android" ? "plain" : undefined} /> + ) : props.selectedProfileLabel !== null ? ( + ) : selectedEnvironmentLabel ? ( = [ + { scope: profileScopeForId(acme), label: "Acme", color: "#ea580c" }, + { scope: "unassigned", label: "Unassigned", color: null }, +]; describe("buildHomeListFilterMenu", () => { it("adds a project scope submenu that selects and clears the same scope as the chips", () => { @@ -11,10 +24,13 @@ describe("buildHomeListFilterMenu", () => { { key: "environment-1:project-1", label: "Codething" }, { key: "environment-1:project-2", label: "Website" }, ], + profiles: [], selectedEnvironmentId: null, selectedProjectKey: "environment-1:project-1", + selectedProfileScope: "all", onEnvironmentChange: vi.fn(), onProjectChange, + onProfileScopeChange: vi.fn(), }); const projectMenu = menu.items.find( @@ -36,4 +52,70 @@ describe("buildHomeListFilterMenu", () => { expect(onProjectChange).toHaveBeenNthCalledWith(1, null); expect(onProjectChange).toHaveBeenNthCalledWith(2, "environment-1:project-2"); }); + + it("shows a profile submenu only when profiles exist, and selects scopes from it", () => { + const onProfileScopeChange = vi.fn(); + const baseProps = { + environments: [], + projects: [], + selectedEnvironmentId: null, + selectedProjectKey: null, + selectedProfileScope: profileScopeForId(acme), + onEnvironmentChange: vi.fn(), + onProjectChange: vi.fn(), + onProfileScopeChange, + }; + expect( + buildHomeListFilterMenu({ ...baseProps, profiles: [] }).items.map((item) => item.title), + ).toEqual(["Environment"]); + + const menu = buildHomeListFilterMenu({ ...baseProps, profiles }); + const profileMenu = menu.items[0]; + expect(profileMenu).toMatchObject({ + type: "submenu", + title: "Profile", + items: [ + { title: "All profiles", state: "off" }, + { title: "Acme", state: "on" }, + { title: "Unassigned", state: "off" }, + ], + }); + if (profileMenu?.type !== "submenu") throw new Error("Expected profile submenu"); + profileMenu.items[0]?.onPress(); + profileMenu.items[2]?.onPress(); + expect(onProfileScopeChange).toHaveBeenNthCalledWith(1, "all"); + expect(onProfileScopeChange).toHaveBeenNthCalledWith(2, "unassigned"); + }); + + it("builds a MenuView profile submenu with color dots whose events round-trip", () => { + expect(buildHomeProfileMenuAction([], "all")).toBeNull(); + const action = buildHomeProfileMenuAction(profiles, "unassigned"); + expect(action?.subactions).toEqual([ + expect.objectContaining({ id: "profile-scope:all", state: "off" }), + { + id: "profile-scope:profile:acme", + title: "Acme", + state: "off", + image: "circle.fill", + imageColor: "#ea580c", + }, + { id: "profile-scope:unassigned", title: "Unassigned", state: "on" }, + ]); + + expect(resolveHomeProfileMenuEvent("profile-scope:all", profiles)).toBe("all"); + expect(resolveHomeProfileMenuEvent("profile-scope:profile:acme", profiles)).toBe( + "profile:acme", + ); + expect(resolveHomeProfileMenuEvent("profile-scope:unassigned", profiles)).toBe("unassigned"); + expect(resolveHomeProfileMenuEvent("profile-scope:profile:deleted", profiles)).toBeNull(); + expect(resolveHomeProfileMenuEvent("project:environment-1:project-1", profiles)).toBeNull(); + }); + + it("keeps a profile named All apart from the all-profiles choice", () => { + const named = [ + { scope: profileScopeForId(ProviderProfileId.make("all")), label: "All", color: null }, + ] as const; + expect(resolveHomeProfileMenuEvent("profile-scope:all", named)).toBe("all"); + expect(resolveHomeProfileMenuEvent("profile-scope:profile:all", named)).toBe("profile:all"); + }); }); diff --git a/apps/mobile/src/features/home/home-list-filter-menu.ts b/apps/mobile/src/features/home/home-list-filter-menu.ts index ac53e427d21f3..c389bfc5bd401 100644 --- a/apps/mobile/src/features/home/home-list-filter-menu.ts +++ b/apps/mobile/src/features/home/home-list-filter-menu.ts @@ -1,4 +1,6 @@ +import type { ProfileScope } from "@t3tools/client-runtime/state/profile-scope"; import type { EnvironmentId } from "@t3tools/contracts"; +import type { MenuAction } from "@react-native-menu/menu"; export interface HomeListFilterMenuEnvironment { readonly environmentId: EnvironmentId; @@ -10,6 +12,13 @@ export interface HomeListFilterMenuProject { readonly label: string; } +/** One non-"All profiles" choice in the profile filter: a profile, or "Unassigned". */ +export interface HomeListFilterMenuProfile { + readonly scope: Exclude; + readonly label: string; + readonly color: string | null; +} + type HomeListFilterMenuAction = { readonly type: "action"; readonly title: string; @@ -32,13 +41,40 @@ export interface HomeListFilterMenu { export function buildHomeListFilterMenu(props: { readonly environments: ReadonlyArray; readonly projects: ReadonlyArray; + /** Empty hides the profile filter. */ + readonly profiles: ReadonlyArray; readonly selectedEnvironmentId: EnvironmentId | null; readonly selectedProjectKey: string | null; + readonly selectedProfileScope: ProfileScope; readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void; readonly onProjectChange: (projectKey: string | null) => void; + readonly onProfileScopeChange: (scope: ProfileScope) => void; }): HomeListFilterMenu { const items: Array = []; + // Native UIMenus tint every icon, so profile colors only show in the MenuView menus. + if (props.profiles.length > 0) { + items.push({ + type: "submenu", + title: "Profile", + items: [ + { + type: "action", + title: "All profiles", + subtitle: "Show threads from every profile", + state: props.selectedProfileScope === "all" ? "on" : "off", + onPress: () => props.onProfileScopeChange("all"), + }, + ...props.profiles.map((profile) => ({ + type: "action" as const, + title: profile.label, + state: props.selectedProfileScope === profile.scope ? ("on" as const) : ("off" as const), + onPress: () => props.onProfileScopeChange(profile.scope), + })), + ], + }); + } + items.push({ type: "submenu", title: "Environment", @@ -89,3 +125,46 @@ export function buildHomeListFilterMenu(props: { items, }; } + +// Distinct from the `profile:` scope values the ids carry after it. +const PROFILE_MENU_ACTION_PREFIX = "profile-scope:"; + +/** + * The profile submenu for MenuView-based filter menus (Android, and the iPad sidebar's + * non-native header), with each profile's color as a dot. Null when there are no profiles. + */ +export function buildHomeProfileMenuAction( + profiles: ReadonlyArray, + selectedScope: ProfileScope, +): MenuAction | null { + if (profiles.length === 0) return null; + return { + id: "profile", + title: "Profile", + subactions: [ + { + id: `${PROFILE_MENU_ACTION_PREFIX}all`, + title: "All profiles", + subtitle: "Show threads from every profile", + state: selectedScope === "all" ? "on" : "off", + }, + ...profiles.map((profile): MenuAction => ({ + id: `${PROFILE_MENU_ACTION_PREFIX}${profile.scope}`, + title: profile.label, + state: selectedScope === profile.scope ? "on" : "off", + ...(profile.color === null ? {} : { image: "circle.fill", imageColor: profile.color }), + })), + ], + }; +} + +/** The scope a `buildHomeProfileMenuAction` event picks, or null for other menu events. */ +export function resolveHomeProfileMenuEvent( + event: string, + profiles: ReadonlyArray, +): ProfileScope | null { + if (!event.startsWith(PROFILE_MENU_ACTION_PREFIX)) return null; + const scope = event.slice(PROFILE_MENU_ACTION_PREFIX.length); + if (scope === "all") return "all"; + return profiles.find((profile) => profile.scope === scope)?.scope ?? null; +} diff --git a/apps/mobile/src/features/home/home-profile-scope.test.ts b/apps/mobile/src/features/home/home-profile-scope.test.ts new file mode 100644 index 0000000000000..030af3d989e86 --- /dev/null +++ b/apps/mobile/src/features/home/home-profile-scope.test.ts @@ -0,0 +1,32 @@ +import type { ProfileScopeProfile } from "@t3tools/client-runtime/state/profile-scope"; +import { ProviderProfileId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { buildHomeProfileFilterOptions } from "./home-profile-scope"; + +const acme = ProviderProfileId.make("acme"); +const globex = ProviderProfileId.make("globex"); +const profiles: ReadonlyArray = [ + { id: acme, name: "Acme", color: "#ea580c" }, + { id: globex, name: "Globex", color: null }, +]; + +describe("buildHomeProfileFilterOptions", () => { + it("hides the filter when no environment defines a profile", () => { + expect(buildHomeProfileFilterOptions({ profiles: [], hasUnassignedProjects: true })).toEqual( + [], + ); + }); + + it("lists each profile with its color and adds Unassigned only when a project has none", () => { + expect(buildHomeProfileFilterOptions({ profiles, hasUnassignedProjects: false })).toEqual([ + { scope: "profile:acme", label: "Acme", color: "#ea580c" }, + { scope: "profile:globex", label: "Globex", color: null }, + ]); + expect( + buildHomeProfileFilterOptions({ profiles, hasUnassignedProjects: true }).map( + (option) => option.scope, + ), + ).toEqual(["profile:acme", "profile:globex", "unassigned"]); + }); +}); diff --git a/apps/mobile/src/features/home/home-profile-scope.ts b/apps/mobile/src/features/home/home-profile-scope.ts new file mode 100644 index 0000000000000..7ecfcc87007d2 --- /dev/null +++ b/apps/mobile/src/features/home/home-profile-scope.ts @@ -0,0 +1,28 @@ +import { + profileScopeForId, + type ProfileScopeProfile, +} from "@t3tools/client-runtime/state/profile-scope"; + +import type { HomeListFilterMenuProfile } from "./home-list-filter-menu"; + +/** + * The thread list's profile choices besides "All profiles": each profile, then "Unassigned" when + * some project has none. Empty when no environment defines a profile, which hides the filter. A + * stored "Unassigned" with nothing left in it resolves to all profiles (`resolveProfileScope`), + * so the menu never shows a choice it no longer lists. + */ +export function buildHomeProfileFilterOptions(input: { + readonly profiles: ReadonlyArray; + readonly hasUnassignedProjects: boolean; +}): ReadonlyArray { + if (input.profiles.length === 0) return []; + const options: HomeListFilterMenuProfile[] = input.profiles.map((profile) => ({ + scope: profileScopeForId(profile.id), + label: profile.name, + color: profile.color, + })); + if (input.hasUnassignedProjects) { + options.push({ scope: "unassigned", label: "Unassigned", color: null }); + } + return options; +} diff --git a/apps/mobile/src/features/home/homeThreadList.test.ts b/apps/mobile/src/features/home/homeThreadList.test.ts index 98b32edfd748b..8e72a4cc185a3 100644 --- a/apps/mobile/src/features/home/homeThreadList.test.ts +++ b/apps/mobile/src/features/home/homeThreadList.test.ts @@ -5,7 +5,11 @@ import type { import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { buildHomeProjectScopes, sortHomeProjectScopes } from "./homeThreadList"; +import { + buildHomeProjectScopes, + resolveHomeListProjectRefs, + sortHomeProjectScopes, +} from "./homeThreadList"; function makeProject( input: Partial & Pick, @@ -48,6 +52,76 @@ function makeThread( } describe("home project scopes", () => { + it("keeps only the profile's projects, even inside a shared repository group", () => { + const localEnvironmentId = EnvironmentId.make("environment-local"); + const remoteEnvironmentId = EnvironmentId.make("environment-remote"); + const repositoryIdentity = { + canonicalKey: "github.com/acme/app", + locator: { + source: "git-remote" as const, + remoteName: "origin", + remoteUrl: "git@github.com:acme/app.git", + }, + }; + const projects = [ + makeProject({ + environmentId: localEnvironmentId, + id: ProjectId.make("app-local"), + title: "app", + repositoryIdentity, + }), + makeProject({ + environmentId: remoteEnvironmentId, + id: ProjectId.make("app-remote"), + title: "app", + repositoryIdentity, + }), + makeProject({ + environmentId: localEnvironmentId, + id: ProjectId.make("website"), + title: "website", + }), + ]; + const profileProjectKeys = new Set([ + "environment-remote:app-remote", + "environment-local:website", + ]); + + const scopes = buildHomeProjectScopes({ + projects, + environmentId: null, + profileProjectKeys, + projectGroupingMode: "repository", + }); + expect(scopes.map((scope) => scope.projectRefs.map((ref) => ref.projectId))).toEqual( + expect.arrayContaining([[ProjectId.make("app-remote")], [ProjectId.make("website")]]), + ); + expect(scopes).toHaveLength(2); + + // Composes with the environment filter. + expect( + buildHomeProjectScopes({ + projects, + environmentId: localEnvironmentId, + profileProjectKeys, + projectGroupingMode: "repository", + }).map((scope) => scope.title), + ).toEqual(["website"]); + + const website = scopes.find((scope) => scope.title === "website") ?? null; + expect( + resolveHomeListProjectRefs({ selectedScope: website, scopes, profileProjectKeys }), + ).toEqual([{ environmentId: localEnvironmentId, projectId: ProjectId.make("website") }]); + expect( + resolveHomeListProjectRefs({ selectedScope: null, scopes, profileProjectKeys })?.map( + (ref) => ref.projectId, + ), + ).toEqual(expect.arrayContaining([ProjectId.make("app-remote"), ProjectId.make("website")])); + expect( + resolveHomeListProjectRefs({ selectedScope: null, scopes, profileProjectKeys: null }), + ).toBeNull(); + }); + it("builds one v2 scope for the same repository across environments", () => { const localEnvironmentId = EnvironmentId.make("environment-local"); const remoteEnvironmentId = EnvironmentId.make("environment-remote"); diff --git a/apps/mobile/src/features/home/homeThreadList.ts b/apps/mobile/src/features/home/homeThreadList.ts index 0c85972e43e52..0300bdb556f4d 100644 --- a/apps/mobile/src/features/home/homeThreadList.ts +++ b/apps/mobile/src/features/home/homeThreadList.ts @@ -1,3 +1,4 @@ +import { profileScopeProjectKey } from "@t3tools/client-runtime/state/profile-scope"; import { buildProjectGroups } from "@t3tools/client-runtime/state/project-grouping"; import type { EnvironmentProject, @@ -43,10 +44,16 @@ function getProjectSortTimestamp( export function buildHomeProjectScopes(input: { readonly projects: ReadonlyArray; readonly environmentId: EnvironmentId | null; + /** Project keys the profile filter shows, or null for every project. */ + readonly profileProjectKeys?: ReadonlySet | null; readonly projectGroupingMode: SidebarProjectGroupingMode; }): ReadonlyArray { + const profileProjectKeys = input.profileProjectKeys ?? null; const projects = input.projects.filter( - (project) => input.environmentId === null || project.environmentId === input.environmentId, + (project) => + (input.environmentId === null || project.environmentId === input.environmentId) && + (profileProjectKeys === null || + profileProjectKeys.has(profileScopeProjectKey(project.environmentId, project.id))), ); return buildProjectGroups({ projects, @@ -65,6 +72,21 @@ export function buildHomeProjectScopes(input: { }); } +/** + * The projects whose threads the list shows: the selected project scope's, else every + * scope's while a profile filters the list, else null for no project filter. Scopes must + * come from `buildHomeProjectScopes` with the same profile keys. + */ +export function resolveHomeListProjectRefs(input: { + readonly selectedScope: HomeProjectScope | null; + readonly scopes: ReadonlyArray; + readonly profileProjectKeys: ReadonlySet | null; +}): ReadonlyArray | null { + if (input.selectedScope !== null) return input.selectedScope.projectRefs; + if (input.profileProjectKeys === null) return null; + return input.scopes.flatMap((scope) => scope.projectRefs); +} + export function sortHomeProjectScopes(input: { readonly scopes: ReadonlyArray; readonly threads: ReadonlyArray; diff --git a/apps/mobile/src/features/home/use-home-profile-scope.ts b/apps/mobile/src/features/home/use-home-profile-scope.ts new file mode 100644 index 0000000000000..0baa28dbd2f5d --- /dev/null +++ b/apps/mobile/src/features/home/use-home-profile-scope.ts @@ -0,0 +1,62 @@ +import { useAtomSet, useAtomValue } from "@effect/atom-react"; +import { + buildProjectProfileMap, + collectProfiles, + resolveProfileScope, + scopedProjectKeysForProfile, + type ProfileScope, +} from "@t3tools/client-runtime/state/profile-scope"; +import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; +import { AsyncResult } from "effect/unstable/reactivity"; +import { useCallback, useMemo } from "react"; + +import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; +import { environmentServerConfigsAtom } from "../../state/server"; +import { buildHomeProfileFilterOptions } from "./home-profile-scope"; + +/** + * The thread list's provider profile filter, shared by the compact Home list and the + * iPad sidebar. The choice is a device preference, so it survives restarts and layout + * changes; a profile no connected environment defines shows every thread. + */ +export function useHomeProfileScope(projects: ReadonlyArray) { + const serverConfigs = useAtomValue(environmentServerConfigsAtom); + const settingsByEnvironment = useMemo( + () => new Map([...serverConfigs].map(([id, config]) => [id, config.settings] as const)), + [serverConfigs], + ); + // Mobile has no primary environment; the first environment that defines an id names it. + const profiles = useMemo( + () => collectProfiles([...settingsByEnvironment], null), + [settingsByEnvironment], + ); + const projectProfiles = useMemo( + () => buildProjectProfileMap(projects, settingsByEnvironment), + [projects, settingsByEnvironment], + ); + const preferences = useAtomValue(mobilePreferencesAtom); + const savePreferences = useAtomSet(updateMobilePreferencesAtom); + const storedScope = AsyncResult.isSuccess(preferences) + ? (preferences.value.threadListProfileScope ?? "all") + : "all"; + const hasUnassignedProjects = useMemo( + () => [...projectProfiles.values()].some((profileId) => profileId === null), + [projectProfiles], + ); + const scope = resolveProfileScope(storedScope, profiles, hasUnassignedProjects); + const options = useMemo( + () => buildHomeProfileFilterOptions({ profiles, hasUnassignedProjects }), + [hasUnassignedProjects, profiles], + ); + const scopedProjectKeys = useMemo( + () => scopedProjectKeysForProfile(scope, projectProfiles), + [projectProfiles, scope], + ); + const setScope = useCallback( + (next: ProfileScope) => savePreferences({ threadListProfileScope: next }), + [savePreferences], + ); + const selectedLabel = + scope === "all" ? null : (options.find((option) => option.scope === scope)?.label ?? null); + return { options, scope, scopedProjectKeys, selectedLabel, setScope } as const; +} diff --git a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx index a6362f205a16f..5a1d783a3a215 100644 --- a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx +++ b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx @@ -41,8 +41,13 @@ import { useSavedRemoteConnections } from "../../state/use-remote-environment-re import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands"; import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts"; import { useHomeListOptions } from "../home/home-list-options"; -import { buildHomeListFilterMenu } from "../home/home-list-filter-menu"; -import { buildHomeProjectScopes } from "../home/homeThreadList"; +import { + buildHomeListFilterMenu, + buildHomeProfileMenuAction, + resolveHomeProfileMenuEvent, +} from "../home/home-list-filter-menu"; +import { buildHomeProjectScopes, resolveHomeListProjectRefs } from "../home/homeThreadList"; +import { useHomeProfileScope } from "../home/use-home-profile-scope"; import { SwipeableScrollGateProvider, useSwipeableScrollGate } from "../home/thread-swipe-actions"; import { usePendingTaskListActions } from "../home/usePendingTaskListActions"; import { useThreadListActions } from "../home/useThreadListActions"; @@ -206,15 +211,18 @@ function ThreadNavigationSidebarPane( () => new Set(threadSearch.matches.map(threadSearchMatchKey)), [threadSearch.matches], ); + const profileScope = useHomeProfileScope(projects); + const profileProjectKeys = profileScope.scopedProjectKeys; const [selectedProjectKey, setSelectedProjectKey] = useState(null); const projectScopes = useMemo( () => buildHomeProjectScopes({ projects, environmentId: options.selectedEnvironmentId, + profileProjectKeys, projectGroupingMode: options.projectGroupingMode, }), - [options.projectGroupingMode, options.selectedEnvironmentId, projects], + [options.projectGroupingMode, options.selectedEnvironmentId, profileProjectKeys, projects], ); const projectFilterOptions = useMemo( () => @@ -254,16 +262,25 @@ function ThreadNavigationSidebarPane( setSelectedProjectKey(null); } }, [projectFilterOptions, selectedProjectKey]); + const listProjectRefs = useMemo( + () => + resolveHomeListProjectRefs({ + selectedScope: selectedProjectScope, + scopes: projectScopes, + profileProjectKeys, + }), + [profileProjectKeys, projectScopes, selectedProjectScope], + ); const selectedProjectRefs = useMemo( () => - selectedProjectScope === null + listProjectRefs === null ? null : new Set( - selectedProjectScope.projectRefs.map((projectRef) => + listProjectRefs.map((projectRef) => scopedProjectKey(projectRef.environmentId, projectRef.projectId), ), ), - [selectedProjectScope], + [listProjectRefs], ); const projectByKey = useMemo(() => { const map = new Map(); @@ -280,7 +297,7 @@ function ThreadNavigationSidebarPane( const [settledVisibleCount, setSettledVisibleCount] = useState( THREAD_LIST_V2_SETTLED_INITIAL_COUNT, ); - const settledResetKey = `${options.selectedEnvironmentId ?? "all"}:${selectedProjectKey ?? "all"}:${props.searchQuery.trim()}`; + const settledResetKey = `${options.selectedEnvironmentId ?? "all"}:${selectedProjectKey ?? "all"}:${profileScope.scope}:${props.searchQuery.trim()}`; const lastSettledResetKeyRef = useRef(settledResetKey); if (lastSettledResetKeyRef.current !== settledResetKey) { lastSettledResetKeyRef.current = settledResetKey; @@ -433,7 +450,7 @@ function ThreadNavigationSidebarPane( pendingOrder, threads: threads.filter((thread) => thread.archivedAt === null), environmentId: options.selectedEnvironmentId, - projectRefs: selectedProjectScope === null ? null : selectedProjectScope.projectRefs, + projectRefs: listProjectRefs, searchQuery: props.searchQuery, matchedThreadKeys, settlementEnvironmentIds, @@ -460,7 +477,7 @@ function ThreadNavigationSidebarPane( settlementEnvironmentIds, snoozeEnvironmentIds, threads, - selectedProjectScope, + listProjectRefs, ]); // Re-partition the moment the earliest snooze expires (clamped to the // signed-32-bit setTimeout range; far-future wakes re-arm at the clamp). @@ -531,8 +548,13 @@ function ThreadNavigationSidebarPane( snoozeEnvironmentIds, threadListV2Layout, ]); + const profileMenuAction = useMemo( + () => buildHomeProfileMenuAction(profileScope.options, profileScope.scope), + [profileScope.options, profileScope.scope], + ); const listMenuActions = useMemo( () => [ + ...(profileMenuAction === null ? [] : [profileMenuAction]), { id: "environment", title: "Environment", @@ -575,11 +597,16 @@ function ThreadNavigationSidebarPane( }, ] satisfies MenuAction[])), ], - [environments, options, projectFilterOptions, selectedProjectKey], + [environments, options, profileMenuAction, projectFilterOptions, selectedProjectKey], ); const handleListMenuAction = useCallback( ({ nativeEvent }: { readonly nativeEvent: { readonly event: string } }) => { const event = nativeEvent.event; + const nextProfileScope = resolveHomeProfileMenuEvent(event, profileScope.options); + if (nextProfileScope !== null) { + profileScope.setScope(nextProfileScope); + return; + } if (event === "environment:all") { setSelectedEnvironmentId(null); return; @@ -603,7 +630,13 @@ function ThreadNavigationSidebarPane( return; } }, - [environments, projectFilterOptions, setSelectedEnvironmentId], + [ + environments, + profileScope.options, + profileScope.setScope, + projectFilterOptions, + setSelectedEnvironmentId, + ], ); const [measuredHeaderHeight, setMeasuredHeaderHeight] = useState(null); @@ -873,9 +906,12 @@ function ThreadNavigationSidebarPane( unsnoozeThread, ], ); - // The list ignores sort/group options, so only the environment and project - // filters can light the "customized" state. - const filterCustomized = options.selectedEnvironmentId !== null || selectedProjectKey !== null; + // The list ignores sort/group options, so only the profile, environment and + // project filters can light the "customized" state. + const filterCustomized = + options.selectedEnvironmentId !== null || + selectedProjectKey !== null || + profileScope.scope !== "all"; const filterIcon = filterCustomized ? "line.3.horizontal.decrease.circle.fill" : "line.3.horizontal.decrease.circle"; @@ -884,12 +920,24 @@ function ThreadNavigationSidebarPane( buildHomeListFilterMenu({ environments, projects: projectFilterOptions, + profiles: profileScope.options, selectedEnvironmentId: options.selectedEnvironmentId, selectedProjectKey, + selectedProfileScope: profileScope.scope, onEnvironmentChange: setSelectedEnvironmentId, onProjectChange: setSelectedProjectKey, + onProfileScopeChange: profileScope.setScope, }), - [environments, options, projectFilterOptions, selectedProjectKey, setSelectedEnvironmentId], + [ + environments, + options, + profileScope.options, + profileScope.scope, + profileScope.setScope, + projectFilterOptions, + selectedProjectKey, + setSelectedEnvironmentId, + ], ); const nativeHeaderItems = useMemo( () => @@ -921,7 +969,9 @@ function ThreadNavigationSidebarPane( : "No matching threads" : selectedProjectScope !== null ? `No threads in ${selectedProjectScope.title}` - : "No threads yet"} + : profileScope.selectedLabel !== null + ? `No threads in ${profileScope.selectedLabel}` + : "No threads yet"} ); diff --git a/apps/mobile/src/lib/storage.test.ts b/apps/mobile/src/lib/storage.test.ts index 78a165c5ff1d8..50d149bd9b29d 100644 --- a/apps/mobile/src/lib/storage.test.ts +++ b/apps/mobile/src/lib/storage.test.ts @@ -232,6 +232,16 @@ describe("mobile connection storage", () => { expect(fallback.updatedAt).toEqual(expect.any(Number)); }); + it("persists the thread list profile filter and drops invalid values", async () => { + await expect(savePreferencesPatch({ threadListProfileScope: "acme" })).resolves.toEqual({ + threadListProfileScope: "acme", + }); + await expect(loadPreferences()).resolves.toEqual({ threadListProfileScope: "acme" }); + + mocks.setPreferencesJson(JSON.stringify({ baseFontSize: 17, threadListProfileScope: 3 }), 10); + await expect(loadPreferences()).resolves.toEqual({ baseFontSize: 17 }); + }); + it("persists thread list shelf expansion preferences", async () => { await expect( savePreferencesPatch({ diff --git a/apps/mobile/src/persistence/mobile-preferences.ts b/apps/mobile/src/persistence/mobile-preferences.ts index 9514efdff424b..beedf61114125 100644 --- a/apps/mobile/src/persistence/mobile-preferences.ts +++ b/apps/mobile/src/persistence/mobile-preferences.ts @@ -43,6 +43,8 @@ export interface Preferences { /** Fresh keys reset both shelves to collapsed when users update. */ readonly threadListSettledShelfExpanded?: boolean; readonly threadListSnoozedShelfExpanded?: boolean; + /** Thread list profile filter: "all", "unassigned", or a provider profile id. */ + readonly threadListProfileScope?: string; } export class MobilePreferencesLoadError extends Schema.TaggedError()( @@ -103,6 +105,7 @@ function sanitizePreferences(parsed: Preferences): Preferences { modelFavorites?: Preferences["modelFavorites"]; threadListSettledShelfExpanded?: boolean; threadListSnoozedShelfExpanded?: boolean; + threadListProfileScope?: string; } = {}; if (typeof parsed.liveActivitiesEnabled === "boolean") { @@ -187,6 +190,9 @@ function sanitizePreferences(parsed: Preferences): Preferences { if (typeof parsed.threadListSnoozedShelfExpanded === "boolean") { preferences.threadListSnoozedShelfExpanded = parsed.threadListSnoozedShelfExpanded; } + if (typeof parsed.threadListProfileScope === "string" && parsed.threadListProfileScope) { + preferences.threadListProfileScope = parsed.threadListProfileScope; + } return preferences; } diff --git a/docs/user/project-settings.md b/docs/user/project-settings.md index 4266423cbb85b..c47bf9fa836fd 100644 --- a/docs/user/project-settings.md +++ b/docs/user/project-settings.md @@ -88,7 +88,8 @@ Profiles belong to an environment, like its providers, so the page edits the env the top. Create the profile on each machine you use, with the same name when you first create it: the sidebar then treats them as one company, even if you rename one later. You can also choose a project's profile with **Provider profile** in **Settings → General** with the project selected. -On mobile, the same choice is on the **New threads** server settings page. +On mobile, the same choice is on the **New threads** server settings page, and the thread list can +be filtered by profile. ## Storage cleanup diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index 128d8b95604f6..fd97bb9095466 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -141,7 +141,8 @@ new thread. Choosing a profile yourself while a thread is open keeps your choice Collapsing the sidebar leaves a strip of icons: the profile, new thread, search, notes, automations, and one icon per pinned or active thread, marked with its status. Hover an icon for the -thread's title. With **All profiles**, a colored edge shows each thread's profile. +thread's title. With **All profiles**, a colored edge shows each thread's profile. On mobile, filter +the thread list by profile from the filter menu. ## Find and reference work