instant_record 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Copy-on-write local SQLite staging branch coordinator for the browser worker.
|
|
2
|
+
//
|
|
3
|
+
// Forks the live `main` SQLite file bytes into an isolated offline AppHost so
|
|
4
|
+
// users or AI agents can execute bulk actions, inspect the resulting state
|
|
5
|
+
// rendered through the real Rails UI, and then either:
|
|
6
|
+
// - approve: extract ONLY the new branch OutboxMutations and replay them onto
|
|
7
|
+
// current `main` (preserving remote deltas that arrived during review), or
|
|
8
|
+
// - discard: drop the cloned SQLite instance with zero `main` or server impact.
|
|
9
|
+
|
|
10
|
+
function generateBranchId() {
|
|
11
|
+
if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID();
|
|
12
|
+
return "branch-" + Math.random().toString(36).slice(2, 10);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function normalizeName(name) {
|
|
16
|
+
const cleaned = typeof name === "string" ? name.trim() : "";
|
|
17
|
+
return cleaned || "staging";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class BranchManager {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.active = null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
isActive() {
|
|
26
|
+
return this.active !== null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
host() {
|
|
30
|
+
return this.active ? this.active.host : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Clone the live `main` SQLite bytes into a fresh isolated staging host.
|
|
34
|
+
fork({ name = "staging", mainHost, createHost, schemaVersion = 0 }) {
|
|
35
|
+
if (this.active) {
|
|
36
|
+
throw new Error(`Already on staging branch '${this.active.name}' — approve or discard it first`);
|
|
37
|
+
}
|
|
38
|
+
if (!mainHost) {
|
|
39
|
+
throw new Error("Cannot create a staging branch before the app has booted");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const bytes = mainHost.cloneDatabaseBytes
|
|
43
|
+
? mainHost.cloneDatabaseBytes()
|
|
44
|
+
: (() => {
|
|
45
|
+
const raw = mainHost.databaseBytes();
|
|
46
|
+
return raw ? new Uint8Array(raw) : null;
|
|
47
|
+
})();
|
|
48
|
+
if (!bytes) {
|
|
49
|
+
throw new Error("SQLite has not produced a database snapshot yet");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const baseOutbox = mainHost.branchOutbox([]);
|
|
53
|
+
const baseOutboxIds = (baseOutbox.mutations || []).map((mutation) => String(mutation.id));
|
|
54
|
+
const branchName = normalizeName(name);
|
|
55
|
+
const branchHost = createHost(bytes, schemaVersion, branchName);
|
|
56
|
+
branchHost.ensureBooted();
|
|
57
|
+
|
|
58
|
+
this.active = {
|
|
59
|
+
id: generateBranchId(),
|
|
60
|
+
name: branchName,
|
|
61
|
+
createdAt: Date.now(),
|
|
62
|
+
host: branchHost,
|
|
63
|
+
baseOutboxIds,
|
|
64
|
+
mainDeltasDuringReview: 0,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return this.inspect();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Record that `main` received remote deltas while this branch was open.
|
|
71
|
+
noteMainDelta(count = 1) {
|
|
72
|
+
if (!this.active) return null;
|
|
73
|
+
this.active.mainDeltasDuringReview += Math.max(1, Number(count) || 1);
|
|
74
|
+
return this.inspect();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Current preview status + staged OutboxMutation diff.
|
|
78
|
+
inspect() {
|
|
79
|
+
if (!this.active) return { active: false };
|
|
80
|
+
|
|
81
|
+
const summary = this.active.host.branchOutbox(this.active.baseOutboxIds);
|
|
82
|
+
return {
|
|
83
|
+
active: true,
|
|
84
|
+
id: this.active.id,
|
|
85
|
+
name: this.active.name,
|
|
86
|
+
createdAt: this.active.createdAt,
|
|
87
|
+
stagedCount: summary.count || 0,
|
|
88
|
+
byOperation: summary.by_operation || {},
|
|
89
|
+
byType: summary.by_type || {},
|
|
90
|
+
mutations: summary.mutations || [],
|
|
91
|
+
mainDeltasDuringReview: this.active.mainDeltasDuringReview,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Extract ONLY branch mutations and replay them onto current `main` via `onMain`.
|
|
96
|
+
async approve({ conflict = "lww", onMain }) {
|
|
97
|
+
if (!this.active) {
|
|
98
|
+
throw new Error("No active staging branch to approve");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const branch = this.active;
|
|
102
|
+
const summary = branch.host.branchOutbox(branch.baseOutboxIds);
|
|
103
|
+
const mutations = summary.mutations || [];
|
|
104
|
+
|
|
105
|
+
const replay = await onMain(mutations, conflict);
|
|
106
|
+
this.active = null;
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
approved: true,
|
|
110
|
+
id: branch.id,
|
|
111
|
+
name: branch.name,
|
|
112
|
+
mutationsCount: mutations.length,
|
|
113
|
+
applied: replay.applied || 0,
|
|
114
|
+
skipped: replay.skipped || 0,
|
|
115
|
+
conflicts: replay.conflicts || [],
|
|
116
|
+
changes: replay.changes || null,
|
|
117
|
+
mainDeltasDuringReview: branch.mainDeltasDuringReview,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Drop the staging branch with zero writes to `main` and zero server impact.
|
|
122
|
+
discard() {
|
|
123
|
+
if (!this.active) {
|
|
124
|
+
throw new Error("No active staging branch to discard");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const branch = this.active;
|
|
128
|
+
const summary = branch.host.branchOutbox(branch.baseOutboxIds);
|
|
129
|
+
const dropped = summary.count || 0;
|
|
130
|
+
this.active = null;
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
discarded: true,
|
|
134
|
+
id: branch.id,
|
|
135
|
+
name: branch.name,
|
|
136
|
+
dropped,
|
|
137
|
+
mainDeltasDuringReview: branch.mainDeltasDuringReview,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
// Visual staging-branch preview overlay for compiled InstantRecord apps.
|
|
2
|
+
//
|
|
3
|
+
// Mounted in a Shadow DOM host by `main.js` so every compiled Rails app gets a
|
|
4
|
+
// consistent, style-isolated staging preview bar, expandable OutboxMutation diff
|
|
5
|
+
// inspector, and Approve & Sync / Discard controls without editing host views.
|
|
6
|
+
|
|
7
|
+
export function formatBreakdown(byOperation = {}) {
|
|
8
|
+
const parts = [];
|
|
9
|
+
if (byOperation.create) parts.push(`${byOperation.create} created`);
|
|
10
|
+
if (byOperation.update) parts.push(`${byOperation.update} updated`);
|
|
11
|
+
if (byOperation.destroy) parts.push(`${byOperation.destroy} deleted`);
|
|
12
|
+
return parts.join(" · ");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function formatMutationSummary(mutation) {
|
|
16
|
+
const op = String(mutation.operation || "change").toUpperCase();
|
|
17
|
+
const type = String(mutation.record_type || "Record");
|
|
18
|
+
const id = String(mutation.record_id || "").slice(0, 8);
|
|
19
|
+
const changes = mutation.changes && typeof mutation.changes === "object"
|
|
20
|
+
? Object.keys(mutation.changes).filter((key) => !["id", "created_at", "updated_at", "server_version", "sync_state"].includes(key))
|
|
21
|
+
: [];
|
|
22
|
+
const fields = changes.length ? ` (${changes.join(", ")})` : "";
|
|
23
|
+
return `${op} ${type}#${id}${fields}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function formatOutcomeNotice(action) {
|
|
27
|
+
if (!action) return "";
|
|
28
|
+
if (action.approved) {
|
|
29
|
+
const conflictNote = action.skipped > 0
|
|
30
|
+
? ` · ${action.skipped} skipped (LWW conflict)`
|
|
31
|
+
: "";
|
|
32
|
+
const rebaseNote = action.mainDeltasDuringReview > 0
|
|
33
|
+
? ` · preserved ${action.mainDeltasDuringReview} main ${action.mainDeltasDuringReview === 1 ? "update" : "updates"}`
|
|
34
|
+
: "";
|
|
35
|
+
return `Approved '${action.name}': ${action.applied} ${action.applied === 1 ? "mutation" : "mutations"} replayed onto main${conflictNote}${rebaseNote}`;
|
|
36
|
+
}
|
|
37
|
+
if (action.discarded) {
|
|
38
|
+
return `Discarded '${action.name}': ${action.dropped} staged ${action.dropped === 1 ? "change" : "changes"} dropped · 0 server impact`;
|
|
39
|
+
}
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const STYLES = `
|
|
44
|
+
:host {
|
|
45
|
+
all: initial;
|
|
46
|
+
font-family: Figtree, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
47
|
+
color: #302B26;
|
|
48
|
+
}
|
|
49
|
+
.launcher {
|
|
50
|
+
position: fixed;
|
|
51
|
+
right: 16px;
|
|
52
|
+
bottom: 16px;
|
|
53
|
+
z-index: 9000;
|
|
54
|
+
display: inline-flex;
|
|
55
|
+
align-items: center;
|
|
56
|
+
gap: 8px;
|
|
57
|
+
height: 36px;
|
|
58
|
+
padding: 0 14px;
|
|
59
|
+
border: 1px solid #E2D9CC;
|
|
60
|
+
border-radius: 9999px;
|
|
61
|
+
background: #FFFFFF;
|
|
62
|
+
color: #302B26;
|
|
63
|
+
font: 600 13px/1 inherit;
|
|
64
|
+
box-shadow: 0 10px 24px -14px rgba(48, 43, 38, 0.35), 0 0 0 1px rgba(10, 10, 10, 0.04);
|
|
65
|
+
cursor: pointer;
|
|
66
|
+
}
|
|
67
|
+
.launcher:hover { background: #FBF8F2; }
|
|
68
|
+
.launcher:disabled { opacity: 0.6; cursor: default; }
|
|
69
|
+
.dot {
|
|
70
|
+
width: 8px;
|
|
71
|
+
height: 8px;
|
|
72
|
+
border-radius: 50%;
|
|
73
|
+
background: #B07A2A;
|
|
74
|
+
flex: none;
|
|
75
|
+
}
|
|
76
|
+
.banner {
|
|
77
|
+
position: fixed;
|
|
78
|
+
left: 50%;
|
|
79
|
+
bottom: 16px;
|
|
80
|
+
transform: translateX(-50%);
|
|
81
|
+
z-index: 9100;
|
|
82
|
+
width: min(760px, calc(100vw - 24px));
|
|
83
|
+
border-radius: 16px;
|
|
84
|
+
background: #302B26;
|
|
85
|
+
color: #FBF8F2;
|
|
86
|
+
box-shadow: 0 20px 44px -18px rgba(48, 43, 38, 0.65), 0 0 0 1px rgba(251, 248, 242, 0.14);
|
|
87
|
+
overflow: hidden;
|
|
88
|
+
}
|
|
89
|
+
.bar {
|
|
90
|
+
display: flex;
|
|
91
|
+
flex-wrap: wrap;
|
|
92
|
+
align-items: center;
|
|
93
|
+
justify-content: space-between;
|
|
94
|
+
gap: 10px 14px;
|
|
95
|
+
padding: 10px 14px;
|
|
96
|
+
}
|
|
97
|
+
.meta {
|
|
98
|
+
display: flex;
|
|
99
|
+
flex-wrap: wrap;
|
|
100
|
+
align-items: center;
|
|
101
|
+
gap: 8px;
|
|
102
|
+
min-width: 0;
|
|
103
|
+
}
|
|
104
|
+
.pill {
|
|
105
|
+
display: inline-flex;
|
|
106
|
+
align-items: center;
|
|
107
|
+
gap: 6px;
|
|
108
|
+
height: 24px;
|
|
109
|
+
padding: 0 10px;
|
|
110
|
+
border-radius: 9999px;
|
|
111
|
+
background: rgba(251, 239, 216, 0.16);
|
|
112
|
+
color: #FBEFD8;
|
|
113
|
+
font-size: 12px;
|
|
114
|
+
font-weight: 600;
|
|
115
|
+
white-space: nowrap;
|
|
116
|
+
}
|
|
117
|
+
.pill-rebase {
|
|
118
|
+
background: rgba(62, 138, 91, 0.28);
|
|
119
|
+
color: #D7F5E3;
|
|
120
|
+
}
|
|
121
|
+
.summary {
|
|
122
|
+
font-size: 13px;
|
|
123
|
+
color: rgba(251, 248, 242, 0.88);
|
|
124
|
+
}
|
|
125
|
+
.actions {
|
|
126
|
+
display: flex;
|
|
127
|
+
align-items: center;
|
|
128
|
+
gap: 8px;
|
|
129
|
+
}
|
|
130
|
+
button.btn {
|
|
131
|
+
height: 30px;
|
|
132
|
+
padding: 0 12px;
|
|
133
|
+
border: 0;
|
|
134
|
+
border-radius: 9999px;
|
|
135
|
+
font: 600 12px/1 inherit;
|
|
136
|
+
cursor: pointer;
|
|
137
|
+
white-space: nowrap;
|
|
138
|
+
}
|
|
139
|
+
button.btn:disabled { opacity: 0.55; cursor: default; }
|
|
140
|
+
button.btn-diff {
|
|
141
|
+
background: rgba(255, 255, 255, 0.12);
|
|
142
|
+
color: #FBF8F2;
|
|
143
|
+
}
|
|
144
|
+
button.btn-diff:hover { background: rgba(255, 255, 255, 0.2); }
|
|
145
|
+
button.btn-discard {
|
|
146
|
+
background: rgba(255, 233, 228, 0.16);
|
|
147
|
+
color: #FFD6CE;
|
|
148
|
+
}
|
|
149
|
+
button.btn-discard:hover { background: rgba(255, 233, 228, 0.26); }
|
|
150
|
+
button.btn-approve {
|
|
151
|
+
background: #3E8A5B;
|
|
152
|
+
color: #FFFFFF;
|
|
153
|
+
}
|
|
154
|
+
button.btn-approve:hover { filter: brightness(1.08); }
|
|
155
|
+
.diff {
|
|
156
|
+
max-height: 220px;
|
|
157
|
+
overflow-y: auto;
|
|
158
|
+
border-top: 1px solid rgba(251, 248, 242, 0.12);
|
|
159
|
+
padding: 8px 14px 10px;
|
|
160
|
+
background: rgba(0, 0, 0, 0.18);
|
|
161
|
+
font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
162
|
+
font-size: 11px;
|
|
163
|
+
}
|
|
164
|
+
.diff ul {
|
|
165
|
+
list-style: none;
|
|
166
|
+
margin: 0;
|
|
167
|
+
padding: 0;
|
|
168
|
+
display: flex;
|
|
169
|
+
flex-direction: column;
|
|
170
|
+
gap: 4px;
|
|
171
|
+
}
|
|
172
|
+
.diff li {
|
|
173
|
+
color: rgba(251, 248, 242, 0.88);
|
|
174
|
+
word-break: break-word;
|
|
175
|
+
}
|
|
176
|
+
.notice {
|
|
177
|
+
position: fixed;
|
|
178
|
+
left: 50%;
|
|
179
|
+
bottom: 62px;
|
|
180
|
+
transform: translateX(-50%);
|
|
181
|
+
z-index: 9050;
|
|
182
|
+
max-width: min(680px, calc(100vw - 24px));
|
|
183
|
+
padding: 8px 14px;
|
|
184
|
+
border-radius: 9999px;
|
|
185
|
+
background: #302B26;
|
|
186
|
+
color: #FBF8F2;
|
|
187
|
+
font-size: 12px;
|
|
188
|
+
font-weight: 500;
|
|
189
|
+
box-shadow: 0 12px 26px -14px rgba(48, 43, 38, 0.5);
|
|
190
|
+
}
|
|
191
|
+
.edge {
|
|
192
|
+
position: fixed;
|
|
193
|
+
inset: 0;
|
|
194
|
+
z-index: 8999;
|
|
195
|
+
pointer-events: none;
|
|
196
|
+
box-shadow: inset 0 0 0 3px rgba(176, 122, 42, 0.55);
|
|
197
|
+
}
|
|
198
|
+
`;
|
|
199
|
+
|
|
200
|
+
export function mountBranchPreview(api) {
|
|
201
|
+
if (typeof document === "undefined") return () => {};
|
|
202
|
+
|
|
203
|
+
let state = { active: false };
|
|
204
|
+
let diffOpen = false;
|
|
205
|
+
let busy = false;
|
|
206
|
+
let noticeText = "";
|
|
207
|
+
let noticeTimer = null;
|
|
208
|
+
let hostEl = null;
|
|
209
|
+
let shadow = null;
|
|
210
|
+
|
|
211
|
+
function ensureMounted() {
|
|
212
|
+
if (!document.body) return false;
|
|
213
|
+
if (hostEl && hostEl.isConnected) return true;
|
|
214
|
+
hostEl = document.createElement("div");
|
|
215
|
+
hostEl.id = "instant-record-branch-preview";
|
|
216
|
+
hostEl.setAttribute("data-turbo-permanent", "");
|
|
217
|
+
shadow = hostEl.attachShadow({ mode: "open" });
|
|
218
|
+
document.body.appendChild(hostEl);
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function showNotice(text) {
|
|
223
|
+
clearTimeout(noticeTimer);
|
|
224
|
+
noticeText = text;
|
|
225
|
+
render();
|
|
226
|
+
if (text) {
|
|
227
|
+
noticeTimer = setTimeout(() => {
|
|
228
|
+
noticeText = "";
|
|
229
|
+
render();
|
|
230
|
+
}, 5000);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function runAction(fn) {
|
|
235
|
+
if (busy) return;
|
|
236
|
+
busy = true;
|
|
237
|
+
render();
|
|
238
|
+
try {
|
|
239
|
+
const result = await fn();
|
|
240
|
+
const text = formatOutcomeNotice(result);
|
|
241
|
+
if (text) showNotice(text);
|
|
242
|
+
} catch (error) {
|
|
243
|
+
showNotice(error?.message || String(error));
|
|
244
|
+
} finally {
|
|
245
|
+
busy = false;
|
|
246
|
+
render();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function render() {
|
|
251
|
+
if (!ensureMounted()) return;
|
|
252
|
+
shadow.replaceChildren();
|
|
253
|
+
|
|
254
|
+
const style = document.createElement("style");
|
|
255
|
+
style.textContent = STYLES;
|
|
256
|
+
shadow.appendChild(style);
|
|
257
|
+
|
|
258
|
+
if (noticeText) {
|
|
259
|
+
const toast = document.createElement("div");
|
|
260
|
+
toast.className = "notice";
|
|
261
|
+
toast.setAttribute("role", "status");
|
|
262
|
+
toast.textContent = noticeText;
|
|
263
|
+
shadow.appendChild(toast);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (!state.active) {
|
|
267
|
+
const launcher = document.createElement("button");
|
|
268
|
+
launcher.type = "button";
|
|
269
|
+
launcher.className = "launcher";
|
|
270
|
+
launcher.disabled = busy;
|
|
271
|
+
launcher.setAttribute("data-branch-action", "fork");
|
|
272
|
+
const dot = document.createElement("span");
|
|
273
|
+
dot.className = "dot";
|
|
274
|
+
launcher.append(dot, document.createTextNode(busy ? "Staging…" : "Stage branch"));
|
|
275
|
+
launcher.addEventListener("click", () => runAction(() => api.fork("staging")));
|
|
276
|
+
shadow.appendChild(launcher);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const edge = document.createElement("div");
|
|
281
|
+
edge.className = "edge";
|
|
282
|
+
edge.setAttribute("aria-hidden", "true");
|
|
283
|
+
shadow.appendChild(edge);
|
|
284
|
+
|
|
285
|
+
const banner = document.createElement("aside");
|
|
286
|
+
banner.className = "banner";
|
|
287
|
+
banner.setAttribute("role", "region");
|
|
288
|
+
banner.setAttribute("aria-label", "Staging branch preview");
|
|
289
|
+
|
|
290
|
+
const bar = document.createElement("div");
|
|
291
|
+
bar.className = "bar";
|
|
292
|
+
|
|
293
|
+
const meta = document.createElement("div");
|
|
294
|
+
meta.className = "meta";
|
|
295
|
+
|
|
296
|
+
const pill = document.createElement("span");
|
|
297
|
+
pill.className = "pill";
|
|
298
|
+
const dot = document.createElement("span");
|
|
299
|
+
dot.className = "dot";
|
|
300
|
+
pill.append(dot, document.createTextNode(`Staging · ${state.name}`));
|
|
301
|
+
meta.appendChild(pill);
|
|
302
|
+
|
|
303
|
+
const summary = document.createElement("span");
|
|
304
|
+
summary.className = "summary";
|
|
305
|
+
const breakdown = formatBreakdown(state.byOperation);
|
|
306
|
+
summary.textContent = state.stagedCount > 0
|
|
307
|
+
? `${state.stagedCount} staged ${state.stagedCount === 1 ? "change" : "changes"}${breakdown ? ` (${breakdown})` : ""} · offline`
|
|
308
|
+
: "0 staged changes · edits stay offline until approved";
|
|
309
|
+
meta.appendChild(summary);
|
|
310
|
+
|
|
311
|
+
if (state.mainDeltasDuringReview > 0) {
|
|
312
|
+
const rebase = document.createElement("span");
|
|
313
|
+
rebase.className = "pill pill-rebase";
|
|
314
|
+
rebase.textContent = `+${state.mainDeltasDuringReview} on main (will rebase)`;
|
|
315
|
+
meta.appendChild(rebase);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const actions = document.createElement("div");
|
|
319
|
+
actions.className = "actions";
|
|
320
|
+
|
|
321
|
+
if (state.stagedCount > 0) {
|
|
322
|
+
const diffBtn = document.createElement("button");
|
|
323
|
+
diffBtn.type = "button";
|
|
324
|
+
diffBtn.className = "btn btn-diff";
|
|
325
|
+
diffBtn.textContent = diffOpen ? "Hide diff" : "Inspect diff";
|
|
326
|
+
diffBtn.addEventListener("click", () => {
|
|
327
|
+
diffOpen = !diffOpen;
|
|
328
|
+
render();
|
|
329
|
+
});
|
|
330
|
+
actions.appendChild(diffBtn);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const discardBtn = document.createElement("button");
|
|
334
|
+
discardBtn.type = "button";
|
|
335
|
+
discardBtn.className = "btn btn-discard";
|
|
336
|
+
discardBtn.disabled = busy;
|
|
337
|
+
discardBtn.setAttribute("data-branch-action", "discard");
|
|
338
|
+
discardBtn.textContent = "Discard";
|
|
339
|
+
discardBtn.addEventListener("click", () => {
|
|
340
|
+
diffOpen = false;
|
|
341
|
+
runAction(() => api.discard());
|
|
342
|
+
});
|
|
343
|
+
actions.appendChild(discardBtn);
|
|
344
|
+
|
|
345
|
+
const approveBtn = document.createElement("button");
|
|
346
|
+
approveBtn.type = "button";
|
|
347
|
+
approveBtn.className = "btn btn-approve";
|
|
348
|
+
approveBtn.disabled = busy;
|
|
349
|
+
approveBtn.setAttribute("data-branch-action", "approve");
|
|
350
|
+
approveBtn.textContent = busy ? "Replaying…" : "Approve & Sync";
|
|
351
|
+
approveBtn.addEventListener("click", () => {
|
|
352
|
+
diffOpen = false;
|
|
353
|
+
runAction(() => api.approve({ conflict: "lww" }));
|
|
354
|
+
});
|
|
355
|
+
actions.appendChild(approveBtn);
|
|
356
|
+
|
|
357
|
+
bar.append(meta, actions);
|
|
358
|
+
banner.appendChild(bar);
|
|
359
|
+
|
|
360
|
+
if (diffOpen && Array.isArray(state.mutations) && state.mutations.length > 0) {
|
|
361
|
+
const diff = document.createElement("div");
|
|
362
|
+
diff.className = "diff";
|
|
363
|
+
const list = document.createElement("ul");
|
|
364
|
+
for (const mutation of state.mutations) {
|
|
365
|
+
const item = document.createElement("li");
|
|
366
|
+
item.textContent = formatMutationSummary(mutation);
|
|
367
|
+
list.appendChild(item);
|
|
368
|
+
}
|
|
369
|
+
diff.appendChild(list);
|
|
370
|
+
banner.appendChild(diff);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
shadow.appendChild(banner);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const onBranchChanged = (event) => {
|
|
377
|
+
state = event.detail || { active: false };
|
|
378
|
+
if (!state.active) diffOpen = false;
|
|
379
|
+
if (state.lastAction) showNotice(formatOutcomeNotice(state.lastAction));
|
|
380
|
+
render();
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const onDomSwap = () => render();
|
|
384
|
+
|
|
385
|
+
window.addEventListener("instant-record:branch-changed", onBranchChanged);
|
|
386
|
+
window.addEventListener("instant-record:dom-written", onDomSwap);
|
|
387
|
+
document.addEventListener("turbo:load", onDomSwap);
|
|
388
|
+
render();
|
|
389
|
+
|
|
390
|
+
return () => {
|
|
391
|
+
clearTimeout(noticeTimer);
|
|
392
|
+
window.removeEventListener("instant-record:branch-changed", onBranchChanged);
|
|
393
|
+
window.removeEventListener("instant-record:dom-written", onDomSwap);
|
|
394
|
+
document.removeEventListener("turbo:load", onDomSwap);
|
|
395
|
+
hostEl?.remove();
|
|
396
|
+
};
|
|
397
|
+
}
|