import axios from 'axios' ; import create , { PartialState , State , StateCreator , StoreApi , GetState } from 'zustand' ; import { devtools } from 'zustand/middleware' ; import { track } from '../Tracker' ; export type NamedSetState < T extends State > = ( partial : PartialState < T > , name ? : any ) => void ; type NamedStateCreator < T extends State > = ( set : NamedSetState < T > , get : GetState < T > , api : StoreApi < T > ) => T ; export const withTrack = < T extends State > ( fn ) : NamedStateCreator < T > => ( set , get , api ) => { return fn ( ( state , name ? ) => { if ( name ) { track ( name , typeof state === 'object' ? state : { } ) ; } set ( state , name ) ; } , get , api ) ; } ; export const createWithDevtools = < T extends StateCreator < any > > ( stateAndActions , storeName ? ) => { const actionsWithTrack = withTrack ( stateAndActions ) ; const withDevTools = process . env . NODE_ENV === 'development' ? devtools ( actionsWithTrack , storeName ) : actionsWithTrack ; return create < ReturnType < T > > ( withDevTools ) ; } ; export interface AddRemovePaperToGroup { paperId : string ; shouldAdd : boolean ; groupId : string ; } export const addRemovePaperToGroupHelper = async ( { paperId , groupId , shouldAdd } ) => { return axios . post ( `/groups/group/${ groupId }` , { paper_id : paperId , add : shouldAdd } ) ; } ;