Skip to content

Commit

Permalink
more typesafety
Browse files Browse the repository at this point in the history
  • Loading branch information
jespino committed Jan 10, 2025
1 parent 3ef6e53 commit b93eccf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
8 changes: 5 additions & 3 deletions webapp/src/ai_integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import {GlobalState} from 'mattermost-webapp/packages/types/src/store';
import {useSelector} from 'react-redux';

import {BotSelector, Bot, BotsLoaderHook} from './types/ai';

Check failure on line 7 in webapp/src/ai_integration.ts

View workflow job for this annotation

GitHub Actions / lint

Member 'Bot' of the import declaration should be sorted alphabetically

export const aiPluginID = 'mattermost-ai';

export const useAIAvailable = () => {
Expand All @@ -12,21 +14,21 @@ export const useAIAvailable = () => {
};

export const useAIAvailableBots = () => {
return useSelector<GlobalState, any[]>((state) => {
return useSelector<GlobalState, Bot[]>((state) => {
//@ts-ignore plugins state is a thing
return state['plugins-' + aiPluginID]?.bots || [];
});
};

export const useBotSelector = () => {
return useSelector<GlobalState, any[]>((state) => {
return useSelector<GlobalState, BotSelector>((state) => {
//@ts-ignore plugins state is a thing
return state['plugins-' + aiPluginID]?.botSelector;
});
};

export const useBotsLoaderHook = () => {
return useSelector<GlobalState, any[]>((state) => {
return useSelector<GlobalState, BotsLoaderHook>((state) => {
//@ts-ignore plugins state is a thing
return state['plugins-' + aiPluginID]?.botsLoaderHook || (() => null);
});
Expand Down
10 changes: 6 additions & 4 deletions webapp/src/components/modals/ai_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {Textbox} from 'src/webapp_globals';
import {generateStatusUpdate} from 'src/client';
import {useBotSelector, useBotsLoaderHook} from 'src/ai_integration';

import {Bot} from 'src/types/ai';

import postEventListener, {PostUpdateWebsocketMessage} from 'src/websocket';

type Version = {
Expand Down Expand Up @@ -53,12 +55,12 @@ const AIModal = ({playbookRunId, onAccept, onClose, isOpen}: Props) => {
const [copied, setCopied] = useState(false);
const [instruction, setInstruction] = useState('');
const suggestionBox = useRef<HTMLDivElement|null>(null);
const BotSelector = useBotSelector() as any;
const useBotlist = useBotsLoaderHook() as any;
const BotSelector = useBotSelector();
const useBotlist = useBotsLoaderHook();
const {bots, activeBot, setActiveBot} = useBotlist();
const [currentVersion, setCurrentVersion] = useState<number>(0);
const [versions, setVersions] = useState<Version[]>([]);
const [generating, setGenerating] = useState<any>(null);
const [generating, setGenerating] = useState(false);

useEffect(() => {
if (activeBot?.id && isOpen) {
Expand Down Expand Up @@ -89,7 +91,7 @@ const AIModal = ({playbookRunId, onAccept, onClose, isOpen}: Props) => {
};
}, [generating]);

const onBotChange = useCallback((bot: any) => {
const onBotChange = useCallback((bot: Bot) => {
setActiveBot(bot);
setCurrentVersion(versions.length + 1);
setVersions([...versions, {instruction: '', value: '', prevValue: ''}]);
Expand Down
14 changes: 14 additions & 0 deletions webapp/src/types/ai.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'

Check failure on line 1 in webapp/src/types/ai.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

export type Bot = {
id: string

Check failure on line 4 in webapp/src/types/ai.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2
}

type BotSelectorProps = {
bots: Bot[]

Check failure on line 8 in webapp/src/types/ai.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2
activeBot: Bot

Check failure on line 9 in webapp/src/types/ai.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2
setActiveBot: (bot: Bot) => void

Check failure on line 10 in webapp/src/types/ai.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2
}

export type BotsLoaderHook = () => BotSelectorProps
export type BotSelector = React.FunctionComponent<BotSelectorProps>

0 comments on commit b93eccf

Please sign in to comment.