# Show layer details

> Open a panel with a layer's terms when it is clicked.

Clicking a layer is the natural way to ask for more about it. `onSelect` tells you which layer was clicked; what you show is up to you.

Click a layer:

## Listen for the click

```tsx
import { useState } from 'react';
import { Towers } from '@vinsa/towers';

function ProgramTowers({ response, program }) {
  const [selected, setSelected] = useState(null);

  return (
    <div className="layout">
      <Towers
        data={response}
        onSelect={(key, risk) => setSelected(key ? { key, riskId: risk.riskId } : null)}
      />
      <LayerPanel program={program} selected={selected} />
    </div>
  );
}
```

`onSelect` receives:

- `key`: the layer's `layerRef`, or its `layerName` if it has no reference. `null` when the selection is cleared.
- `risk`: the risk the layer belongs to, from the response.

## Find the layer in your own data

The response carries what is needed to draw. Premium, reinstatements and the rest are in the request you sent, so look the layer up there with `layerKey`:

```tsx
import { formatAmount, layerKey, layerTerms } from '@vinsa/towers';

function LayerPanel({ program, selected }) {
  if (!selected) return <p>Select a layer to see its terms.</p>;

  const risk = program.risks.find((r) => r.riskId === selected.riskId);
  const contracts = program.contracts.filter((c) => risk?.contractRefs.includes(c.contractRef));
  const layers = contracts.flatMap((c) => c.layers);
  const layer = layers.find((l) => layerKey(l) === selected.key);
  if (!layer) return null;

  return (
    <aside>
      <h2>{layer.layerName}</h2>
      <p>{layerTerms(layer)}</p>
      <p>Premium: {layer.premium != null ? formatAmount(layer.premium) : 'not stated'}</p>
      <ul>
        {layer.lines?.map((r) => (
          <li key={r.reinsurer}>
            {r.reinsurer}: {r.writtenShare}% ({r.status})
          </li>
        ))}
      </ul>
    </aside>
  );
}
```

Give each layer a `layerRef` when two could share a name. The key is how they are told apart.

## Select from outside the chart

To highlight a layer from a row in your own table, control the selection: hold the key in state and pass it as `selectedKey`.

```tsx
const [selected, setSelected] = useState<string | null>(null);

<Towers data={response} riskId={riskId} selectedKey={selected} onSelect={setSelected} />

<table>
  {program.contracts.filter((c) => risk.contractRefs.includes(c.contractRef)).flatMap((c) => c.layers).map((l) => (
    <tr key={layerKey(l)} onClick={() => setSelected(layerKey(l))}>
      <td>{l.layerName}</td>
    </tr>
  ))}
</table>
```
