Skip to content

Conversation

@takaokouji
Copy link

概要

Issue #433 で提案されたGoogleドライブの自動保存機能とOAuth認証エラーハンドリングを実装しました。

実装内容

1. Googleドライブ自動保存機能

プロジェクトに変更が加えられた後、30秒経過すると自動的にGoogleドライブに保存する機能を実装しました。

動作フロー

  1. ユーザーがGoogleドライブからファイルを読み込む、またはGoogleドライブにコピーを保存
  2. プロジェクトを編集(ブロックの追加、削除など)
  3. 最後の変更から30秒経過後、自動的に「Googleドライブに直ちに保存」を実行
  4. 保存中は既存のスピナー表示を使用
  5. 保存完了後、「プロジェクトが保存されました。」と表示

技術的詳細

  • デバウンス処理: componentDidUpdateprojectChanged 状態を監視し、変更時に30秒のタイマーをスケジュール
  • タイマー管理: componentWillUnmount でタイマーをクリーンアップ
  • 条件チェック: googleDriveFile.isGoogleDriveFile === true の場合のみ自動保存を実行
  • 競合回避: 手動保存中は自動保存タイマーをクリア

2. OAuth認証エラーハンドリング

Google OAuthのアクセストークンは通常1時間で有効期限が切れます。自動保存中にトークンが期限切れになった場合の対応を実装しました。

動作フロー

  1. 自動保存または手動保存が実行される
  2. OAuth認証が期限切れでエラーが発生(401 Unauthorized)
  3. メニュー右側に「直ちに保存」ボタンと「認証が期限切れです。クリックして保存してください。」メッセージを表示
  4. ユーザーがボタンをクリック
  5. 自動的に再認証ダイアログを表示
  6. 認証成功後、保存を実行
  7. 保存完了後、通常の「プロジェクトが保存されました。」表示

技術的詳細

  • エラー検出: エラーのステータスコード(401)またはメッセージ("auth", "unauthorized")を検出
  • 状態管理: saveDirectStatus'auth_error' 状態を追加
  • UI実装: menu-bar.jsx に認証エラー時のButtonコンポーネントを追加
  • 国際化: 日本語メッセージを src/locales/ja.js に追加

変更ファイル

  • src/containers/google-drive-saver-hoc.jsx:

    • 自動保存ロジックの実装
    • OAuth認証エラーハンドリングの追加
    • ライフサイクルメソッド(componentDidUpdate, componentWillUnmount)の追加
    • タイマー管理メソッド(scheduleAutoSave, clearAutoSaveTimeout, tryToAutoSave)の追加
  • src/components/menu-bar/menu-bar.jsx:

    • 認証エラー時のUI追加
    • 「直ちに保存」ボタンの実装
  • src/locales/ja.js:

    • gui.menuBar.saveDirectlyButton: "直ちに保存"
    • gui.menuBar.authExpired: "認証が期限切れです。クリックして保存してください。"

テスト結果

  • ✅ Lintチェック: 合格
  • ✅ ビルド: 成功(34秒)

参考

スクリーンショット

(実際の動作確認時に追加予定)

🤖 Generated with Claude Code

takaokouji and others added 8 commits December 15, 2025 09:00
Implements auto-save functionality for Google Drive files:
- Auto-saves after 30 seconds of project changes
- Only saves files loaded from or saved to Google Drive
- Prevents unnecessary saves during manual save operations

OAuth authentication error handling:
- Detects 401 errors and expired authentication tokens
- Displays "Save directly" button in menu bar on auth errors
- Automatically re-authenticates and saves when button is clicked

Technical details:
- Added componentDidUpdate to monitor projectChanged state
- Implemented debounced auto-save with setTimeout
- Enhanced error handling in handleSaveDirectlyToGoogleDrive
- Added auth_error state to saveDirectStatus
- Added internationalization for auth error messages

Resolves #433

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Fixed projectChanged flag management:
- Reset to false after loading from Google Drive
- Reset to false after successful direct save
- Reduced auto-save interval to 5 seconds for debugging

This ensures the auto-save timer is triggered correctly when
the project is modified after loading or saving.

Technical details:
- Added setProjectUnchanged() import to both loader and saver HOCs
- Called onSetProjectUnchanged() after successful operations
- Added detailed debug logging for auto-save timing
- Fixed lint errors in debug log conditions

Related to #433

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Change auto-save interval from 5 seconds (debug) to 30 seconds (production)
- Remove all debug console.log statements added during development
- Keep essential error logging (log.warn, log.error, console.error)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
UI Improvements:
- Display only "Save directly" button when auth expires
- Show warning message as tooltip on hover (title attribute)
- Remove redundant text message for cleaner UI

Re-authentication:
- User-initiated save: automatically re-authenticate and retry save
- Auto-save: show auth error button only (no automatic OAuth dialog)
- Prevents surprising users with unexpected OAuth popups

Technical Details:
- Add isUserInitiated parameter to handleSaveDirectlyToGoogleDrive
- Use googleDriveAPI.requestAccessToken() for re-authentication
- Regenerate project data after re-auth before retry save
- Add handleSaveDirectlyToGoogleDrive method to MenuBar component

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add debug functionality to test authentication error handling:
- Add simulateAuthErrorCount counter (default: 2)
- First save attempt: throws 401 error (auto-save scenario)
- Second save attempt: throws 401 error (user clicks button)
- Third save attempt: normal save (after re-authentication)

This allows testing the complete re-authentication flow without
waiting for OAuth token expiration (1 hour).

To disable: Set simulateAuthErrorCount = 0 in constructor

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Set simulateAuthErrorCount to 0 to disable debug functionality.
Testing of re-authentication flow completed successfully.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…method

Refactor duplicate code by creating performSaveToGoogleDrive method:
- Consolidates project data generation
- Consolidates Google Drive upload
- Consolidates success state management
- Reduces code duplication from 2 locations (lines 204-218, 237-250)

Benefits:
- Single source of truth for save logic
- Easier to maintain and update
- Improved code readability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Display logic changes:
- Show "Save directly" button when:
  * File is from Google Drive (isGoogleDriveFile)
  * AND project has changes (projectChanged)
  * AND not currently saving/saved
- Show "Saving..." when status is 'saving'
- Show "Saved" when status is 'saved'
- Show nothing when:
  * Not a Google Drive file
  * OR no changes
  * OR during/after save

Added props:
- googleDriveFile: Full Google Drive file object
- projectChanged: Track if project has unsaved changes

This provides better UX by showing save button whenever
there are changes, not just on auth errors.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@takaokouji takaokouji merged commit 49b24c9 into develop Dec 15, 2025
2 checks passed
@takaokouji takaokouji deleted the feature/google-drive-auto-save branch December 15, 2025 15:02
github-actions bot pushed a commit that referenced this pull request Dec 15, 2025
…e-drive-auto-save

feat: Google Drive auto-save and OAuth authentication error handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants