|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { Button } from '@blueprintjs/core'; |
| 4 | +import { observer } from 'mobx-react'; |
| 5 | + |
| 6 | +import { GistHistoryDialog } from './history'; |
| 7 | +import { AppState } from '../state'; |
| 8 | + |
| 9 | +interface HistoryWrapperProps { |
| 10 | + appState: AppState; |
| 11 | + buttonOnly?: boolean; |
| 12 | + className?: string; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * A component that observes the appState and manages the history dialog. |
| 17 | + * Can be rendered as just a button or as a button with a dialog. |
| 18 | + */ |
| 19 | +@observer |
| 20 | +export class HistoryWrapper extends React.Component<HistoryWrapperProps> { |
| 21 | + private toggleHistory = () => { |
| 22 | + const { appState } = this.props; |
| 23 | + appState.toggleHistory(); |
| 24 | + }; |
| 25 | + |
| 26 | + private handleRevisionSelect = async (revisionId: string) => { |
| 27 | + const { remoteLoader } = window.app; |
| 28 | + try { |
| 29 | + await remoteLoader.fetchGistAndLoad( |
| 30 | + this.props.appState.gistId!, |
| 31 | + revisionId, |
| 32 | + ); |
| 33 | + } catch (error: any) { |
| 34 | + console.error('Failed to load revision', error); |
| 35 | + this.props.appState.showErrorDialog( |
| 36 | + `Failed to load revision: ${error.message || 'Unknown error'}`, |
| 37 | + ); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + public renderHistoryButton() { |
| 42 | + const { className } = this.props; |
| 43 | + |
| 44 | + return ( |
| 45 | + <Button |
| 46 | + icon="history" |
| 47 | + onClick={this.toggleHistory} |
| 48 | + className={className} |
| 49 | + aria-label="View revision history" |
| 50 | + data-testid="history-button" |
| 51 | + /> |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public render() { |
| 56 | + const { appState, buttonOnly } = this.props; |
| 57 | + const dialogKey = 'history-dialog'; |
| 58 | + |
| 59 | + return ( |
| 60 | + <> |
| 61 | + {buttonOnly ? ( |
| 62 | + this.renderHistoryButton() |
| 63 | + ) : ( |
| 64 | + <> |
| 65 | + {this.renderHistoryButton()} |
| 66 | + <GistHistoryDialog |
| 67 | + key={dialogKey} |
| 68 | + appState={appState} |
| 69 | + isOpen={appState.isHistoryShowing} |
| 70 | + onClose={this.toggleHistory} |
| 71 | + onRevisionSelect={this.handleRevisionSelect} |
| 72 | + activeRevision={appState.activeGistRevision} |
| 73 | + /> |
| 74 | + </> |
| 75 | + )} |
| 76 | + </> |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
0 commit comments