|
| 1 | +import { createComponent, InstallationConfigurationProps } from '@gitbook/runtime'; |
| 2 | +import { GitHubRuntimeContext, GitHubRuntimeEnvironment } from './types'; |
| 3 | + |
| 4 | +export const configComponent = createComponent< |
| 5 | + InstallationConfigurationProps<GitHubRuntimeEnvironment>, |
| 6 | + { step: 'edit.repo' | 'authenticate' | 'initial'; repo?: string }, |
| 7 | + { action: 'save.repo' | 'edit.repo' }, |
| 8 | + GitHubRuntimeContext |
| 9 | +>({ |
| 10 | + componentId: 'config', |
| 11 | + initialState: (props) => { |
| 12 | + const { installation } = props; |
| 13 | + if (installation.configuration?.repository && installation.configuration?.oauth_credentials) { |
| 14 | + return { step: 'initial' as const }; |
| 15 | + } |
| 16 | + if (installation.configuration?.repository) { |
| 17 | + return { step: 'authenticate' as const }; |
| 18 | + } |
| 19 | + return { step: 'edit.repo' as const, repo: '' }; |
| 20 | + }, |
| 21 | + action: async (element, action, context) => { |
| 22 | + switch (action.action) { |
| 23 | + case 'edit.repo': |
| 24 | + return { |
| 25 | + state: { |
| 26 | + step: 'edit.repo', |
| 27 | + repo: context.environment.installation?.configuration?.repository ?? '', |
| 28 | + }, |
| 29 | + }; |
| 30 | + case 'save.repo': |
| 31 | + await context.api.integrations.updateIntegrationInstallation( |
| 32 | + context.environment.integration.name, |
| 33 | + context.environment.installation!.id, |
| 34 | + { |
| 35 | + configuration: { |
| 36 | + repository: action.repo, |
| 37 | + }, |
| 38 | + }, |
| 39 | + ); |
| 40 | + return { state: { step: 'authenticate' } }; |
| 41 | + } |
| 42 | + }, |
| 43 | + render: async (element, context) => { |
| 44 | + const { installation } = context.environment; |
| 45 | + if (!installation) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + switch (element.state.step) { |
| 49 | + case 'initial': |
| 50 | + return ( |
| 51 | + <configuration> |
| 52 | + <input |
| 53 | + label="Repository" |
| 54 | + hint={<text>The integration is configured with the following repository:</text>} |
| 55 | + element={<textinput state="repo" initialValue={installation.configuration!.repository!} disabled={true} />} |
| 56 | + /> |
| 57 | + <box> |
| 58 | + <button style="secondary" label="Edit configuration" onPress={{ action: 'edit.repo' }} /> |
| 59 | + </box> |
| 60 | + <divider /> |
| 61 | + <input |
| 62 | + label="Authenticate" |
| 63 | + hint="Authorize GitBook to access your GitHub discussions." |
| 64 | + element={<button style="secondary" label="Authorize" onPress={{ action: '@ui.url.open', url: `${installation.urls.publicEndpoint}/oauth` }} />} |
| 65 | + /> |
| 66 | + </configuration> |
| 67 | + ); |
| 68 | + case 'edit.repo': |
| 69 | + return ( |
| 70 | + <configuration> |
| 71 | + <input |
| 72 | + label="Repository" |
| 73 | + hint={<text>Repository in the form owner/repo.</text>} |
| 74 | + element={<textinput state="repo" placeholder="owner/repo" />} |
| 75 | + /> |
| 76 | + <box> |
| 77 | + <button style="primary" label="Save" onPress={{ action: 'save.repo', repo: element.dynamicState('repo') }} /> |
| 78 | + </box> |
| 79 | + </configuration> |
| 80 | + ); |
| 81 | + case 'authenticate': |
| 82 | + return ( |
| 83 | + <configuration> |
| 84 | + <input |
| 85 | + label="Repository" |
| 86 | + element={<textinput state="repo" initialValue={installation.configuration!.repository!} disabled={true} />} |
| 87 | + /> |
| 88 | + <divider /> |
| 89 | + <input |
| 90 | + label="Authenticate" |
| 91 | + hint="Authorize GitBook to access your GitHub discussions." |
| 92 | + element={<button style="secondary" label="Authorize" onPress={{ action: '@ui.url.open', url: `${installation.urls.publicEndpoint}/oauth` }} />} |
| 93 | + /> |
| 94 | + </configuration> |
| 95 | + ); |
| 96 | + default: |
| 97 | + return null; |
| 98 | + } |
| 99 | + }, |
| 100 | +}); |
0 commit comments