Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.7 KB
Raw Blame
import '@app/index.css';
import type { SortKey } from '@core/database-sql/generated/types';
import { createSignal } from 'solid-js';
import { render } from 'solid-js/web';
import type { DatabaseTableControls } from '../../database/components/database-table';
import { GridCell } from '../../database/components/grid-cell';
import type { DatabaseViewColumn } from '../../database/core/database-view';
import { moveBeside } from '../../database/core/move-beside';
import type { DatabaseRow } from '../../database/core/table';
import { withSort } from '../../database/core/view-query';
import { DatabaseTableView } from '../../database/views/database-table-view';

// Real grid and editors; records and saved layout are local to this browser fixture.
function TableFixture() {
  let controls: DatabaseTableControls | undefined;
  const [columns, setColumns] = createSignal<DatabaseViewColumn[]>([
    {
      id: 'name',
      name: 'Name',
      dataType: 'STRING',
      writable: true,
      isMultiSelect: false,
      options: [],
    },
    {
      id: 'notes',
      name: 'Notes',
      dataType: 'STRING',
      writable: true,
      isMultiSelect: false,
      options: [],
    },
    {
      id: 'status',
      name: 'Status',
      dataType: 'SELECT_STRING',
      writable: true,
      isMultiSelect: false,
      options: [
        { id: 'open', label: 'Open', color: null },
        { id: 'done', label: 'Done', color: null },
      ],
    },
    {
      id: 'amount',
      name: 'Amount',
      dataType: 'NUMBER',
      writable: true,
      isMultiSelect: false,
      options: [],
    },
  ]);
  const [rows, setRows] = createSignal<DatabaseRow[]>([
    {
      rowId: 'one',
      cells: { name: 'First', notes: 'First note', status: 'Open', amount: 12 },
    },
    {
      rowId: 'two',
      cells: {
        name: 'Second',
        notes: 'Second note',
        status: 'Done',
        amount: 24,
      },
    },
  ]);
  const [order, setOrder] = createSignal(['name', 'notes', 'status', 'amount']);
  const [widths, setWidths] = createSignal<Record<string, number | null>>({});
  const [sort, setSort] = createSignal<SortKey[]>([]);
  const [resizeSaves, setResizeSaves] = createSignal(0);
  const [writes, setWrites] = createSignal(0);
  return (
    <main class="flex h-screen flex-col bg-panel text-ink">
      <div class="flex gap-4 p-4">
        <button
          onMouseDown={(event) => event.preventDefault()}
          onClick={() => {
            setColumns((before) => before.map((column) => ({ ...column })));
            setRows((before) =>
              before.map((row) => ({
                ...row,
                cells: { ...row.cells, notes: 'Remote update' },
              }))
            );
          }}
        >
          Remote update
        </button>
        <button
          onClick={() => {
            setOrder(['status', 'notes', 'name']);
            setWidths({ name: 240, notes: 220, status: 180 });
          }}
        >
          Switch view
        </button>
        <output aria-label="Resize saves">{resizeSaves()}</output>
        <output aria-label="Writes">{writes()}</output>
        <output aria-label="Sort">{JSON.stringify(sort())}</output>
        <button
          onClick={() =>
            setRows(
              Array.from({ length: 1000 }, (_, index) => ({
                rowId: `row-${index}`,
                cells: {
                  name: `Record ${index}`,
                  notes: '',
                  status: 'Open',
                  amount: index,
                },
              }))
            )
          }
        >
          Load 1000 records
        </button>
        <button onClick={() => controls?.editCell('row-999', 'name')}>
          Edit last record
        </button>
        <button onClick={() => controls?.editCell('row-0', 'name')}>
          Edit first record
        </button>
      </div>
      <DatabaseTableView
        controlsRef={(value) => {
          controls = value;
        }}
        name="Tasks"
        rows={rows()}
        columns={columns()}
        columnOrder={order()}
        widths={widths()}
        sort={sort()}
        titleColumnId="name"
        canEdit
        pending={false}
        addColumn={<button>Add column</button>}
        onOpen={() => {}}
        getRowTitle={(row) => String(row.cells.name)}
        onSort={(id, direction) =>
          setSort((before) =>
            withSort(
              before,
              id,
              direction === null
                ? null
                : direction === 'asc'
                  ? 'ascending'
                  : 'descending'
            )
          )
        }
        onResizeColumn={(id, width) => {
          setWidths((before) => ({ ...before, [id]: width }));
          setResizeSaves((count) => count + 1);
        }}
        onReorderColumn={async (id, target, edge) => {
          setOrder((before) => moveBeside(before, id, target, edge) ?? before);
        }}
        renderCell={(row, column, options) => (
          <GridCell
            column={column()}
            value={row().cells[column().id] ?? null}
            canEdit
            onAddOption={async () => true}
            onWrite={async (value) => {
              const rowId = row().rowId;
              const columnId = column().id;
              setWrites((count) => count + 1);
              setRows((before) =>
                before.map((entry) =>
                  entry.rowId === rowId
                    ? { ...entry, cells: { ...entry.cells, [columnId]: value } }
                    : entry
                )
              );
              return true;
            }}
            {...options}
          />
        )}
      />
    </main>
  );
}
render(() => <TableFixture />, document.getElementById('root')!);