Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
feat(widget): Enhance 9dof table widget (#56)
Browse files Browse the repository at this point in the history
- define types for table column and item
- new component Table
- clean up imports
- use title from user configuration

Co-authored-by: Pablo Klaschka <[email protected]>
  • Loading branch information
fussel178 and pklaschka authored Mar 1, 2021
1 parent ee2e980 commit 0add43a
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/model/sample-user-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const userConfig: UserConfig = {
rows: 12,
widgets: [
{
widgetName: '9dof',
widgetName: '9dof-widget',
width: 3,
height: 4,
title: 'Current values'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { Widget } from '@wuespace/telestion-client-types';
import { Widget as WidgetRenderer } from './widget';

export const widget: Widget = {
name: '9dof',
name: '9dof-widget',
Widget: WidgetRenderer
};
28 changes: 28 additions & 0 deletions src/widgets/9dof-widget/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export type ColumnKey = string;

/**
* A table column.
*/
export interface Column {
/**
* The key of the table column.
*/
key: ColumnKey;

/**
* The name/title of the table column.
*/
name: string | JSX.Element;
}

export type ItemValue = string | number | boolean;

/**
* A table item.
*/
export type Item = {
/**
* Unique key of the table item.
*/
key: string;
} & Record<ColumnKey, ItemValue>;
44 changes: 44 additions & 0 deletions src/widgets/9dof-widget/table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
Table as RSTable,
TableHeader,
Column,
TableBody,
Row,
Cell
} from '@react-spectrum/table';
import { Column as ColumnType, Item as ItemType, ItemValue } from './model';

function display(value: ItemValue): string | number {
switch (typeof value) {
case 'boolean':
return value ? 'true' : 'false';
case 'number':
return Math.round(value * 1000) / 1000;
case 'string':
return value;
default:
throw new TypeError(
`Type ${typeof value} can not be rendered in a table cell.`
);
}
}

export interface TableProps {
columns: ColumnType[];
items: ItemType[];
}

export function Table({ columns, items }: TableProps) {
return (
<RSTable>
<TableHeader columns={columns}>
{column => <Column>{column.name}</Column>}
</TableHeader>
<TableBody items={items}>
{item => (
<Row key={item.key}>{key => <Cell>{display(item[key])}</Cell>}</Row>
)}
</TableBody>
</RSTable>
);
}
48 changes: 48 additions & 0 deletions src/widgets/9dof-widget/widget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Heading, View } from '@adobe/react-spectrum';
import { useChannelLatest } from '@wuespace/telestion-client-core';
import { LoadingIndicator } from '@wuespace/telestion-client-common';
import { BaseRendererProps } from '@wuespace/telestion-client-types';

import { NineDofMessage } from '../../model/messages/nine-dof';
import { NineDOF } from '../../model/channels';

import { Table } from './table';
import { Column, Item } from './model';

const columns: Column[] = [
{ name: 'Sensor', key: 'sensor' },
{ name: <code>x</code>, key: 'x' },
{ name: <code>y</code>, key: 'y' },
{ name: <code>z</code>, key: 'z' }
];

export function Widget({ title }: BaseRendererProps) {
const latestData = useChannelLatest<NineDofMessage>(NineDOF)?.result[0];

const items: Item[] = [
{
sensor: 'Accelerometer',
key: `acc-${latestData ? Object.values(latestData?.acc) : ''}`,
...latestData?.acc
},
{
sensor: 'Gyroscope',
key: `gyro-${latestData ? Object.values(latestData?.gyro) : ''}`,
...latestData?.gyro
},
{
sensor: 'Magnetometer',
key: `mag-${latestData ? Object.values(latestData?.mag) : ''}`,
...latestData?.mag
}
];

return (
<View marginX={'size-200'}>
<Heading level={3}>{title}</Heading>
<LoadingIndicator dependencies={[latestData]}>
{() => <Table columns={columns} items={items} />}
</LoadingIndicator>
</View>
);
}
63 changes: 0 additions & 63 deletions src/widgets/9dof/widget.tsx

This file was deleted.

3 changes: 2 additions & 1 deletion src/widgets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using the tc-cli generate widget command.

import { Widget } from '@wuespace/telestion-client-types';
import { widget as sampleWidget } from './sample-widget';
import { widget as nineDofWidget } from './9dof-widget';
import { widget as stateWidget } from './state-widget';
import { widget as ninedof } from './9dof';
import { widget as graphWidget } from './graph-widget';
Expand All @@ -16,6 +17,6 @@ export const projectWidgets: Widget[] = [
// ARRAY_FIRST_ELEMENT_INSERT_MARK
stateWidget,
graphWidget as Widget,
ninedof,
nineDofWidget,
sampleWidget
];

0 comments on commit 0add43a

Please sign in to comment.