-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add iOS New Thread home screen widget #3786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import type { | ||
| EnvironmentProject, | ||
| EnvironmentThreadShell, | ||
| } from "@t3tools/client-runtime/state/shell"; | ||
|
|
||
| import { | ||
| makeNewThreadWidgetProps, | ||
| NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT, | ||
| } from "./new-thread-widget"; | ||
|
|
||
| function makeProject(overrides: { | ||
| readonly id: string; | ||
| readonly environmentId?: string; | ||
| readonly title?: string; | ||
| readonly updatedAt?: string; | ||
| }): EnvironmentProject { | ||
| return { | ||
| id: overrides.id, | ||
| environmentId: overrides.environmentId ?? "env-1", | ||
| title: overrides.title ?? overrides.id, | ||
| updatedAt: overrides.updatedAt ?? "2026-07-01T00:00:00.000Z", | ||
| } as unknown as EnvironmentProject; | ||
| } | ||
|
|
||
| function makeThread(overrides: { | ||
| readonly projectId: string; | ||
| readonly environmentId?: string; | ||
| readonly updatedAt: string; | ||
| }): EnvironmentThreadShell { | ||
| return { | ||
| id: `thread-${overrides.projectId}`, | ||
| projectId: overrides.projectId, | ||
| environmentId: overrides.environmentId ?? "env-1", | ||
| updatedAt: overrides.updatedAt, | ||
| } as unknown as EnvironmentThreadShell; | ||
| } | ||
|
|
||
| describe("makeNewThreadWidgetProps", () => { | ||
| it("orders projects by their newest thread activity", () => { | ||
| const props = makeNewThreadWidgetProps( | ||
| [ | ||
| makeProject({ id: "stale", updatedAt: "2026-07-06T00:00:00.000Z" }), | ||
| makeProject({ id: "busy", updatedAt: "2026-07-01T00:00:00.000Z" }), | ||
| ], | ||
| [makeThread({ projectId: "busy", updatedAt: "2026-07-07T00:00:00.000Z" })], | ||
| ); | ||
| expect(props.projects?.map((project) => project.title)).toEqual(["busy", "stale"]); | ||
| }); | ||
|
|
||
| it("falls back to the project's own updatedAt when it has no threads", () => { | ||
| const props = makeNewThreadWidgetProps( | ||
| [ | ||
| makeProject({ id: "old", updatedAt: "2026-07-01T00:00:00.000Z" }), | ||
| makeProject({ id: "fresh", updatedAt: "2026-07-07T00:00:00.000Z" }), | ||
| ], | ||
| [], | ||
| ); | ||
| expect(props.projects?.map((project) => project.title)).toEqual(["fresh", "old"]); | ||
| }); | ||
|
|
||
| it("ignores thread activity from a same-named project in another environment", () => { | ||
| const props = makeNewThreadWidgetProps( | ||
| [ | ||
| makeProject({ id: "proj", environmentId: "env-1", title: "one" }), | ||
| makeProject({ | ||
| id: "proj", | ||
| environmentId: "env-2", | ||
| title: "two", | ||
| updatedAt: "2026-07-02T00:00:00.000Z", | ||
| }), | ||
| ], | ||
| [ | ||
| makeThread({ | ||
| projectId: "proj", | ||
| environmentId: "env-2", | ||
| updatedAt: "2026-07-07T00:00:00.000Z", | ||
| }), | ||
| ], | ||
| ); | ||
| expect(props.projects?.map((project) => project.title)).toEqual(["two", "one"]); | ||
| }); | ||
|
|
||
| it("caps the payload at the widget's sync limit", () => { | ||
| const props = makeNewThreadWidgetProps( | ||
| Array.from({ length: NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT + 5 }, (_, n) => | ||
| makeProject({ | ||
| id: `p${n}`, | ||
| updatedAt: `2026-06-${String(n + 1).padStart(2, "0")}T00:00:00.000Z`, | ||
| }), | ||
| ), | ||
| [], | ||
| ); | ||
| expect(props.projects).toHaveLength(NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT); | ||
| expect(props.projects?.[0]?.title).toBe(`p${NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT + 4}`); | ||
| }); | ||
|
|
||
| it("URL-encodes deep link parameters", () => { | ||
| const props = makeNewThreadWidgetProps( | ||
| [makeProject({ id: "p 1", environmentId: "env&1", title: "T3 & friends" })], | ||
| [], | ||
| ); | ||
| expect(props.projects?.[0]?.deepLink).toBe( | ||
| "/new/draft?environmentId=env%261&projectId=p%201&title=T3%20%26%20friends", | ||
| ); | ||
| }); | ||
|
|
||
| it("returns an empty list when there are no projects", () => { | ||
| expect(makeNewThreadWidgetProps([], []).projects).toEqual([]); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { | ||
| EnvironmentProject, | ||
| EnvironmentThreadShell, | ||
| } from "@t3tools/client-runtime/state/shell"; | ||
|
|
||
| import type { NewThreadWidgetProps } from "../../widgets/NewThread"; | ||
|
|
||
| // Candidate pool synced to the widget. Deliberately larger than any family | ||
| // displays (medium shows 3 rows, large 7): the "pinned projects" widget | ||
| // configuration is per widget instance and only visible inside the widget | ||
| // process, so the layout matches pins against this pool — a pin should still | ||
| // resolve when its project is not among the most recent few. | ||
| export const NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT = 20; | ||
|
|
||
| /** | ||
| * Builds the home-screen widget payload: the most-recently-active projects, | ||
| * each with a deep link into the new-task draft composer for that project. | ||
| * | ||
| * "Recently active" means the newest thread update in the project, falling | ||
| * back to the project's own updatedAt for projects with no threads — so the | ||
| * widget surfaces where the user actually works, not what was created last. | ||
| */ | ||
| export function makeNewThreadWidgetProps( | ||
| projects: ReadonlyArray<EnvironmentProject>, | ||
| threads: ReadonlyArray<EnvironmentThreadShell>, | ||
| ): NewThreadWidgetProps { | ||
| const latestThreadActivity = new Map<string, string>(); | ||
| for (const thread of threads) { | ||
| const key = projectActivityKey(thread.environmentId, thread.projectId); | ||
| const previous = latestThreadActivity.get(key); | ||
| // ISO-8601 timestamps order lexicographically. | ||
| if (previous === undefined || thread.updatedAt > previous) { | ||
| latestThreadActivity.set(key, thread.updatedAt); | ||
| } | ||
| } | ||
|
|
||
| const lastActivity = (project: EnvironmentProject): string => { | ||
| const threadActivity = latestThreadActivity.get( | ||
| projectActivityKey(project.environmentId, project.id), | ||
| ); | ||
| return threadActivity !== undefined && threadActivity > project.updatedAt | ||
| ? threadActivity | ||
| : project.updatedAt; | ||
| }; | ||
|
|
||
| const ordered = [...projects].sort((a, b) => lastActivity(b).localeCompare(lastActivity(a))); | ||
|
|
||
| return { | ||
| projects: ordered.slice(0, NEW_THREAD_WIDGET_PROJECT_SYNC_LIMIT).map((project) => ({ | ||
| title: project.title, | ||
| // Matches the NewTaskSheet > NewTaskDraft linking config in Stack.tsx; | ||
| // the widget layout prefixes the scheme after a safety check. | ||
| deepLink: | ||
| `/new/draft?environmentId=${encodeURIComponent(String(project.environmentId))}` + | ||
| `&projectId=${encodeURIComponent(String(project.id))}` + | ||
| `&title=${encodeURIComponent(project.title)}`, | ||
| })), | ||
| }; | ||
| } | ||
|
|
||
| function projectActivityKey(environmentId: string, projectId: string): string { | ||
| return `${environmentId}\u0000${projectId}`; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { useEffect } from "react"; | ||
| import { Platform } from "react-native"; | ||
|
|
||
| import { useProjects, useThreadShells } from "../../state/entities"; | ||
| import NewThreadWidget from "../../widgets/NewThread"; | ||
| import { makeNewThreadWidgetProps } from "./new-thread-widget"; | ||
|
|
||
| // Last payload written to the widget timeline. Module-level (not React state) | ||
| // so navigator remounts don't rewrite an unchanged payload: every write ends | ||
| // in a WidgetCenter reload, and iOS throttles widgets that reload too often. | ||
| let lastSyncedSignature: string | null = null; | ||
|
|
||
| /** | ||
| * Keeps the NewThread home-screen widget's project shortcuts in sync with the | ||
| * workspace. Writing the snapshot also persists the widget layout to the | ||
| * shared app group, which is what lets the widget render at all — so this | ||
| * must run on every launch, not only when projects change. | ||
| */ | ||
| export function useNewThreadWidgetSync(): void { | ||
| const projects = useProjects(); | ||
| const threads = useThreadShells(); | ||
|
|
||
| useEffect(() => { | ||
| if (Platform.OS !== "ios") { | ||
| return; | ||
| } | ||
| const props = makeNewThreadWidgetProps(projects, threads); | ||
| const signature = JSON.stringify(props); | ||
| if (signature === lastSyncedSignature) { | ||
| return; | ||
| } | ||
| lastSyncedSignature = signature; | ||
| try { | ||
| NewThreadWidget.updateSnapshot(props); | ||
| } catch (error) { | ||
| // Clear the signature so the next data change retries the write. | ||
| lastSyncedSignature = null; | ||
| if (__DEV__) { | ||
| console.warn("[new-thread-widget] snapshot update failed", error); | ||
| } | ||
| } | ||
| }, [projects, threads]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Widget sync won't retryMedium Severity When Reviewed by Cursor Bugbot for commit 19d8807. Configure here. |
||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pins omit low-activity projects
Medium Severity
Pinned project names are resolved only against the widget timeline payload, which
makeNewThreadWidgetPropscaps at the 20 most recently active projects. A pinned title whose project ranks below that cutoff is never inprops.projects, so the widget skips it with no row or fallback beyond the generic new-thread action.Additional Locations (1)
apps/mobile/src/widgets/NewThread.tsx#L93-L104Reviewed by Cursor Bugbot for commit 19d8807. Configure here.