Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ export function useMainHeaderAssistant() {
topicId?: string;
}>();
const topic = useTopic(topicId);
const currentAssistantId = topicId ? topic.data?.assistantId : assistantId;
// 话题一旦解析出来就以它为准,但在那之前**不放弃 URL 上还带着的 assistantId**。
// 硬切(`topicId ? topic.data?.assistantId : assistantId`)会让助手按钮在话题加载完之前
// 变成 null:新话题发出第一条消息时导航先于话题可读,按钮因此整个消失再回来,iOS 上
// toolbar 的 children 数量从 2 掉到 1,两个按钮一起走原生重建(实测淡出再淡入 600ms)。
//
// 回落值不会是别的助手:`open-topic` 走 `setParams({ topicId })`,assistantId 原样留在
// URL 上且就是这个话题的助手;而从话题列表进来那条路带的 params 只有 topicId,回落到
// undefined,与从前一致。
const currentAssistantId = (topicId ? topic.data?.assistantId : undefined) ?? assistantId;
const { assistant } = useAssistantApiById(currentAssistantId);

const openNewTopic = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ describe('MainHeaderAssistantButton', () => {
});
});

it('keeps the route assistant while the topic is still loading', async () => {
// 发出新话题第一条消息时导航先于话题可读:`topicId` 已经在 URL 上、`useTopic` 还没
// 返回。按钮此刻若消失,iOS 的 toolbar 会连着旁边那个按钮一起原生重建。
mockAssistantId = 'assistant-1';
mockTopicAssistantId = undefined;
mockTopicId = 'topic-1';

await act(async () => {
renderer = create(<Harness />);
});

expect(
renderer?.root.findByProps({ testID: 'current-assistant-button' }).props.accessibilityLabel,
).toBe('Peanut');
});

it('starts a new topic with the current topic assistant', async () => {
await act(async () => {
renderer = create(<NewTopicHarness />);
Expand Down