chore(common): analytics on spotlight (#3727)

Co-authored-by: amk-dev <akash.k.mohan98@gmail.com>
This commit is contained in:
James George
2024-02-02 15:32:06 +05:30
committed by GitHub
parent aab76f1358
commit d7cdeb796a
10 changed files with 267 additions and 76 deletions

View File

@@ -66,6 +66,13 @@ export type HoppAction =
| "user.login" // Login to Hoppscotch
| "user.logout" // Log out of Hoppscotch
| "editor.format" // Format editor content
| "modals.team.delete" // Delete team
| "workspace.switch" // Switch workspace
| "rest.request.open" // Open REST request
| "request.open-tab" // Open REST request
| "share.request" // Share REST request
| "tab.duplicate-tab" // Duplicate REST request
| "gql.request.open" // Open GraphQL request
/**
* Defines the arguments, if present for a given type that is required to be passed on
@@ -112,6 +119,7 @@ type HoppActionArgsMap = {
requestType: "gql"
request: HoppGQLRequest
}
| undefined
"request.open-tab": {
tab: RESTOptionTabs | GQLOptionTabs
}
@@ -121,7 +129,6 @@ type HoppActionArgsMap = {
"tab.duplicate-tab": {
tabID?: string
}
"gql.request.open": {
request: HoppGQLRequest
saveContext?: HoppGQLSaveContext
@@ -132,11 +139,23 @@ type HoppActionArgsMap = {
}
}
type KeysWithValueUndefined<T> = {
[K in keyof T]: undefined extends T[K] ? K : never
}[keyof T]
/**
* HoppActions which require arguments for their invocation
*/
export type HoppActionWithArgs = keyof HoppActionArgsMap
/**
* HoppActions which optionally takes in arguments for their invocation
*/
export type HoppActionWithOptionalArgs =
| HoppActionWithNoArgs
| KeysWithValueUndefined<HoppActionArgsMap>
/**
* HoppActions which do not require arguments for their invocation
*/
@@ -145,27 +164,26 @@ export type HoppActionWithNoArgs = Exclude<HoppAction, HoppActionWithArgs>
/**
* Resolves the argument type for a given HoppAction
*/
type ArgOfHoppAction<A extends HoppAction | HoppActionWithArgs> =
A extends HoppActionWithArgs ? HoppActionArgsMap[A] : undefined
type ArgOfHoppAction<A extends HoppAction> = A extends HoppActionWithArgs
? HoppActionArgsMap[A]
: undefined
/**
* Resolves the action function for a given HoppAction, used by action handler function defs
*/
type ActionFunc<A extends HoppAction | HoppActionWithArgs> =
A extends HoppActionWithArgs ? (arg: ArgOfHoppAction<A>) => void : () => void
type ActionFunc<A extends HoppAction> = A extends HoppActionWithArgs
? (arg: ArgOfHoppAction<A>, trigger?: InvocationTriggers) => void
: (_?: undefined, trigger?: InvocationTriggers) => void
type BoundActionList = {
// eslint-disable-next-line no-unused-vars
[A in HoppAction | HoppActionWithArgs]?: Array<ActionFunc<A>>
[A in HoppAction]?: Array<ActionFunc<A>>
}
const boundActions: BoundActionList = reactive({})
export const activeActions$ = new BehaviorSubject<
(HoppAction | HoppActionWithArgs)[]
>([])
export const activeActions$ = new BehaviorSubject<HoppAction[]>([])
export function bindAction<A extends HoppAction | HoppActionWithArgs>(
export function bindAction<A extends HoppAction>(
action: A,
handler: ActionFunc<A>
) {
@@ -179,27 +197,33 @@ export function bindAction<A extends HoppAction | HoppActionWithArgs>(
activeActions$.next(Object.keys(boundActions) as HoppAction[])
}
export type InvocationTriggers = "keypress" | "mouseclick"
type InvokeActionFunc = {
(action: HoppActionWithNoArgs, args?: undefined): void
(
action: HoppActionWithOptionalArgs,
args?: undefined,
trigger?: InvocationTriggers
): void
<A extends HoppActionWithArgs>(action: A, args: HoppActionArgsMap[A]): void
}
/**
* Invokes a action, triggering action handlers if any registered.
* The second argument parameter is optional if your action has no args required
* Invokes an action, triggering action handlers if any registered.
* The second and third arguments are optional
* @param action The action to fire
* @param args The argument passed to the action handler. Optional if action has no args required
* @param trigger Optionally supply the trigger that invoked the action (keypress/mouseclick)
*/
export const invokeAction: InvokeActionFunc = <
A extends HoppAction | HoppActionWithArgs,
>(
export const invokeAction: InvokeActionFunc = <A extends HoppAction>(
action: A,
args: ArgOfHoppAction<A>
args?: ArgOfHoppAction<A>,
trigger?: InvocationTriggers
) => {
boundActions[action]?.forEach((handler) => handler(args! as any))
boundActions[action]?.forEach((handler) => handler(args! as any, trigger))
}
export function unbindAction<A extends HoppAction | HoppActionWithArgs>(
export function unbindAction<A extends HoppAction>(
action: A,
handler: ActionFunc<A>
) {
@@ -232,7 +256,7 @@ export function isActionBound(action: HoppAction): Ref<boolean> {
* @param handler The function to be called when the action is invoked
* @param isActive A ref that indicates whether the action is active
*/
export function defineActionHandler<A extends HoppAction | HoppActionWithArgs>(
export function defineActionHandler<A extends HoppAction>(
action: A,
handler: ActionFunc<A>,
isActive: Ref<boolean> | undefined = undefined