|
| 1 | +import { createComponent, RuntimeContext, RuntimeEnvironment } from '@gitbook/runtime'; |
| 2 | + |
| 3 | +type GARuntimeEnvironment = RuntimeEnvironment<{}, GASiteInstallationConfiguration>; |
| 4 | + |
| 5 | +type GARuntimeContext = RuntimeContext<GARuntimeEnvironment>; |
| 6 | + |
| 7 | +type GASiteInstallationConfiguration = { |
| 8 | + tracking_id?: string; |
| 9 | +}; |
| 10 | + |
| 11 | +type GAState = GASiteInstallationConfiguration; |
| 12 | + |
| 13 | +type GAProps = { |
| 14 | + siteInstallation: { |
| 15 | + configuration?: GASiteInstallationConfiguration; |
| 16 | + }; |
| 17 | +}; |
| 18 | + |
| 19 | +export type GAAction = { action: 'save.config' }; |
| 20 | + |
| 21 | +export const configBlock = createComponent<GAProps, GAState, GAAction, GARuntimeContext>({ |
| 22 | + componentId: 'config', |
| 23 | + initialState: (props) => { |
| 24 | + const siteInstallation = props.siteInstallation; |
| 25 | + return { |
| 26 | + tracking_id: siteInstallation.configuration?.tracking_id ?? '', |
| 27 | + }; |
| 28 | + }, |
| 29 | + action: async (element, action, context) => { |
| 30 | + switch (action.action) { |
| 31 | + case 'save.config': |
| 32 | + const { api, environment } = context; |
| 33 | + const siteInstallation = environment.siteInstallation; |
| 34 | + if (!siteInstallation) { |
| 35 | + throw Error('No site installation found'); |
| 36 | + } |
| 37 | + |
| 38 | + const configurationBody = { |
| 39 | + ...siteInstallation.configuration, |
| 40 | + tracking_id: element.state.tracking_id, |
| 41 | + }; |
| 42 | + await api.integrations.updateIntegrationSiteInstallation( |
| 43 | + siteInstallation.integration, |
| 44 | + siteInstallation.installation, |
| 45 | + siteInstallation.site, |
| 46 | + { |
| 47 | + configuration: { |
| 48 | + ...configurationBody, |
| 49 | + }, |
| 50 | + }, |
| 51 | + ); |
| 52 | + return element; |
| 53 | + } |
| 54 | + }, |
| 55 | + render: async () => { |
| 56 | + return ( |
| 57 | + <configuration> |
| 58 | + <input |
| 59 | + label="Client ID" |
| 60 | + hint={<text>The unique identifier of your GA app client.</text>} |
| 61 | + element={<textinput state="tracking_id" placeholder="Tracking ID" />} |
| 62 | + /> |
| 63 | + |
| 64 | + <divider /> |
| 65 | + <input |
| 66 | + label="" |
| 67 | + hint="" |
| 68 | + element={ |
| 69 | + <button |
| 70 | + style="primary" |
| 71 | + disabled={false} |
| 72 | + label="Save" |
| 73 | + tooltip="Save configuration" |
| 74 | + onPress={{ |
| 75 | + action: 'save.config', |
| 76 | + }} |
| 77 | + /> |
| 78 | + } |
| 79 | + /> |
| 80 | + </configuration> |
| 81 | + ); |
| 82 | + }, |
| 83 | +}); |
0 commit comments