mirror of
https://github.com/EthanMarti/infio-copilot.git
synced 2026-05-15 04:01:08 +00:00
feat: update custom mode draft
This commit is contained in:
95
src/hooks/use-custom-mode.ts
Normal file
95
src/hooks/use-custom-mode.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user