|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + Classes, |
| 5 | + Dialog, |
| 6 | + Icon, |
| 7 | + NonIdealState, |
| 8 | + Spinner, |
| 9 | + Tag, |
| 10 | +} from '@blueprintjs/core'; |
| 11 | +import { observer } from 'mobx-react'; |
| 12 | + |
| 13 | +import { AppState } from '../state'; |
| 14 | + |
| 15 | +interface GistRevision { |
| 16 | + sha: string; |
| 17 | + date: string; |
| 18 | + changes: { |
| 19 | + deletions: number; |
| 20 | + additions: number; |
| 21 | + total: number; |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +interface HistoryProps { |
| 26 | + appState: AppState; |
| 27 | + isOpen: boolean; |
| 28 | + onClose: () => void; |
| 29 | + onRevisionSelect: (revisionId: string) => Promise<void>; |
| 30 | +} |
| 31 | + |
| 32 | +interface HistoryState { |
| 33 | + isLoading: boolean; |
| 34 | + revisions: GistRevision[]; |
| 35 | + error: string | null; |
| 36 | +} |
| 37 | + |
| 38 | +@observer |
| 39 | +export class GistHistoryDialog extends React.Component< |
| 40 | + HistoryProps, |
| 41 | + HistoryState |
| 42 | +> { |
| 43 | + constructor(props: HistoryProps) { |
| 44 | + super(props); |
| 45 | + this.state = { |
| 46 | + isLoading: true, |
| 47 | + revisions: [], |
| 48 | + error: null, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + public async componentDidMount() { |
| 53 | + await this.loadRevisions(); |
| 54 | + } |
| 55 | + |
| 56 | + private async loadRevisions() { |
| 57 | + const { appState } = this.props; |
| 58 | + const { remoteLoader } = window.app; |
| 59 | + |
| 60 | + if (!appState.gistId) { |
| 61 | + this.setState({ isLoading: false, error: 'No Gist ID available' }); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + this.setState({ isLoading: true, error: null }); |
| 66 | + |
| 67 | + try { |
| 68 | + const revisions = await remoteLoader.getGistRevisions(appState.gistId); |
| 69 | + this.setState({ revisions, isLoading: false }); |
| 70 | + } catch (error) { |
| 71 | + console.error('Failed to load gist revisions', error); |
| 72 | + this.setState({ |
| 73 | + isLoading: false, |
| 74 | + error: 'Failed to load revision history', |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private handleRevisionSelect = async (revision: GistRevision) => { |
| 80 | + try { |
| 81 | + await this.props.onRevisionSelect(revision.sha); |
| 82 | + this.props.onClose(); |
| 83 | + } catch (error: any) { |
| 84 | + console.error('Failed to load revision', error); |
| 85 | + // show an error to the user and hide popover |
| 86 | + |
| 87 | + this.props.appState.showErrorDialog( |
| 88 | + `Failed to load revision: ${error.message || 'Unknown error'}`, |
| 89 | + ); |
| 90 | + this.props.onClose(); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + private renderChangeStats(changes: GistRevision['changes']) { |
| 95 | + return ( |
| 96 | + <div className="revision-changes"> |
| 97 | + <Tag intent="success" minimal> |
| 98 | + +{changes.additions} |
| 99 | + </Tag> |
| 100 | + <Tag intent="danger" minimal> |
| 101 | + -{changes.deletions} |
| 102 | + </Tag> |
| 103 | + <Tag minimal>{changes.total} total</Tag> |
| 104 | + </div> |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + private renderRevisionItem = (revision: GistRevision, index: number) => { |
| 109 | + const date = new Date(revision.date).toLocaleString(); |
| 110 | + const shortSha = revision.sha.substring(0, 7); |
| 111 | + const titleLabel = index === 0 ? 'Created' : `Revision ${index}`; |
| 112 | + |
| 113 | + return ( |
| 114 | + <li |
| 115 | + key={revision.sha} |
| 116 | + className="revision-item" |
| 117 | + onClick={() => this.handleRevisionSelect(revision)} |
| 118 | + > |
| 119 | + <div className="revision-content"> |
| 120 | + <h4> |
| 121 | + <Icon icon="history" className="revision-icon" /> |
| 122 | + {titleLabel} |
| 123 | + <span className="sha-label">{shortSha}</span> |
| 124 | + </h4> |
| 125 | + <div className="revision-details"> |
| 126 | + <span className="revision-date">{date}</span> |
| 127 | + {this.renderChangeStats(revision.changes)} |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </li> |
| 131 | + ); |
| 132 | + }; |
| 133 | + |
| 134 | + private renderContent() { |
| 135 | + const { isLoading, revisions, error } = this.state; |
| 136 | + |
| 137 | + if (isLoading) { |
| 138 | + return ( |
| 139 | + <div className="history-loading"> |
| 140 | + <Spinner /> |
| 141 | + <p>Loading revision history...</p> |
| 142 | + </div> |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + if (error) { |
| 147 | + return ( |
| 148 | + <NonIdealState |
| 149 | + icon="error" |
| 150 | + title="Error Loading History" |
| 151 | + description={error} |
| 152 | + /> |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + if (revisions.length === 0) { |
| 157 | + return ( |
| 158 | + <NonIdealState |
| 159 | + icon="history" |
| 160 | + title="No Revision History" |
| 161 | + description="This Gist doesn't have any revisions" |
| 162 | + /> |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + return ( |
| 167 | + <div className="revision-list"> |
| 168 | + <ul>{revisions.map(this.renderRevisionItem)}</ul> |
| 169 | + </div> |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + public render() { |
| 174 | + const { isOpen, onClose } = this.props; |
| 175 | + |
| 176 | + return ( |
| 177 | + <Dialog |
| 178 | + isOpen={isOpen} |
| 179 | + onClose={onClose} |
| 180 | + title="Revision History" |
| 181 | + className="gist-history-dialog" |
| 182 | + icon="history" |
| 183 | + > |
| 184 | + <div className={Classes.DIALOG_BODY}>{this.renderContent()}</div> |
| 185 | + </Dialog> |
| 186 | + ); |
| 187 | + } |
| 188 | +} |
0 commit comments