Skip to content

Commit 3e73aef

Browse files
Merge pull request #1994 from QueryaHub/issue/1890-grid-staging-markers
feat(frontend): grid edits write into staging markers
2 parents 38338cd + e6953cc commit 3e73aef

7 files changed

Lines changed: 514 additions & 16 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, it } from "vitest";
2+
import { NULL_SENTINEL, StagingBuffer } from "./buffer";
3+
4+
describe("StagingBuffer (frontend)", () => {
5+
function sample() {
6+
return new StagingBuffer(
7+
["id", "name", "email"],
8+
[
9+
["1", "Alice", "alice@test.com"],
10+
["2", "Bob", "bob@test.com"],
11+
],
12+
["id"],
13+
);
14+
}
15+
16+
it("starts clean", () => {
17+
const buf = sample();
18+
expect(buf.isDirty()).toBe(false);
19+
expect(buf.cellStatus(0, 1)).toBe("clean");
20+
expect(buf.getCellValue(0, 1)).toBe("Alice");
21+
});
22+
23+
it("marks cell dirty and clears when reverted to original", () => {
24+
const buf = sample();
25+
buf.setCell(0, 1, "Alice Cooper");
26+
expect(buf.isDirty()).toBe(true);
27+
expect(buf.cellStatus(0, 1)).toBe("modified");
28+
expect(buf.rowStatus(0)).toBe("modified");
29+
expect(buf.getCellValue(0, 1)).toBe("Alice Cooper");
30+
buf.setCell(0, 1, "Alice");
31+
expect(buf.isDirty()).toBe(false);
32+
expect(buf.cellStatus(0, 1)).toBe("clean");
33+
});
34+
35+
it("toggle delete and null sentinel", () => {
36+
const buf = sample();
37+
buf.toggleDeleteRow(1);
38+
expect(buf.rowStatus(1)).toBe("deleted");
39+
buf.setCellNull(0, 2);
40+
expect(buf.getCellValue(0, 2)).toBe("NULL");
41+
expect(buf.effectiveRows()[0]?.[2]).toBe("NULL");
42+
expect(NULL_SENTINEL).toContain("QUERYA_NULL");
43+
});
44+
});

