Skip to content

Commit 4d3ebdd

Browse files
committed
Fix rendering of custom cells
1 parent 45bc0bf commit 4d3ebdd

File tree

4 files changed

+25
-58
lines changed

4 files changed

+25
-58
lines changed

src/spicedb-common/components/relationshipeditor/RelationshipEditor.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ export function RelationshipEditor({
470470

471471
const getCellData = useCallback(
472472
([col, row]: readonly [number, number]): GridCell => {
473-
console.log("getting cell data")
474473
if (row >= data.length) {
475474
return {
476475
kind: GridCellKind.Text,
@@ -935,16 +934,13 @@ export function RelationshipEditor({
935934
});
936935
};
937936

938-
console.log("data dump")
939-
console.log(data)
940-
const { drawCell, provideEditor } = useCustomCells(
937+
const { customRenderers } = useCustomCells(
941938
relationshipsService,
942939
data,
943940
gridSelection,
944941
resolver,
945942
similarHighlighting,
946943
columnsWithWidths,
947-
isReadOnly,
948944
);
949945

950946
// TODO: get JSX out of state.
@@ -1009,8 +1005,6 @@ export function RelationshipEditor({
10091005
label="Highlight same types, objects and relations"
10101006
/>
10111007
</div>
1012-
{console.log("this is how long we think data is")}
1013-
{console.log(data.length)}
10141008
<DataEditor
10151009
theme={dataEditorTheme}
10161010
keybindings={{ search: true }}
@@ -1020,8 +1014,7 @@ export function RelationshipEditor({
10201014
columns={columnsWithWidths}
10211015
rows={data.length}
10221016
onPaste={handlePaste}
1023-
drawCell={drawCell}
1024-
provideEditor={provideEditor}
1017+
customRenderers={customRenderers}
10251018
getCellsForSelection={getCellsForSelection}
10261019
onCellEdited={isReadOnly ? undefined : handleCellEdited}
10271020
onRowAppended={isReadOnly ? undefined : handleRowAppended}

src/spicedb-common/components/relationshipeditor/commentcell.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { CustomCell, DrawCellCallback } from "@glideapps/glide-data-grid";
1+
import {
2+
CustomCell,
3+
CustomRenderer,
4+
GridCellKind,
5+
} from "@glideapps/glide-data-grid";
26
import TextField from "@material-ui/core/TextField";
37
import React, { MutableRefObject, useEffect, useRef } from "react";
48
import { Column, CommentCellPrefix } from "./columns";
@@ -93,12 +97,13 @@ const CommentCellEditor = (props: CommentCellEditorProps) => {
9397

9498
export const CommentCellRenderer = (
9599
props: MutableRefObject<FieldCellRendererProps>,
96-
) => {
100+
): CustomRenderer<CommentCell> => {
97101
return {
102+
kind: GridCellKind.Custom,
98103
isMatch: (cell: CustomCell): cell is CommentCell =>
99104
// TODO: see if there's a way to do this without casting the cell
100105
(cell as CommentCell).data.kind === COMMENT_CELL_KIND,
101-
draw: (args: Parameters<DrawCellCallback>[0], cell: CommentCell) => {
106+
draw: (args, cell) => {
102107
const { ctx, rect } = args;
103108
const { dataValue } = cell.data;
104109
ctx.save();
@@ -115,7 +120,7 @@ export const CommentCellRenderer = (
115120
ctx.restore();
116121
return true;
117122
},
118-
provideEditor: () => (p: CommentCellEditorProps) => {
123+
provideEditor: () => (p) => {
119124
const { onChange, value, initialValue } = p;
120125
return (
121126
<CommentCellEditor

src/spicedb-common/components/relationshipeditor/customcells.ts

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import {
22
type DataEditorProps,
3-
type GridCell,
4-
GridCellKind,
3+
type CustomRenderer,
54
type GridSelection,
6-
type ProvideEditorCallback,
75
} from "@glideapps/glide-data-grid";
8-
import { useCallback, useMemo, useRef } from "react";
6+
import { useMemo, useRef } from "react";
97
import { Resolver } from "../../parsers/dsl/resolution";
108
import { RelationshipsService } from "../../services/relationshipsservice";
119
import { COLUMNS, Column, DataKind } from "./columns";
@@ -52,10 +50,12 @@ export function useCustomCells(
5250
resolver: Resolver | undefined,
5351
similarHighlighting: boolean,
5452
columnsWithWidths: Column[],
55-
isReadOnly: boolean,
5653
): {
57-
drawCell: DrawCallback;
58-
provideEditor: ProvideEditorCallback<GridCell>;
54+
// The type we're providing here is about as narrow as we can
55+
// get without fully figuring out a discriminated union here.
56+
// TODO: Make this part type-safe by figuring out the types.
57+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
58+
customRenderers: readonly CustomRenderer<any>[];
5959
} {
6060
const selectedType = useMemo(() => {
6161
if (!gridSelection?.current?.cell) {
@@ -253,42 +253,9 @@ export function useCustomCells(
253253
];
254254
}, []);
255255

256-
// TODO: come up with a way to express this that doesn't require looping.
257-
const drawCell = useCallback<DrawCallback>(
258-
(args) => {
259-
console.log("calling drawCell");
260-
console.log(args);
261-
const { cell } = args;
262-
console.log("cell.kind:", cell.kind);
263-
console.log(cell);
264-
if (cell.kind !== GridCellKind.Custom) return false;
265-
console.log("looping over renderers");
266-
for (const r of renderers) {
267-
if (r.isMatch(cell)) {
268-
// @ts-expect-error typescript can't see that we're looping over something static
269-
return r.draw(args, cell);
270-
}
271-
}
272-
return false;
273-
},
274-
[renderers],
275-
);
276-
277-
const provideEditor = useCallback<ProvideEditorCallback<GridCell>>(
278-
// @ts-expect-error typescript can't see that we're looping over something static.
279-
(cell) => {
280-
if (cell.kind !== GridCellKind.Custom || isReadOnly) return undefined;
281-
282-
for (const r of renderers) {
283-
if (r.isMatch(cell)) {
284-
return r.provideEditor();
285-
}
286-
}
287-
288-
return undefined;
289-
},
290-
[renderers, isReadOnly],
291-
);
292-
293-
return { drawCell, provideEditor };
256+
// TODO: there seems to be a type bug in glide-data-grid where
257+
// the inferred type on provideEditor doesn't match the type
258+
// that's expected here. You can remove the `as` to see what i mean.
259+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
260+
return { customRenderers: renderers as CustomRenderer<any>[] };
294261
}

src/spicedb-common/components/relationshipeditor/fieldcell.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CustomCell,
3+
GridCellKind,
34
DrawCellCallback,
45
GridSelection,
56
} from "@glideapps/glide-data-grid";
@@ -124,6 +125,7 @@ function fieldCellRenderer<T extends CustomCell<Q>, Q extends FieldCellProps>(
124125
return {
125126
// TODO: see if there's a way to do this without the as
126127
isMatch: (cell: CustomCell): cell is T => (cell as T).data.kind === kind,
128+
kind: GridCellKind.Custom,
127129
draw: (args: Parameters<DrawCellCallback>[0], cell: CustomCell<Q>) => {
128130
const { ctx, rect, row, col, theme, highlighted } = args;
129131
let { dataValue } = cell.data;

0 commit comments

Comments
 (0)