11import type { Callable } from '~types/host/callable'
22
3+ import type { RouteParams } from '@omnicajs/symfony-router'
4+
35import {
46 Context ,
57 ContextAccessor ,
@@ -17,14 +19,23 @@ import type { CustomContextStore } from '@retailcrm/embed-ui-v1-contexts/remote/
1719
1820import type { Endpoint , RemoteCallable } from '@remote-ui/rpc'
1921
22+ import { Router } from '@omnicajs/symfony-router'
23+
2024import type {
2125 ComputedRef ,
2226 WritableComputedRef ,
2327} from 'vue'
2428
25- import { computed } from 'vue'
29+ import {
30+ computed ,
31+ shallowRef ,
32+ watch ,
33+ } from 'vue'
34+
2635import { defineStore } from 'pinia'
2736
37+ import { useContext as useSettings } from '@retailcrm/embed-ui-v1-contexts/remote/settings'
38+
2839type Computed < S extends ContextSchema , F extends keyof S > = IsReadonly < S [ F ] > extends true
2940 ? ComputedRef < TypeOf < S [ F ] > >
3041 : WritableComputedRef < TypeOf < S [ F ] > >
@@ -98,3 +109,27 @@ export const useHost = (): RemoteCallable<Callable> => {
98109 } ,
99110 }
100111}
112+
113+ export const useRouter = ( ) : ComputedRef < Pick < Router , 'generate' > > => {
114+ const stringify = JSON . stringify
115+ const clone = < T > ( pojo : T ) => JSON . parse ( stringify ( pojo ) ) as T
116+
117+ const settings = useSettings ( )
118+ const routing = shallowRef ( clone ( settings [ 'system.routing' ] ) )
119+
120+ watch ( ( ) => settings [ 'system.routing' ] , ( changed ) => {
121+ if ( stringify ( changed ) !== stringify ( routing . value ) ) {
122+ routing . value = clone ( changed )
123+ }
124+ } )
125+
126+ return computed ( ( ) => {
127+ Router . setData ( clone ( routing . value ) )
128+
129+ return {
130+ generate : ( name : string , params : RouteParams = { } , absolute = false ) => {
131+ return Router . getInstance ( ) . generate ( name , params , absolute )
132+ } ,
133+ }
134+ } )
135+ }
0 commit comments