mirror of
https://github.com/EthanMarti/infio-copilot.git
synced 2026-01-17 00:33:49 +00:00
96 lines
2.4 KiB
TypeScript
96 lines
2.4 KiB
TypeScript
import { useCallback, useEffect, useMemo, useState } from 'react'
|
|
|
|
import { useApp } from '../contexts/AppContext'
|
|
import { CustomModeManager } from '../database/json/custom-mode/CustomModeManager'
|
|
import { CustomMode, GroupEntry } from '../database/json/custom-mode/types'
|
|
|
|
|
|
type UseCustomModes = {
|
|
createCustomMode: (
|
|
name: string,
|
|
roleDefinition: string,
|
|
customInstructions: string,
|
|
groups: GroupEntry[]
|
|
) => Promise<void>
|
|
deleteCustomMode: (id: string) => Promise<void>
|
|
updateCustomMode: (
|
|
id: string,
|
|
name: string,
|
|
roleDefinition: string,
|
|
customInstructions: string,
|
|
groups: GroupEntry[]
|
|
) => Promise<void>
|
|
FindCustomModeByName: (name: string) => Promise<CustomMode | undefined>
|
|
customModeList: CustomMode[]
|
|
}
|
|
|
|
export function useCustomModes(): UseCustomModes {
|
|
|
|
const [customModeList, setCustomModeList] = useState<CustomMode[]>([])
|
|
|
|
const app = useApp()
|
|
const customModeManager = useMemo(() => new CustomModeManager(app), [app])
|
|
|
|
const fetchCustomModeList = useCallback(async () => {
|
|
customModeManager.ListCustomModes().then((rows) => {
|
|
setCustomModeList(rows)
|
|
})
|
|
}, [customModeManager])
|
|
|
|
useEffect(() => {
|
|
void fetchCustomModeList()
|
|
}, [fetchCustomModeList])
|
|
|
|
const createCustomMode = useCallback(
|
|
async (
|
|
name: string,
|
|
roleDefinition: string,
|
|
customInstructions: string,
|
|
groups: GroupEntry[]
|
|
): Promise<void> => {
|
|
await customModeManager.createCustomMode({
|
|
name,
|
|
roleDefinition,
|
|
customInstructions,
|
|
groups,
|
|
})
|
|
fetchCustomModeList()
|
|
},
|
|
[customModeManager, fetchCustomModeList],
|
|
)
|
|
|
|
const deleteCustomMode = useCallback(
|
|
async (id: string): Promise<void> => {
|
|
await customModeManager.deleteCustomMode(id)
|
|
fetchCustomModeList()
|
|
},
|
|
[customModeManager, fetchCustomModeList],
|
|
)
|
|
|
|
const updateCustomMode = useCallback(
|
|
async (id: string, name: string, roleDefinition: string, customInstructions: string, groups: GroupEntry[]): Promise<void> => {
|
|
await customModeManager.updateCustomMode(id, {
|
|
name,
|
|
roleDefinition,
|
|
customInstructions,
|
|
groups,
|
|
})
|
|
fetchCustomModeList()
|
|
},
|
|
[customModeManager, fetchCustomModeList],
|
|
)
|
|
|
|
const FindCustomModeByName = useCallback(
|
|
async (name: string): Promise<CustomMode | undefined> => {
|
|
return customModeList.find((customMode) => customMode.name === name)
|
|
}, [customModeList])
|
|
|
|
return {
|
|
createCustomMode,
|
|
deleteCustomMode,
|
|
updateCustomMode,
|
|
FindCustomModeByName,
|
|
customModeList,
|
|
}
|
|
}
|