‎apps/tauri/src/staging/buffer.ts‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/** Frontend staging buffer — Flutter / Rust `StagingBuffer` parity (cell layer). */
2+
3+
export const NULL_SENTINEL = "\u0000__QUERYA_NULL__\u0000";
4+
5+
export type StagedRowStatus =
6+
| "unchanged"
7+
| "modified"
8+
| "inserted"
9+
| "deleted";
10+
11+
export type StagedCellStatus = "clean" | "modified";
12+
13+
export class StagingBuffer {
14+
private columns: string[];
15+
private originalRows: string[][];
16+
private primaryKeys: string[];
17+
private modifiedCells = new Map<number, Map<number, string>>();
18+
private insertedRows: string[][] = [];
19+
private deletedRowIndices = new Set<number>();
20+
private revision = 0;
21+
22+
constructor(
23+
columns: string[],
24+
rows: string[][],
25+
primaryKeys: string[] = [],
26+
) {
27+
this.columns = [...columns];
28+
this.originalRows = rows.map((r) => [...r]);
29+
this.primaryKeys = [...primaryKeys];
30+
}
31+
32+
/** Replace baseline (e.g. after a new query) and clear staged edits. */
33+
replaceBaseline(
34+
columns: string[],
35+
rows: string[][],
36+
primaryKeys: string[] = [],
37+
): void {
38+
this.columns = [...columns];
39+
this.originalRows = rows.map((r) => [...r]);
40+
this.primaryKeys = [...primaryKeys];
41+
this.modifiedCells.clear();
42+
this.insertedRows = [];
43+
this.deletedRowIndices.clear();
44+
this.revision += 1;
45+
}
46+
47+
getRevision(): number {
48+
return this.revision;
49+
}
50+
51+
getColumns(): string[] {
52+
return this.columns;
53+
}
54+
55+
getPrimaryKeys(): string[] {
56+
return this.primaryKeys;
57+
}
58+
59+
totalRowCount(): number {
60+
return this.originalRows.length + this.insertedRows.length;
61+
}
62+
63+
isDirty(): boolean {
64+
return (
65+
this.modifiedCells.size > 0 ||
66+
this.insertedRows.length > 0 ||
67+
this.deletedRowIndices.size > 0
68+
);
69+
}
70+
71+
changeCount(): number {
72+
let cells = 0;
73+
for (const m of this.modifiedCells.values()) cells += m.size;
74+
return cells + this.insertedRows.length + this.deletedRowIndices.size;
75+
}
76+
77+
getCellValue(row: number, col: number): string {
78+
if (row < this.originalRows.length) {
79+
const staged = this.modifiedCells.get(row)?.get(col);
80+
if (staged !== undefined) return sanitizeNull(staged);
81+
return this.originalRows[row]?.[col] ?? "";
82+
}
83+
const insertIdx = row - this.originalRows.length;
84+
return sanitizeNull(this.insertedRows[insertIdx]?.[col] ?? "");
85+
}
86+
87+
rowStatus(row: number): StagedRowStatus {
88+
if (row >= this.originalRows.length) {
89+
return row < this.totalRowCount() ? "inserted" : "unchanged";
90+
}
91+
if (this.deletedRowIndices.has(row)) return "deleted";
92+
const mods = this.modifiedCells.get(row);
93+
if (mods && mods.size > 0) return "modified";
94+
return "unchanged";
95+
}
96+
97+
cellStatus(row: number, col: number): StagedCellStatus {
98+
if (
99+
row < this.originalRows.length &&
100+
this.modifiedCells.get(row)?.has(col)
101+
) {
102+
return "modified";
103+
}
104+
return "clean";
105+
}
106+
107+
setCell(row: number, col: number, value: string): void {
108+
if (row < 0 || col < 0) return;
109+
if (row < this.originalRows.length) {
110+
const orig = this.originalRows[row]?.[col] ?? "";
111+
if (value === orig) {
112+
const map = this.modifiedCells.get(row);
113+
if (map) {
114+
map.delete(col);
115+
if (map.size === 0) this.modifiedCells.delete(row);
116+
this.revision += 1;
117+
}
118+
} else {
119+
let map = this.modifiedCells.get(row);
120+
if (!map) {
121+
map = new Map();
122+
this.modifiedCells.set(row, map);
123+
}
124+
if (map.get(col) !== value) {
125+
map.set(col, value);
126+
this.revision += 1;
127+
}
128+
}
129+
return;
130+
}
131+
const insertIdx = row - this.originalRows.length;
132+
const ins = this.insertedRows[insertIdx];
133+
if (!ins) return;
134+
while (ins.length <= col) ins.push("");
135+
if (ins[col] !== value) {
136+
ins[col] = value;
137+
this.revision += 1;
138+
}
139+
}
140+
141+
setCellNull(row: number, col: number): void {
142+
this.setCell(row, col, NULL_SENTINEL);
143+
}
144+
145+
toggleDeleteRow(row: number): void {
146+
if (row < 0) return;
147+
if (row < this.originalRows.length) {
148+
if (this.deletedRowIndices.has(row)) this.deletedRowIndices.delete(row);
149+
else this.deletedRowIndices.add(row);
150+
this.revision += 1;
151+
} else {
152+
const insertIdx = row - this.originalRows.length;
153+
if (insertIdx >= 0 && insertIdx < this.insertedRows.length) {
154+
this.insertedRows.splice(insertIdx, 1);
155+
this.revision += 1;
156+
}
157+
}
158+
}
159+
160+
revertAll(): void {
161+
if (!this.isDirty()) return;
162+
this.modifiedCells.clear();
163+
this.insertedRows = [];
164+
this.deletedRowIndices.clear();
165+
this.revision += 1;
166+
}
167+
168+
/** Effective grid values (deleted rows kept for strike-through). */
169+
effectiveRows(): string[][] {
170+
const out: string[][] = [];
171+
for (let r = 0; r < this.originalRows.length; r++) {
172+
const mods = this.modifiedCells.get(r);
173+
if (mods) {
174+
const row = [...(this.originalRows[r] ?? [])];
175+
for (const [c, v] of mods) {
176+
if (c < row.length) row[c] = sanitizeNull(v);
177+
}
178+
out.push(row);
179+
} else {
180+
out.push([...(this.originalRows[r] ?? [])]);
181+
}
182+
}
183+
for (const ins of this.insertedRows) {
184+
out.push(ins.map(sanitizeNull));
185+
}
186+
return out;
187+
}
188+
}
189+
190+
function sanitizeNull(value: string): string {
191+
return value === NULL_SENTINEL ? "NULL" : value;
192+
}
193+
194+
/** Coerce query result cells to staging strings. */
195+
export function rowsToStrings(rows: unknown[][]): string[][] {
196+
return rows.map((row) =>
197+
row.map((cell) => {
198+
if (cell === null || cell === undefined) return "NULL";
199+
if (typeof cell === "string") return cell;
200+
if (typeof cell === "number" || typeof cell === "boolean") {
201+
return String(cell);
202+
}
203+
return JSON.stringify(cell);
204+
}),
205+
);
206+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createPinia, setActivePinia } from "pinia";
2+
import { beforeEach, describe, expect, it } from "vitest";
3+
import { useGridStagingStore } from "./gridStaging";
4+
5+
describe("useGridStagingStore", () => {
6+
beforeEach(() => {
7+
setActivePinia(createPinia());
8+
});
9+
10+
it("attaches baseline and tracks dirty cell markers", () => {
11+
const store = useGridStagingStore();
12+
store.attachFromQuery(
13+
["id", "name"],
14+
[
15+
["1", "Alice"],
16+
["2", "Bob"],
17+
],
18+
);
19+
expect(store.isDirty).toBe(false);
20+
expect(store.cellStatus(0, 1)).toBe("clean");
21+
22+
store.setCell(0, 1, "Alice Cooper");
23+
expect(store.isDirty).toBe(true);
24+
expect(store.changeCount).toBe(1);
25+
expect(store.cellStatus(0, 1)).toBe("modified");
26+
expect(store.rowStatus(0)).toBe("modified");
27+
expect(store.getCellValue(0, 1)).toBe("Alice Cooper");
28+
expect(store.effectiveRows()[0]?.[1]).toBe("Alice Cooper");
29+
});
30+
31+
it("replaceBaseline clears prior edits", () => {
32+
const store = useGridStagingStore();
33+
store.attachFromQuery(["id"], [["1"]]);
34+
store.setCell(0, 0, "9");
35+
expect(store.isDirty).toBe(true);
36+
store.attachFromQuery(["id"], [["2"]]);
37+
expect(store.isDirty).toBe(false);
38+
expect(store.getCellValue(0, 0)).toBe("2");
39+
});
40+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { defineStore } from "pinia";
2+
import { computed, ref } from "vue";
3+
import { rowsToStrings, StagingBuffer } from "../staging/buffer";
4+
5+
/**
6+
* Pinia façade over {@link StagingBuffer} for the result grid.
7+
* Apply / discard IPC lands in #1891; this store owns dirty markers (#1890).
8+
*/
9+
export const useGridStagingStore = defineStore("gridStaging", () => {
10+
const buffer = ref<StagingBuffer | null>(null);
11+
/** Mirrors buffer revision so Vue / Glide can watch cheaply. */
12+
const revision = ref(0);
13+
14+
const isDirty = computed(() => buffer.value?.isDirty() ?? false);
15+
const changeCount = computed(() => buffer.value?.changeCount() ?? 0);
16+
const rowCount = computed(() => buffer.value?.totalRowCount() ?? 0);
17+
18+
function bump() {
19+
revision.value = buffer.value?.getRevision() ?? 0;
20+
}
21+
22+
function attachFromQuery(columns: string[], rows: unknown[][]) {
23+
const stringRows = rowsToStrings(rows);
24+
if (!buffer.value) {
25+
buffer.value = new StagingBuffer(columns, stringRows);
26+
} else {
27+
buffer.value.replaceBaseline(columns, stringRows);
28+
}
29+
bump();
30+
}
31+
32+
function clear() {
33+
buffer.value = null;
34+
revision.value = 0;
35+
}
36+
37+
function setCell(row: number, col: number, value: string) {
38+
buffer.value?.setCell(row, col, value);
39+
bump();
40+
}
41+
42+
function cellStatus(row: number, col: number) {
43+
return buffer.value?.cellStatus(row, col) ?? "clean";
44+
}
45+
46+
function rowStatus(row: number) {
47+
return buffer.value?.rowStatus(row) ?? "unchanged";
48+
}
49+
50+
function getCellValue(row: number, col: number) {
51+
return buffer.value?.getCellValue(row, col) ?? "";
52+
}
53+
54+
function effectiveRows(): string[][] {
55+
return buffer.value?.effectiveRows() ?? [];
56+
}
57+
58+
function revertAll() {
59+
buffer.value?.revertAll();
60+
bump();
61+
}
62+
63+
return {
64+
revision,
65+
isDirty,
66+
changeCount,
67+
rowCount,
68+
attachFromQuery,
69+
clear,
70+
setCell,
71+
cellStatus,
72+
rowStatus,
73+
getCellValue,
74+
effectiveRows,
75+
revertAll,
76+
};
77+
});

0 commit comments

Comments
 (0)