import { useState } from 'shared/hooks' ; import { Resource , Kind } from 'teleport/services/resources' ; export default function useResources < T extends Kind > ( resources , templates ) { const [ state , setState ] = useState ( { status : 'reading' as EditingStatus , item : null as Resource < T > , } ) ; const create = ( kind ) => { const content = templates [ kind ] || '' ; setState ( { status : 'creating' , item : { kind , name : '' , content , id : '' , } , } ) ; } ; const disregard = ( ) => { setState ( { status : 'empty' , item : null , } ) ; } ; const edit = ( id ) => { const item = resources . find ( c => c . id === id ) ; setState ( { status : 'editing' , item , } ) ; } ; const remove = ( id ) => { const item = resources . find ( c => c . id === id ) ; setState ( { status : 'removing' , item , } ) ; } ; return { ... state , create , edit , disregard , remove } ; } export type State = ReturnType < typeof useResources > ; type EditingStatus = 'creating' | 'editing' | 'removing' | 'empty' ; type Templates < T extends string > = Partial < Record < T , string > > ;