All files / website/src main.tsx

0% Statements 0/52
0% Branches 0/38
0% Functions 0/10
0% Lines 0/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141                                                                                                                                                                                                                                                                                         
import { StrictMode, useRef, useState } from 'react';
import { createRoot } from 'react-dom/client';
import {
  detectPhotometricFormat,
  getPhotometricTypeSupport,
  parseLDT,
  parsePhotometry,
  type IESProfile,
  type PhotometricDocument,
} from '@ies-tools/core';
import { LightViewer } from '@ies-tools/viewer/react';
import { GraphPanel } from './GraphPanel.js';
import '@ies-tools/viewer/styles.css';
import './style.css';
 
function App() {
  const [profile, setProfile] = useState<IESProfile>();
  const [photometry, setPhotometry] = useState<PhotometricDocument>();
  const [sourceText, setSourceText] = useState('');
  const [sourceFormat, setSourceFormat] = useState<'ies' | 'ldt'>('ies');
  const [lampSets, setLampSets] = useState<string[]>([]);
  const [selectedLampSet, setSelectedLampSet] = useState<number>();
  const loadRevision = useRef(0);
  const [name, setName] = useState('No file selected');
  const [error, setError] = useState('');
  const load = async (file?: File) => {
    if (!file) return;
    const revision = ++loadRevision.current;
    try {
      const text = await file.text();
      if (revision !== loadRevision.current) return;
      const format = detectPhotometricFormat(text);
      const sets = format === 'ldt' ? parseLDT(text).lampSets : [];
      setSourceText(text);
      setSourceFormat(format);
      setLampSets(
        sets.map(
          (set, index) =>
            `${index + 1}: ${set.description || 'Lamp configuration'} · ${set.totalLumens} lm · ${set.watts} W`,
        ),
      );
      setSelectedLampSet(undefined);
      const next = sets.length > 1 ? undefined : parsePhotometry(text, format);
      const support = next && getPhotometricTypeSupport(next.profile);
      if (support && !support.supported) throw new Error(support.message);
      setPhotometry(next);
      setProfile(next?.profile);
      setName(file.name);
      setError('');
    } catch (reason) {
      if (revision !== loadRevision.current) return;
      setError(reason instanceof Error ? reason.message : String(reason));
    }
  };
  const selectLampSet = (value: string) => {
    const index = value === '' ? undefined : Number(value);
    setSelectedLampSet(index);
    try {
      const next = index === undefined ? undefined : parsePhotometry(sourceText, sourceFormat, { lampSetIndex: index });
      setPhotometry(next);
      setProfile(next?.profile);
      setError('');
    } catch (reason) {
      setError(reason instanceof Error ? reason.message : String(reason));
    }
  };
  return (
    <main className="dark">
      <header>
        <div>
          <p className="eyebrow">PHOTOMETRIC WORKBENCH</p>
          <h1>Light, specified.</h1>
          <p>Inspect an IES or EULUMDAT distribution on a floor and wall. Files stay in your browser.</p>
        </div>
        <a href="https://github.com/bhouston/ies-tools">GitHub ↗</a>
      </header>
      <section className="toolbar">
        <label className="file">
          Open .ies or .ldt file
          <input type="file" accept=".ies,.ldt" onChange={(event) => void load(event.target.files?.[0])} />
        </label>
        <span>{name}</span>
        {lampSets.length > 1 && (
          <label>
            Alternative lamp configuration
            <select value={selectedLampSet ?? ''} onChange={(event) => selectLampSet(event.target.value)}>
              <option value="">Choose a configuration</option>
              {lampSets.map((label, index) => (
                <option key={index} value={index}>
                  {label}
                </option>
              ))}
            </select>
          </label>
        )}
      </section>
      {error && <p role="alert">{error}</p>}
      <div className="workspace">
        <section className="scene">
          <LightViewer
            document={photometry}
            style={{ height: '100%', minHeight: 520 }}
            onError={(reason) => setError(reason.message)}
          />
          {!profile && (
            <div className="empty">
              {lampSets.length > 1
                ? 'Choose a lamp configuration to view this luminaire.'
                : 'Open an IES or LDT file to reveal its light distribution.'}
            </div>
          )}
        </section>
        <aside>
          <GraphPanel profile={profile} />
          {profile && (
            <dl>
              {Object.entries(profile.metadata)
                .filter(([, value]) => value)
                .slice(0, 8)
                .map(([key, value]) => (
                  <div key={key}>
                    <dt>{key}</dt>
                    <dd>{value}</dd>
                  </div>
                ))}
            </dl>
          )}
        </aside>
      </div>
      <footer>
        IES toolkit · <a href="https://ies-tools.ben3d.ca/">Documentation</a> · Drag to orbit · Scroll to zoom
      </footer>
    </main>
  );
}
createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <App />
  </StrictMode>,
);