Skip to content

Keep a context's star and task history after renaming it#3480

Open
mayteio wants to merge 2 commits into
mainfrom
posthog-code/keep-context-star-and-history-on-rename
Open

Keep a context's star and task history after renaming it#3480
mayteio wants to merge 2 commits into
mainfrom
posthog-code/keep-context-star-and-history-on-rename

Conversation

@mayteio

@mayteio mayteio commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Problem

Renaming a context (channel) in the desktop app had two surprising side effects reported by users:

  1. If the context was starred, the rename dropped it out of the Starred section.
  2. The context's task history appeared to vanish (the feed showed empty).

Neither is data loss — the tasks were still on the server — but both make a rename feel destructive.

Why: both the star and the feed are linked to a context by name, and those links go stale on rename. The star is a per-user desktop shortcut whose ref is the channel's path (its name). The task feed lives on a separate backend "task channel" that the UI resolves by name on every render — so after a rename it no longer matched and a new, empty channel was resolved-or-created.

Changes

  • Star follows the rename — after a successful rename, re-point the shortcut to the new path (create the new one, then remove the stale one) via a new useRepointChannelStar hook.
  • Task history follows the rename — rename the matching backend task channel to the new name using the backend's existing task-channel PATCH endpoint (useRenameBackendChannel, plus a renameTaskChannel method on the API client).
  • No transient empty duplicate — the channels cache now updates optimistically on rename, so the folder name and backend-channel name flip together in the same tick, preventing the feed from briefly resolving-or-creating an empty channel.

Both follow-ups are best-effort and never throw, so a hiccup in either can't turn a successful rename into an error.

How did you test this?

  • Added unit tests (Vitest) and ran them — 10 passing:
    • useRepointChannelStar: moves a starred channel's shortcut to the new path; no-ops when unstarred.
    • useRenameBackendChannel: renames the matching public channel (normalized), cache and fetch-fallback paths, personal/unchanged no-ops, and optimistic rollback on PATCH failure.
  • pnpm --filter @posthog/api-client typecheck and pnpm --filter @posthog/ui typecheck — clean.
  • Biome check on all changed files — clean.
  • Manual verification in the running app not performed in this environment.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code

Renaming a context (channel) silently dropped it from the Starred section and
made its task history look empty. Both stem from name-keyed links that go stale
on rename: the star is a desktop shortcut whose `ref` is the channel path, and
the task feed resolves its backend channel by name.

- Re-point the star's shortcut to the new path after a rename (create new,
  delete stale) via `useRepointChannelStar`.
- Rename the matching backend task channel to the new name so the feed/history
  follows, using the existing task-channel PATCH endpoint, via
  `useRenameBackendChannel` (adds `renameTaskChannel` to the API client).
- Optimistically update the channels cache on rename so the folder name and the
  backend channel name flip together, avoiding a transient empty duplicate.

Both follow-ups are best-effort and never throw, so a hiccup can't turn a
successful rename into an error.

Generated-By: PostHog Code
Task-Id: 795ba9eb-2d2f-432c-a0a3-f0020bb87ccb
@trunk-io

trunk-io Bot commented Jul 15, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit a3a19c2.

The backend task-channel rename and the star re-point are independent (they
touch different caches and resources) and both do their synchronous cache work
before their first await, so running them concurrently doesn't affect the
rename race. Addresses the React Doctor async-parallel finding.

Generated-By: PostHog Code
Task-Id: 795ba9eb-2d2f-432c-a0a3-f0020bb87ccb
@mayteio mayteio marked this pull request as ready for review July 15, 2026 18:01
@mayteio mayteio requested a review from a team July 15, 2026 18:01
@greptile-apps

greptile-apps Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Run rename follow-ups (backend channel +..." | Re-trigger Greptile

Comment on lines +112 to +119
if (!existing && !cached) {
try {
existing = findPublic(await client.getTaskChannels());
} catch {
return;
}
}
if (!existing) return; // No backend channel/history to carry over.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Stale Cache Skips History Rename

When the cache is populated but does not yet contain the old task channel, this path does not fetch the current list and silently returns. This can happen when another tab created the task channel after the last poll; renaming then leaves its history under the old name while the new folder resolves to an empty channel.

Comment on lines +120 to +129

const staleId = existing.id;
const previous = existing;
// Optimistically rename in-cache so the feed's name lookup resolves to
// this channel immediately once the folder name flips.
queryClient.setQueryData<TaskChannel[]>(TASK_CHANNELS_QUERY_KEY, (prev) =>
prev?.map((c) =>
c.id === staleId ? { ...c, name: newNormalized } : c,
),
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Polling Reverts Optimistic Rename

If the 30-second task-channel query is already in flight, its old server response can overwrite this optimistic name before the PATCH completes. useBackendChannel(newName) then sees no match and can resolve or create an empty channel, splitting the feed from the original history.

Comment on lines +139 to +142
queryClient.setQueryData<TaskChannel[]>(
TASK_CHANNELS_QUERY_KEY,
(prev) => prev?.map((c) => (c.id === staleId ? previous : c)),
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Rollback Overwrites Newer State

This rollback restores the captured object without checking whether the same channel has changed since this request began. If another rename or a poll updates that entry before this PATCH fails, the older failure restores the obsolete name and can make the current folder resolve to a different empty feed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant