UNPKG

@open-condo/miniapp-utils

Version:

A set of helper functions / components / hooks used to build new condo apps fast

1 lines 2.06 kB
{"version":3,"sources":["../../src/hooks/useSetPageActionsHandlers.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\n\nimport type { CondoBridge, CondoBridgeSubscriptionListener } from '@open-condo/bridge'\n\nexport type ActionHandler = (actionId: string) => void\nexport type ActionHandlers = Partial<Record<string, ActionHandler>>\n\n/**\n * Subscribes to host action events and routes them to local handlers.\n *\n * Use it with host-provided actions that return `actionsIds` after configuration.\n * Those actions are rendered by the host in a platform-specific location\n * (e.g., bottom action bar on web or top-bar icons on mobile). Pass a map of\n * `{ [actionId]: handler }` so your miniapp can react to clicks on those actions.\n */\nexport function useSetPageActionsHandlers (bridge: CondoBridge, handlers: ActionHandlers): void {\n const handlersRef = useRef<ActionHandlers>(handlers)\n\n useEffect(() => {\n handlersRef.current = handlers\n }, [handlers])\n\n useEffect(() => {\n const listener: CondoBridgeSubscriptionListener = (event) => {\n if (event.type !== 'CondoWebAppActionClickEvent') return\n\n if ('actionId' in event.data && typeof event.data.actionId === 'string') {\n const { actionId } = event.data\n const handler = handlersRef.current[actionId]\n handler?.(actionId)\n }\n }\n\n bridge.subscribe(listener)\n return () => {\n bridge.unsubscribe(listener)\n }\n }, [bridge])\n}\n"],"mappings":";AAAA,SAAS,WAAW,cAAc;AAe3B,SAAS,0BAA2B,QAAqB,UAAgC;AAC5F,QAAM,cAAc,OAAuB,QAAQ;AAEnD,YAAU,MAAM;AACZ,gBAAY,UAAU;AAAA,EAC1B,GAAG,CAAC,QAAQ,CAAC;AAEb,YAAU,MAAM;AACZ,UAAM,WAA4C,CAAC,UAAU;AACzD,UAAI,MAAM,SAAS,8BAA+B;AAElD,UAAI,cAAc,MAAM,QAAQ,OAAO,MAAM,KAAK,aAAa,UAAU;AACrE,cAAM,EAAE,SAAS,IAAI,MAAM;AAC3B,cAAM,UAAU,YAAY,QAAQ,QAAQ;AAC5C,2CAAU;AAAA,MACd;AAAA,IACJ;AAEA,WAAO,UAAU,QAAQ;AACzB,WAAO,MAAM;AACT,aAAO,YAAY,QAAQ;AAAA,IAC/B;AAAA,EACJ,GAAG,CAAC,MAAM,CAAC;AACf;","names":[]}