Skip to content

Commit e24aa27

Browse files
authored
fix: generics in GPT (#425)
Co-authored-by: reffat <--replace-all>
1 parent 614a4c3 commit e24aa27

File tree

11 files changed

+106
-43
lines changed

11 files changed

+106
-43
lines changed

src/extensions/additional/GPT/GptDialog/GptDialog.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type {FC} from 'react';
21
import React, {useCallback, useRef, useState} from 'react';
32

43
import {ArrowRight, ArrowRotateLeft, ThumbsDown, ThumbsUp} from '@gravity-ui/icons';
@@ -50,7 +49,10 @@ export type GptDialogProps<
5049

5150
export const cnGptDialog = cn('gpt-dialog');
5251

53-
export const GptDialog: FC<GptDialogProps> = ({
52+
export const GptDialog = <
53+
AnswerData extends CommonAnswer = CommonAnswer,
54+
PromptData extends unknown = unknown,
55+
>({
5456
markup,
5557
answerRender,
5658
promptPresets,
@@ -66,7 +68,7 @@ export const GptDialog: FC<GptDialogProps> = ({
6668
onDislike,
6769
onUpdate,
6870
gptAlertProps,
69-
}) => {
71+
}: GptDialogProps<AnswerData, PromptData>) => {
7072
const {
7173
answer,
7274
customPrompt,

src/extensions/additional/GPT/MarkupGpt/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {keymap} from '@codemirror/view';
22

33
import {GptWidgetOptions} from '../../..';
4+
import {CommonAnswer} from '../ErrorScreen/types';
45
import {gptHotKeys} from '../constants';
56

67
import {runMarkupGpt} from './commands';
@@ -9,7 +10,10 @@ import {mGptPlugin} from './plugin';
910
export {mGptToolbarItem} from './toolbar';
1011
export {showMarkupGpt, hideMarkupGpt} from './commands';
1112

12-
export function mGptExtension(props: GptWidgetOptions) {
13+
export function mGptExtension<
14+
AnswerData extends CommonAnswer = CommonAnswer,
15+
PromptData extends unknown = unknown,
16+
>(props: GptWidgetOptions<AnswerData, PromptData>) {
1317
return [
1418
mGptPlugin(props).extension,
1519
keymap.of([

src/extensions/additional/GPT/MarkupGpt/plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type ViewUpdate,
1111
} from '../../../../cm/view';
1212
import {ReactRendererFacet} from '../../../../markup';
13+
import {CommonAnswer} from '../ErrorScreen/types';
1314
import {WIDGET_DECO_CLASS_NAME} from '../constants';
1415
import {isEmptyGptPrompts} from '../utils';
1516

@@ -35,7 +36,10 @@ class SpanWidget extends WidgetType {
3536
}
3637
}
3738

38-
export function mGptPlugin(gptProps: GptWidgetOptions) {
39+
export function mGptPlugin<
40+
AnswerData extends CommonAnswer = CommonAnswer,
41+
PromptData extends unknown = unknown,
42+
>(gptProps: GptWidgetOptions<AnswerData, PromptData>) {
3943
return ViewPlugin.fromClass(
4044
class implements PluginValue {
4145
readonly _view: EditorView;

src/extensions/additional/GPT/MarkupGpt/popup.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ import {CommonAnswer} from '../ErrorScreen/types';
66
import {GptDialog, GptDialogProps} from '../GptDialog/GptDialog';
77
import {cnGptPopup} from '../gptExtension/view';
88

9-
type Props = {
9+
type Props<AnswerData extends CommonAnswer = CommonAnswer, PromptData extends unknown = unknown> = {
1010
onClose: () => void;
1111
markup: string;
1212
onConfirmOk?: () => void;
1313
onConfirmCancel?: () => void;
14-
} & GptDialogProps &
14+
} & GptDialogProps<AnswerData, PromptData> &
1515
Pick<PopupProps, 'anchorRef' | 'container'>;
1616

17-
export function renderPopup(anchor: HTMLElement, props: Props) {
18-
const handleUpdate = (result?: CommonAnswer) => props.onUpdate?.(result);
17+
export function renderPopup<
18+
AnswerData extends CommonAnswer = CommonAnswer,
19+
PromptData extends unknown = unknown,
20+
>(anchor: HTMLElement, props: Props<AnswerData, PromptData>) {
21+
const handleUpdate = (result?: AnswerData) => props.onUpdate?.(result);
1922

2023
return (
2124
<Popup

src/extensions/additional/GPT/PresetList/PresetList.tsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
11
import React from 'react';
2-
import type {FC} from 'react';
32

43
import {ActionTooltip, Button, DropdownMenu} from '@gravity-ui/uikit';
54

65
import {cn} from '../../../../classname';
76
import {i18n} from '../../../../i18n/gpt/dialog';
8-
import {type PromptPreset} from '../ErrorScreen/types';
7+
import {CommonAnswer, type PromptPreset} from '../ErrorScreen/types';
98
import type {GptDialogProps} from '../GptDialog/GptDialog';
109
import {gptHotKeys} from '../constants';
1110
import {useGptHotKeys} from '../hooks/useGptHotKeys';
1211
import {usePresetList} from '../hooks/usePresetList';
1312

1413
import './Presetlist.scss';
1514

16-
export type PresetListProps<PromptData extends unknown = unknown> = {
17-
disablePromptPresets: GptDialogProps['disablePromptPresets'];
18-
promptPresets: GptDialogProps['promptPresets'];
19-
onPresetClick: (data: PromptData) => void;
15+
export type PresetListProps<
16+
AnswerData extends CommonAnswer = CommonAnswer,
17+
PromptData extends unknown = unknown,
18+
> = {
19+
disablePromptPresets: GptDialogProps<AnswerData, PromptData>['disablePromptPresets'];
20+
promptPresets: GptDialogProps<AnswerData, PromptData>['promptPresets'];
21+
onPresetClick: (data?: PromptData) => void;
2022
};
2123

22-
type PresetItemType = {
23-
preset: PromptPreset<unknown>;
24-
onPresetClick: PresetListProps['onPresetClick'];
24+
type PresetItemType<
25+
AnswerData extends CommonAnswer = CommonAnswer,
26+
PromptData extends unknown = unknown,
27+
> = {
28+
preset: PromptPreset<PromptData>;
29+
onPresetClick: PresetListProps<AnswerData, PromptData>['onPresetClick'];
2530
disablePromptPresets?: PresetListProps['disablePromptPresets'];
2631
hotKey: string;
2732
};
2833

2934
export const cnGptDialogPresetList = cn('gpt-dialog-preset-list');
3035

31-
const PresetItem: FC<PresetItemType> = ({preset, onPresetClick, disablePromptPresets, hotKey}) => {
36+
const PresetItem = <
37+
AnswerData extends CommonAnswer = CommonAnswer,
38+
PromptData extends unknown = unknown,
39+
>({
40+
preset,
41+
onPresetClick,
42+
disablePromptPresets,
43+
hotKey,
44+
}: PresetItemType<AnswerData, PromptData>) => {
3245
useGptHotKeys(
3346
hotKey,
3447
() => {
@@ -52,13 +65,16 @@ const PresetItem: FC<PresetItemType> = ({preset, onPresetClick, disablePromptPre
5265
);
5366
};
5467

55-
export const PresetList: FC<PresetListProps> = ({
68+
export const PresetList = <
69+
AnswerData extends CommonAnswer = CommonAnswer,
70+
PromptData extends unknown = unknown,
71+
>({
5672
disablePromptPresets,
5773
promptPresets,
5874
onPresetClick,
59-
}) => {
75+
}: PresetListProps<AnswerData, PromptData>) => {
6076
const {presetsContainerRef, visiblePresets, hiddenPresets, showMoreButton, measured} =
61-
usePresetList({
77+
usePresetList<AnswerData, PromptData>({
6278
promptPresets,
6379
onPresetClick,
6480
});

src/extensions/additional/GPT/gptExtension/gptExtension.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {Action, ExtensionWithOptions} from 'src/core';
2-
1+
import {Action, ExtensionBuilder} from '../../../../core';
32
import type {CommonAnswer} from '../ErrorScreen/types';
43
import {showGptWidget} from '../actions';
54
import {runGpt} from '../commands';
@@ -31,7 +30,13 @@ export type GptWidgetOptions<
3130
| 'gptAlertProps'
3231
>;
3332

34-
export const gptExtension: ExtensionWithOptions<GptWidgetOptions> = (builder, options) => {
33+
export const gptExtension = <
34+
AnswerData extends CommonAnswer = CommonAnswer,
35+
PromptData extends unknown = unknown,
36+
>(
37+
builder: ExtensionBuilder,
38+
options: GptWidgetOptions<AnswerData, PromptData>,
39+
) => {
3540
builder.addAction(gptActionName, showGptWidget);
3641
builder.addPlugin(({serializer, markupParser}) => {
3742
return gptWidgetPlugin({

src/extensions/additional/GPT/gptExtension/view.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ export type GptWidgetDecoViewParams<
3434
gptPopupContainer?: PopupProps['container'];
3535
};
3636

37-
export class GptWidgetDecoView implements Required<PluginView> {
37+
export class GptWidgetDecoView<
38+
AnswerData extends CommonAnswer = CommonAnswer,
39+
PromptData extends unknown = unknown,
40+
> implements Required<PluginView>
41+
{
3842
private readonly _view;
3943
private readonly _renderer;
4044

4145
private _decoElem: Element | null = null;
42-
private _params: GptWidgetDecoViewParams;
46+
private _params: GptWidgetDecoViewParams<AnswerData, PromptData>;
4347
private _serializer: Serializer;
4448
private _parser: Parser;
4549
private _confirmOpen: boolean;
4650

47-
constructor(view: EditorView, params: GptWidgetDecoViewParams) {
51+
constructor(view: EditorView, params: GptWidgetDecoViewParams<AnswerData, PromptData>) {
4852
this._view = view;
4953

5054
this._params = params;
@@ -144,7 +148,9 @@ export class GptWidgetDecoView implements Required<PluginView> {
144148
);
145149
}
146150

147-
private _onGptAnswerUpdate: NonNullable<WidgetProps['onUpdate']> = (answer) => {
151+
private _onGptAnswerUpdate: NonNullable<WidgetProps<AnswerData, PromptData>['onUpdate']> = (
152+
answer,
153+
) => {
148154
this._params.onUpdate?.(answer);
149155
};
150156

@@ -233,15 +239,21 @@ export class GptWidgetDecoView implements Required<PluginView> {
233239
}
234240
}
235241

236-
type WidgetProps = Pick<PopupProps, 'anchorRef' | 'container'> & {
242+
type WidgetProps<
243+
AnswerData extends CommonAnswer = CommonAnswer,
244+
PromptData extends unknown = unknown,
245+
> = Pick<PopupProps, 'anchorRef' | 'container'> & {
237246
markup: string;
238247
onClose: () => void;
239248
confirmOpen: boolean;
240249
onConfirmOk: () => void;
241250
onConfirmCancel: () => void;
242-
} & GptDialogProps;
251+
} & GptDialogProps<AnswerData, PromptData>;
243252

244-
function Widget({
253+
function Widget<
254+
AnswerData extends CommonAnswer = CommonAnswer,
255+
PromptData extends unknown = unknown,
256+
>({
245257
markup,
246258
anchorRef,
247259
answerRender,
@@ -259,7 +271,7 @@ function Widget({
259271
onUpdate,
260272
container,
261273
gptAlertProps,
262-
}: WidgetProps) {
274+
}: WidgetProps<AnswerData, PromptData>) {
263275
useMount(() => {
264276
if (anchorRef?.current && 'scrollIntoView' in anchorRef.current) {
265277
anchorRef.current.scrollIntoView({
@@ -270,7 +282,7 @@ function Widget({
270282
});
271283

272284
const handleUpdate = useCallback(
273-
(result?: CommonAnswer) => {
285+
(result?: AnswerData) => {
274286
onUpdate?.(result);
275287
},
276288
[onUpdate],

src/extensions/additional/GPT/hooks/useGpt.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const useGpt = <
150150
[handleCustomPromptApply],
151151
);
152152

153-
const handlePresetClick = useCallback<PresetListProps<PromptData>['onPresetClick']>(
153+
const handlePresetClick = useCallback<PresetListProps<AnswerData, PromptData>['onPresetClick']>(
154154
async (data) => {
155155
const gptRequestData: GptRequestData<PromptData> = {
156156
markup,

src/extensions/additional/GPT/hooks/usePresetList.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@ import {useMemo, useRef} from 'react';
22

33
import type {DropdownMenuItem} from '@gravity-ui/uikit';
44

5+
import {CommonAnswer} from '../ErrorScreen/types';
56
import type {GptDialogProps} from '../GptDialog/GptDialog';
67
import type {PresetListProps} from '../PresetList/PresetList';
78
import {cnGptDialogPresetList} from '../PresetList/PresetList';
89

910
import {useOverflowingHorizontalItems} from './useOverflowingHorizontalItems';
1011

11-
type UsePresetListProps = Pick<GptDialogProps, 'promptPresets'> & {
12-
onPresetClick: PresetListProps['onPresetClick'];
12+
type UsePresetListProps<
13+
AnswerData extends CommonAnswer = CommonAnswer,
14+
PromptData extends unknown = unknown,
15+
> = Pick<GptDialogProps<AnswerData, PromptData>, 'promptPresets'> & {
16+
onPresetClick: PresetListProps<AnswerData, PromptData>['onPresetClick'];
1317
};
1418

15-
export const usePresetList = ({promptPresets, onPresetClick}: UsePresetListProps) => {
19+
export const usePresetList = <
20+
AnswerData extends CommonAnswer = CommonAnswer,
21+
PromptData extends unknown = unknown,
22+
>({
23+
promptPresets,
24+
onPresetClick,
25+
}: UsePresetListProps<AnswerData, PromptData>) => {
1626
const presetsContainerRef = useRef<HTMLDivElement>(null);
1727

1828
const {visibleItems, hiddenItems, measured} = useOverflowingHorizontalItems({

src/extensions/additional/GPT/plugin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Plugin, PluginKey} from 'prosemirror-state';
22
import {Decoration, DecorationSet} from 'prosemirror-view';
33

4+
import {CommonAnswer} from './ErrorScreen/types';
45
import {WIDGET_DECO_CLASS_NAME, WIDGET_DECO_SPEC_FLAG} from './constants';
56
import type {GptWidgetDecoViewParams} from './gptExtension/view';
67
import {GptWidgetDecoView} from './gptExtension/view';
@@ -20,7 +21,12 @@ const key = new PluginKey<DecorationSet>('gpt-widget');
2021

2122
export {key as pluginKey};
2223

23-
export const gptWidgetPlugin = (params: GptWidgetDecoViewParams): Plugin => {
24+
export const gptWidgetPlugin = <
25+
AnswerData extends CommonAnswer = CommonAnswer,
26+
PromptData extends unknown = unknown,
27+
>(
28+
params: GptWidgetDecoViewParams<AnswerData, PromptData>,
29+
): Plugin => {
2430
return new Plugin({
2531
key,
2632
state: {

0 commit comments

Comments
 (0)