Skip to content

Commit

Permalink
feat: remove duplicated events
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Oct 18, 2024
1 parent 21d6099 commit 6066ad6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ function updateAIPanelConfig<T extends keyof BlockSuitePresets.AIActions>(
config.errorStateConfig = buildErrorConfig(aiPanel);
config.copy = buildCopyConfig(aiPanel);
config.discardCallback = () => {
getTracker(host).action_panel.discardAction({
action: id,
control: 'discard_button',
});
reportResponse('result:discard');
};
}
Expand All @@ -205,7 +201,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
if (!blocks || blocks.length === 0) return;
const block = blocks.at(-1);
assertExists(block);
getTracker(host).action_panel.invokeAction({ action: id });
getTracker(host, false).invokeAction({ action: id });
aiPanel.toggle(block, 'placeholder');
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,6 @@ function updateEdgelessAIPanelConfig<
},
};
config.discardCallback = () => {
getTracker(host).action_panel.discardAction({
action: id,
control: 'discard_button',
});
reportResponse('result:discard');
};
config.hideCallback = () => {
Expand Down Expand Up @@ -498,7 +494,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(

togglePanel()
.then(isEmpty => {
getTracker(host).action_panel.invokeAction({ action: id });
getTracker(host, false).invokeAction({ action: id });
aiPanel.toggle(referenceElement, isEmpty ? undefined : 'placeholder');
})
.catch(console.error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,11 @@ export function discard(panel: AffineAIPanelWidget): AIItemConfig {
};
}

export function retry<T extends keyof BlockSuitePresets.AIActions>(
panel: AffineAIPanelWidget,
id: T
): AIItemConfig {
export function retry(panel: AffineAIPanelWidget): AIItemConfig {
return {
name: 'Retry',
icon: ResetIcon,
handler: () => {
getTracker(panel.host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -148,10 +141,6 @@ export function createInsertResp<T extends keyof BlockSuitePresets.AIActions>(
);
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
handler(host, ctx);
const panel = getAIPanel(host);
Expand All @@ -173,10 +162,6 @@ export function asCaption<T extends keyof BlockSuitePresets.AIActions>(
return id === 'generateCaption' && !!panel.answer;
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'as_caption',
});
reportResponse('result:use-as-caption');
const panel = getAIPanel(host);
const caption = panel.answer;
Expand Down Expand Up @@ -595,10 +580,6 @@ export function actionToResponse<T extends keyof BlockSuitePresets.AIActions>(
name: 'Continue in chat',
icon: ChatWithAIIcon,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'continue_in_chat',
});
reportResponse('result:continue-in-chat');
const panel = getAIPanel(host);
AIProvider.slots.requestOpenWithChat.emit({ host });
Expand All @@ -607,7 +588,7 @@ export function actionToResponse<T extends keyof BlockSuitePresets.AIActions>(
},
...getInsertAndReplaceHandler(id, host, ctx, variants),
asCaption(id, host),
retry(getAIPanel(host), id),
retry(getAIPanel(host)),
discard(getAIPanel(host)),
],
},
Expand Down Expand Up @@ -640,23 +621,23 @@ export function actionToErrorResponse<
): ErrorConfig {
return {
upgrade: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
Expand All @@ -669,7 +650,7 @@ export function actionToErrorResponse<
},
{
name: '',
items: [retry(getAIPanel(host), id), discard(getAIPanel(host))],
items: [retry(getAIPanel(host)), discard(getAIPanel(host))],
},
],
};
Expand Down
61 changes: 8 additions & 53 deletions packages/frontend/core/src/blocksuite/presets/ai/ai-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ function asCaption<T extends keyof BlockSuitePresets.AIActions>(
return id === 'generateCaption' && !!panel.answer;
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'as_caption',
});
reportResponse('result:use-as-caption');
const panel = getAIPanel(host);
const caption = panel.answer;
Expand All @@ -87,10 +83,7 @@ function asCaption<T extends keyof BlockSuitePresets.AIActions>(
};
}

function createNewNote<T extends keyof BlockSuitePresets.AIActions>(
host: EditorHost,
id?: T
): AIItemConfig {
function createNewNote(host: EditorHost): AIItemConfig {
return {
name: 'Create new note',
icon: CreateIcon,
Expand All @@ -99,10 +92,6 @@ function createNewNote<T extends keyof BlockSuitePresets.AIActions>(
return !!panel.answer && isInsideEdgelessEditor(host);
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert_note',
});
reportResponse('result:add-note');
// get the note block
const { selectedBlocks } = getSelections(host);
Expand Down Expand Up @@ -208,10 +197,6 @@ export function buildTextResponseConfig<
showWhen: () =>
!!panel.answer && (!id || !INSERT_ABOVE_ACTIONS.includes(id)),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerBelow(panel).catch(console.error);
},
Expand All @@ -222,10 +207,6 @@ export function buildTextResponseConfig<
showWhen: () =>
!!panel.answer && !!id && INSERT_ABOVE_ACTIONS.includes(id),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerAbove(panel).catch(console.error);
},
Expand All @@ -236,15 +217,11 @@ export function buildTextResponseConfig<
icon: ReplaceIcon,
showWhen: () => !!panel.answer,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'replace',
});
reportResponse('result:replace');
replaceWithAnswer(panel).catch(console.error);
},
},
createNewNote(host, id),
createNewNote(host),
],
},
{
Expand All @@ -254,10 +231,6 @@ export function buildTextResponseConfig<
name: 'Continue in chat',
icon: ChatWithAIIcon,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'continue_in_chat',
});
reportResponse('result:continue-in-chat');
AIProvider.slots.requestOpenWithChat.emit({ host });
panel.hide();
Expand All @@ -267,10 +240,6 @@ export function buildTextResponseConfig<
name: 'Regenerate',
icon: RetryIcon,
handler: () => {
getTracker(host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -301,10 +270,7 @@ export function buildErrorResponseConfig<
icon: ReplaceIcon,
showWhen: () => !!panel.answer,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'replace',
});
reportResponse('result:replace');
replaceWithAnswer(panel).catch(console.error);
},
},
Expand All @@ -314,10 +280,7 @@ export function buildErrorResponseConfig<
showWhen: () =>
!!panel.answer && (!id || !INSERT_ABOVE_ACTIONS.includes(id)),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerBelow(panel).catch(console.error);
},
},
Expand All @@ -327,16 +290,12 @@ export function buildErrorResponseConfig<
showWhen: () =>
!!panel.answer && !!id && INSERT_ABOVE_ACTIONS.includes(id),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerAbove(panel).catch(console.error);
},
},
asCaption(host, id),
createNewNote(host, id),
createNewNote(host),
],
},
{
Expand All @@ -347,10 +306,6 @@ export function buildErrorResponseConfig<
icon: RetryIcon,
showWhen: () => true,
handler: () => {
getTracker(host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -386,23 +341,23 @@ export function buildErrorConfig<T extends keyof BlockSuitePresets.AIActions>(

return {
upgrade: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import track from '@affine/track';
import type { EditorHost } from '@blocksuite/affine/block-std';
import { isInsideEdgelessEditor } from '@blocksuite/affine/blocks';

export function getTracker(host: EditorHost) {
return track.copilot[isInsideEdgelessEditor(host) ? 'edgeless' : 'page'];
export function getTracker(host: EditorHost, inline: boolean) {
return track[isInsideEdgelessEditor(host) ? 'doc' : 'edgeless'].editor[
inline ? 'slashMenu' : 'formatToolbar'
];
}
Loading

0 comments on commit 6066ad6

Please sign in to comment.