# Add AIssue to a React app (Vite, Create React App, other bundlers)

For Next.js, use https://aissue.dev/install/nextjs.md instead.

## Rules for the agent

- Make the smallest change that works. Do not refactor or reformat unrelated code.
- **Never** write the user's AIssue API token into a file, a commit, a log, or output you show. Keep it in a shell variable for this session only.
- Put the DSN in environment variables (`.env`, `.env.local`, the hosting provider's settings), not in committed code, except where a browser bundle needs it inline (the guide says when). Make sure `.env*` files are git-ignored.
- If the repository holds several applications, or the setup is unusual, ask the user which one to set up.
- If the project restricts new dependencies, ask before installing.


## Get the DSN

The DSN tells the SDK where to send errors. It looks like `https://<key>@aissue.dev/<number>`.

**If the user already gave you a DSN, use it and skip this section.**

Otherwise, ask the user:

> Please paste your AIssue API token. You can create one at https://aissue.dev/app/account/tokens.

Then, in the shell (the token stays in this session only):

```bash
export AISSUE_TOKEN='<the token the user gave you>'
curl -s https://aissue.dev/api/v1/domains -H "Authorization: Bearer $AISSUE_TOKEN"
```

- If one of the listed domains clearly belongs to this project, use its `id` and `dsn`.
- Otherwise create one, named after the project (for example the repository or package name):

```bash
curl -s -X POST https://aissue.dev/api/v1/domains \
  -H "Authorization: Bearer $AISSUE_TOKEN" -H "Content-Type: application/json" \
  -d '{"name": "<project name>"}'
```

The response contains the domain `id` and its `dsn`. If it says the plan's domain limit is reached, tell the user.


## Install

Use the project's package manager:

```bash
npm install aissue
```

## Configure

1. **Environment variable.** Use the bundler's public prefix:
   - **Vite:** `.env` → `VITE_AISSUE_DSN=<DSN>`
   - **Create React App:** `.env` → `REACT_APP_AISSUE_DSN=<DSN>`
   - **Other bundlers:** use their public-env mechanism, or put the DSN inline. It ends up in the browser bundle anyway, and it can only *send* errors to this project.

2. **In the entry file** (`src/main.tsx`, `src/main.jsx`, `src/index.tsx` or `src/index.js`), initialise AIssue before rendering and wrap the app in the error boundary:

```tsx
import { init, AIssueErrorBoundary } from "aissue/react";

init({ dsn: import.meta.env.VITE_AISSUE_DSN }); // CRA: process.env.REACT_APP_AISSUE_DSN

root.render(
  <AIssueErrorBoundary fallback={<p>Something went wrong.</p>}>
    <App />
  </AIssueErrorBoundary>
);
```

   Keep any existing providers and `StrictMode` wrappers; the boundary goes around them. If the app already has its own error boundary, keep it and call `captureException(error)` (from `"aissue/react"`) in its `componentDidCatch` instead of adding a second boundary.

Behaviour: reporting is off in development builds (`NODE_ENV` = `development`). Pass `enabled: true` to `init` to test locally. It catches render errors (via the boundary), uncaught errors, and unhandled promise rejections.

## Source maps (readable stack traces)

Production bundles are minified, so upload their source maps for every release (the release must equal the one the SDK sends, usually the git commit SHA). Create an account API token (https://aissue.dev/app/account/tokens), set it as `AISSUE_TOKEN` in CI, enable source maps in the build (Vite: `build.sourcemap: true`; webpack: `devtool: "source-map"`), then after the build:

```bash
npx aissue sourcemaps upload dist --release "$GITHUB_SHA" --domain <domain id> --delete-after
```

## Verification step

Run from the project directory after installing:

```bash
AISSUE_DSN='<DSN>' node -e "
const a = require('aissue');
a.init({ enabled: true });
try { throw new Error('AIssue setup test'); } catch (e) { a.captureException(e); }
a.flush(10000).then(ok => console.log(ok ? 'sent' : 'not sent'));
"
```

Also run the project's build to confirm it compiles.

## Verify that errors arrive

1. Send a test error with the command in the verification step above.
2. Check that it arrived (needs the API token; if you only have a DSN, ask the user to look at their AIssue dashboard instead):

```bash
curl -s "https://aissue.dev/api/v1/domains/<domain id>/events?limit=5" -H "Authorization: Bearer $AISSUE_TOKEN"
```

   Look for an event titled `RuntimeError: AIssue setup test` (or `Error: AIssue setup test`). It usually appears within a few seconds.
3. Resolve the test issue so it doesn't clutter the dashboard (`issue_id` is in the event):

```bash
curl -s -X POST https://aissue.dev/api/v1/issues/<issue_id>/resolve -H "Authorization: Bearer $AISSUE_TOKEN"
```

If nothing arrives, check:
- the DSN is exactly right;
- the environment variable is loaded where the command ran;
- the machine can reach `aissue.dev`;
- the SDK isn't disabled in development (the guide says how to enable it for the test).

## Finish

Tell the user:
- which files you changed;
- that `AISSUE_DSN` (and any other variables you added) must also be set in their **production** environment, for example in the hosting provider's dashboard;
- that errors are reported from production by default, not from local development.

**Recommend connecting GitHub.** AI analyses can then point at the exact file, line and commit. It needs the user in a browser:

```bash
curl -s https://aissue.dev/api/v1/domains/<domain id>/github -H "Authorization: Bearer $AISSUE_TOKEN"
```

Give the user the `connect_url` from that response.

**Deployed commit.** AIssue reads the exact deployed code when the SDK knows the git commit. This is automatic on Vercel, Render, Railway, Heroku, Cloudflare Pages and GitHub Actions. With Docker or other hosts, set `AISSUE_RELEASE` to the commit SHA at deploy time, for example `--build-arg AISSUE_RELEASE=$(git rev-parse HEAD)` plus `ENV AISSUE_RELEASE=$AISSUE_RELEASE` in the Dockerfile.
