# Task 6 — PR Preview: Fix PR #193 + PostToolUse Hook

## Context
Two things need to be fixed/completed to make the PR preview flow fully work:
1. PR #193 (EAS Workflow for PR open) — exists but has a failing check and is missing steps
2. Claude Code PostToolUse hook — needs the same fixes applied

The worktree already exists: `.claude/worktrees/eas-pr-preview-workflow`
Branch: `feat/eas-pr-preview-workflow`
PR: #193

## What's wrong currently
- `eas update` in both places doesn't specify `--environment preview` → could bundle
  local env vars instead of remote expo.dev env
- Neither runs `eas channel:edit preview --branch pr-{N}` → installed dev builds
  still watch `preview` branch (pointing to `preview` branch by default), not the PR branch.
  So the QR in the PR comment works for first load but subsequent updates aren't received
  by already-running dev builds.
- The PR comment posts a plain expo.dev URL, not a dev-client deep-link QR

## Fix 1: PR #193 — `.eas/workflows/preview-on-pr-open.yml`

The EAS Workflow file lives in the repo at `.eas/workflows/preview-on-pr-open.yml`.
Check what's there currently, then apply these changes:

```yaml
# The eas update step should be:
- eas/update:
    name: Publish EAS Update
    inputs:
      branch: pr-${{ github.event.number }}
      message: "PR #${{ github.event.number }}: ${{ github.event.pull_request.title }}"
      environment: preview          # ← ADD THIS — use remote expo.dev env vars

# After the update step, add channel redirect:
- run:
    name: Redirect preview channel
    command: eas channel:edit preview --branch pr-${{ github.event.number }} --non-interactive

# The github-comment job should post a dev-client deep-link, not a plain URL:
# Replace the comment body with:
# 📱 **Preview ready** — PR #${{ github.event.number }}
# 
# Open in dev client (shake → home to switch branches):
# [Scan QR or tap link](exp+nanostreet://expo-development-client/?url=https%3A%2F%2Fu.expo.dev%2F5fff892f-218c-4b1c-bd29-bf8b0551d675%3Fbranch%3Dpr-${{ github.event.number }}%26runtime-version%3D${{ runtimeVersion }})
```

Also investigate why 1 check is failing in PR #193 and fix it.

## Fix 2: Claude Code PostToolUse hook

**File**: `.claude/settings.json` in the MAIN REPO (not a worktree)
Path: `/Users/fathoni/Documents/Project/BlockDev/nano-street/mobile/.claude/settings.json`

Find the PostToolUse hook that runs after `git push`. It currently runs:
```
eas update --branch pr-{number} --json --non-interactive
```

Replace with:
```bash
# 1. Publish OTA update using remote env (not local .env.local)
eas update \
  --branch pr-{number} \
  --environment preview \
  --message "Push to PR #{number}" \
  --non-interactive

# 2. Redirect preview channel to this PR's branch
#    (so installed dev builds automatically receive the update)
eas channel:edit preview --branch pr-{number} --non-interactive

# 3. Post/update PR comment with dev-client deep-link QR
#    Use gh api to upsert the comment (update existing if already posted, else create)
```

The comment body should be:
```
📱 **Preview updated** — [open in dev client](exp+nanostreet://expo-development-client/?url=https%3A%2F%2Fu.expo.dev%2F5fff892f-218c-4b1c-bd29-bf8b0551d675%3Fbranch%3Dpr-{number}%26runtime-version%3D1.0.0)

_Shake device → Dev menu → "Go home" to switch between active PR branches_
```

The hook must also:
- Check EAS auth (`eas whoami`) before running — skip gracefully if not authenticated
- Only run when a PR is open for the current branch (already implemented, keep it)
- Use upsert pattern for the PR comment (update existing "Preview" comment, don't spam new ones)

## Key details
- Project ID: `5fff892f-218c-4b1c-bd29-bf8b0551d675`
- Slug: `nanostreet`  
- Deep-link scheme: `exp+nanostreet` (from app.json `scheme: "nanostreet"`)
- Runtime version: currently `1.0.0` (appVersion policy in app.json)
- Preview environment on expo.dev has all `EXPO_PUBLIC_*` vars configured ✅
- `EXPO_TOKEN` available in GitHub secrets for CI side ✅
- For local hook: EAS session auth (`eas whoami` = mfnano) ✅

## Acceptance criteria
- [ ] Open new PR → EAS Workflow fires → OTA update published to `pr-{N}` branch
- [ ] `preview` channel redirected to `pr-{N}` — installed dev builds get the update
- [ ] PR comment posted with working dev-client deep-link
- [ ] Push to existing PR → Claude Code hook fires → OTA updated → comment updated (not duplicated)
- [ ] No local `.env.local` used — bundle uses expo.dev preview environment
- [ ] Closing PR → existing cleanup workflow deletes `pr-{N}` branch ✅ (already works)

## Note on channel:edit side-effect
`eas channel:edit preview --branch pr-{N}` redirects ALL dev builds to the latest PR that
was pushed. This is intentional — devs working on a PR see their latest push immediately.
To load a different PR, they shake → home → pick from list (or scan that PR's QR).
This is the expected UX.
