Skip to content

Commit 88575c0

Browse files
authored
[buzz-app] Fix the pull request icon flash in chats (#877)
1 parent c3f6068 commit 88575c0

6 files changed

Lines changed: 541 additions & 62 deletions

File tree

‎src/bundled/projects/ResourcePicker.tsx‎

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
value,
1919
values,
2020
type Entity,
21+
type ProjectHome,
2122
} from "../../features/projects/destinations";
2223
import {
2324
entityDtag,
@@ -38,8 +39,12 @@ type Item = {
3839
type Items =
3940
| { status: "loading" | "error" }
4041
| { status: "ready"; items: Item[]; truncated: boolean };
42+
type Home =
43+
| { status: "loading"; cached: ProjectHome | undefined }
44+
| { status: "error" }
45+
| ProjectHome;
4146

42-
/** Mounted per destination, so it owns the channel→project mapping and list reads.
47+
/** Mounted per destination; the session retains the channel→project mapping.
4348
* The composer owns the inserted link; this tool only proposes a verified one. */
4449
export function ResourcePicker({
4550
session,
@@ -48,9 +53,10 @@ export function ResourcePicker({
4853
insertResource,
4954
}: ComposerToolProps) {
5055
const [attempt, setAttempt] = useState(0);
51-
const [home, setHome] = useState<
52-
"loading" | "none" | "ambiguous" | "error" | Entity
53-
>("loading");
56+
const [home, setHome] = useState<Home>(() => ({
57+
status: "loading",
58+
cached: session.projects.peekHome(channelId),
59+
}));
5460
const [open, setOpen] = useState(false);
5561
const [opening, setOpening] = useState(0);
5662
const [search, setSearch] = useState("");
@@ -62,18 +68,21 @@ export function ResourcePicker({
6268
const trigger = useRef<HTMLButtonElement>(null);
6369
const controls = useRef<HTMLFieldSetElement>(null);
6470
const searchInput = useRef<HTMLElement>(null);
65-
const project = typeof home === "object" ? home : undefined;
66-
// biome-ignore lint/correctness/useExhaustiveDependencies: attempt is explicit retry.
71+
const pending = home.status === "loading";
72+
const visibleHome = home.status === "loading" ? home.cached : home;
73+
const project = home.status === "home" ? home.project : undefined;
6774
useEffect(() => {
6875
const controller = new AbortController();
69-
setHome("loading");
76+
setHome({
77+
status: "loading",
78+
cached: attempt === 0 ? session.projects.peekHome(channelId) : undefined,
79+
});
7080
session.projects.home(channelId, controller.signal).then(
7181
(result) => {
72-
if (!controller.signal.aborted)
73-
setHome(result.status === "home" ? result.project : result.status);
82+
if (!controller.signal.aborted) setHome(result);
7483
},
7584
() => {
76-
if (!controller.signal.aborted) setHome("error");
85+
if (!controller.signal.aborted) setHome({ status: "error" });
7786
},
7887
);
7988
return () => controller.abort();
@@ -178,7 +187,7 @@ export function ResourcePicker({
178187
wrap: true,
179188
});
180189
const highlighted = shown.find(({ item }) => item.id === highlight.active);
181-
if (home === "none") return null;
190+
if (visibleHome?.status === "none") return null;
182191
// A deleted or retargeted item must not become an accepted link.
183192
const choose = (item: Item) => {
184193
validation.current?.abort();
@@ -221,7 +230,7 @@ export function ResourcePicker({
221230
const label = "Add issue or pull request";
222231
return (
223232
<PopoverRoot
224-
open={open && !disabled && home !== "loading"}
233+
open={open && !disabled && !pending}
225234
onOpenChange={(next) => {
226235
setOpen(next);
227236
validation.current?.abort();
@@ -233,40 +242,44 @@ export function ResourcePicker({
233242
}
234243
}}
235244
>
245+
{pending && (
246+
<span role="status" className="sr-only">
247+
Loading project…
248+
</span>
249+
)}
236250
<fieldset
237251
ref={controls}
238252
disabled={disabled}
253+
hidden={!visibleHome}
239254
className={styles.controls}
240255
aria-label="Issue and pull request controls"
241256
>
242257
<PopoverTrigger
243-
disabled={disabled || home === "loading"}
258+
disabled={disabled || pending}
244259
render={
245260
<IconButton
246-
disabled={disabled || home === "loading"}
261+
disabled={disabled || pending}
247262
size="toolbar"
248263
ref={trigger}
249264
type="button"
250265
aria-label={label}
251266
title={
252-
home === "loading"
267+
pending
253268
? "Loading project…"
254269
: project
255270
? label
256271
: `${label} (unavailable)`
257272
}
258273
data-unavailable={
259-
home === "ambiguous" || home === "error" ? "" : undefined
274+
visibleHome?.status === "ambiguous" ||
275+
visibleHome?.status === "error"
276+
? ""
277+
: undefined
260278
}
261279
icon={<GitPullRequestIcon size={20} aria-hidden="true" />}
262280
/>
263281
}
264282
/>
265-
{home === "loading" && (
266-
<span role="status" className="sr-only">
267-
Loading project…
268-
</span>
269-
)}
270283
<PopoverPopup
271284
side="top"
272285
sideOffset={4}
@@ -286,7 +299,7 @@ export function ResourcePicker({
286299
{!project ? (
287300
<>
288301
<p role="status">
289-
{home === "ambiguous"
302+
{home.status === "ambiguous"
290303
? "This channel belongs to more than one project, so issues and pull requests can't be chosen here."
291304
: "Could not load this channel's project."}
292305
</p>

0 commit comments

Comments
 (0)