Skip to content

Commit a319802

Browse files
authored
fix(ui): keep dream diary navigation visible while scrolling (#118054)
1 parent 4b13410 commit a319802

3 files changed

Lines changed: 227 additions & 74 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { expectDefined } from "@openclaw/normalization-core";
2+
import { render } from "lit";
3+
import { afterEach, describe, expect, it } from "vitest";
4+
import { createDreamingViewState, renderDreaming } from "./view.ts";
5+
6+
const hasBrowserLayout = !navigator.userAgent.toLowerCase().includes("jsdom");
7+
let host: HTMLDivElement | undefined;
8+
9+
afterEach(() => {
10+
host?.remove();
11+
host = undefined;
12+
});
13+
14+
describe.skipIf(!hasBrowserLayout)("dream diary browser layout", () => {
15+
it("keeps diary dates visible and clickable while a long entry scrolls", async () => {
16+
host = document.createElement("div");
17+
host.style.height = "420px";
18+
host.style.width = "760px";
19+
host.style.overflow = "hidden";
20+
document.body.append(host);
21+
22+
const viewState = createDreamingViewState();
23+
viewState.activeSubTab = "diary";
24+
viewState.activeDiarySubTab = "dreams";
25+
const props: Parameters<typeof renderDreaming>[0] = {
26+
viewState,
27+
active: true,
28+
selectedAgentId: "main",
29+
shortTermCount: 0,
30+
promotedCount: 0,
31+
shortTermEntries: [],
32+
promotedEntries: [],
33+
dreamingOf: null,
34+
nextCycle: null,
35+
timezone: null,
36+
statusError: null,
37+
modeSaving: false,
38+
dreamDiaryLoading: false,
39+
dreamDiaryActionLoading: false,
40+
dreamDiaryActionMessage: null,
41+
dreamDiaryActionArchivePath: null,
42+
dreamDiaryError: null,
43+
dreamDiaryContent: [
44+
"# Dream Diary",
45+
"---",
46+
"*January 1, 2026*",
47+
"The earlier diary entry remains available.",
48+
"---",
49+
"*January 2, 2026*",
50+
...Array.from(
51+
{ length: 24 },
52+
(_, index) => `Long diary paragraph ${index + 1}: ${"a visible memory ".repeat(8)}`,
53+
),
54+
].join("\n\n"),
55+
memoryWikiEnabled: false,
56+
wikiImportInsightsLoading: false,
57+
wikiImportInsightsError: null,
58+
wikiImportInsights: null,
59+
wikiOverviewLoading: false,
60+
wikiOverviewError: null,
61+
wikiOverview: null,
62+
onRefreshDiary: () => {},
63+
onRefreshImports: () => {},
64+
onRefreshWikiOverview: () => {},
65+
onOpenConfig: () => {},
66+
onOpenWikiPage: async () => null,
67+
onBackfillDiary: () => {},
68+
onCopyDreamingArchivePath: () => {},
69+
onDedupeDreamDiary: () => {},
70+
onResetDiary: () => {},
71+
onResetGroundedShortTerm: () => {},
72+
onRepairDreamingArtifacts: () => {},
73+
onViewStateChange: () => render(renderDreaming(props), host!),
74+
};
75+
render(renderDreaming(props), host);
76+
77+
const diary = host.querySelector<HTMLElement>(".dreams-diary");
78+
const navigation = host.querySelector<HTMLElement>(".dreams-diary__daychips");
79+
expect(diary).not.toBeNull();
80+
expect(navigation).not.toBeNull();
81+
expect(getComputedStyle(diary!).overflowY).toBe("auto");
82+
expect(getComputedStyle(navigation!.parentElement!).position).toBe("sticky");
83+
expect(diary!.scrollHeight).toBeGreaterThan(diary!.clientHeight);
84+
85+
diary!.scrollTop = 500;
86+
await new Promise<void>((resolve) => {
87+
requestAnimationFrame(() => resolve());
88+
});
89+
90+
const olderDate = expectDefined(
91+
navigation!.querySelectorAll<HTMLButtonElement>(".dreams-diary__day-chip")[1],
92+
"older diary date button",
93+
);
94+
expect(diary!.scrollTop).toBeGreaterThan(100);
95+
expect(olderDate.getBoundingClientRect().top).toBeGreaterThanOrEqual(
96+
diary!.getBoundingClientRect().top,
97+
);
98+
expect(olderDate.getBoundingClientRect().bottom).toBeLessThanOrEqual(
99+
diary!.getBoundingClientRect().bottom,
100+
);
101+
102+
olderDate.click();
103+
expect(viewState.diaryPage).toBe(1);
104+
expect(host.querySelector(".dreams-diary__date")?.textContent).toBe("January 1, 2026");
105+
});
106+
});

‎ui/src/pages/agents/memory/view.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @vitest-environment jsdom */
22

3+
import { expectDefined } from "@openclaw/normalization-core";
34
import { render } from "lit";
45
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
56
import { i18n } from "../../../i18n/index.ts";
@@ -835,6 +836,50 @@ describe("dreaming view", () => {
835836
setDreamSubTab("scene");
836837
});
837838

839+
it.each([
840+
{ tab: "dreams", labels: ["1/2", "1/1"] },
841+
{ tab: "insights", labels: ["Travel", "Health"] },
842+
{ tab: "wiki", labels: ["Syntheses", "Concepts"] },
843+
] as const)("keeps $tab navigation inside the sticky diary controls", ({ tab, labels }) => {
844+
setDreamSubTab("diary");
845+
setDreamDiarySubTab(tab);
846+
const props = buildProps({
847+
dreamDiaryContent: [
848+
"# Dream Diary",
849+
"---",
850+
"*January 1, 2026*",
851+
"An earlier dream.",
852+
"---",
853+
"*January 2, 2026*",
854+
...Array.from({ length: 12 }, (_, index) => `Long diary paragraph ${index + 1}.`),
855+
].join("\n\n"),
856+
onViewStateChange: vi.fn(),
857+
});
858+
const wikiOverview = props.wikiOverview;
859+
if (wikiOverview) {
860+
const firstCluster = expectDefined(wikiOverview.clusters[0], "first memory wiki cluster");
861+
props.wikiOverview = {
862+
...wikiOverview,
863+
clusters: [
864+
...wikiOverview.clusters,
865+
{ ...firstCluster, key: "concept", label: "Concepts" },
866+
],
867+
};
868+
}
869+
870+
const container = renderInto(props);
871+
const stickyChrome = expectElement(container, ".dreams-diary__chrome");
872+
const navigation = expectElement(stickyChrome, ".dreams-diary__daychips");
873+
const buttons = [...navigation.querySelectorAll<HTMLButtonElement>(".dreams-diary__day-chip")];
874+
875+
expect(buttons.map((button) => compactText(button))).toEqual(labels);
876+
expect(container.querySelector("#dream-diary-panel .dreams-diary__daychips")).toBeNull();
877+
878+
buttons[1]?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
879+
expect(viewState.diaryPage).toBe(1);
880+
expect(props.onViewStateChange).toHaveBeenCalledOnce();
881+
});
882+
838883
it("renders diary empty, error, and removed-navigation states", () => {
839884
setDreamSubTab("diary");
840885
setDreamDiarySubTab("dreams");

‎ui/src/pages/agents/memory/view.ts‎

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ type DiaryEntry = {
2222
body: string;
2323
};
2424

25-
type DiaryEntryNav = {
26-
date: string;
27-
body: string;
28-
page: number;
29-
};
25+
type DiaryPanel =
26+
| ReturnType<typeof html>
27+
| { navigation: ReturnType<typeof html>; content: ReturnType<typeof html> };
3028

3129
const DIARY_START_RE = /<!--\s*openclaw:dreaming:diary:start\s*-->/;
3230
const DIARY_END_RE = /<!--\s*openclaw:dreaming:diary:end\s*-->/;
@@ -87,11 +85,6 @@ function formatDiaryChipLabel(date: string): string {
8785
return `${value.getMonth() + 1}/${value.getDate()}`;
8886
}
8987

90-
function buildDiaryNavigation(entries: DiaryEntry[]): DiaryEntryNav[] {
91-
const reversed = [...entries].toReversed();
92-
return reversed.map((entry, page) => Object.assign({}, entry, { page }));
93-
}
94-
9588
type DreamingPhaseInfo = {
9689
enabled: boolean;
9790
cron: string;
@@ -1112,6 +1105,29 @@ function renderWikiInsightCard(props: DreamingProps, card: WikiInsightCard) {
11121105
`;
11131106
}
11141107

1108+
function renderDiaryNavigation(props: DreamingProps, labels: string[], selectedPage: number) {
1109+
const state = props.viewState;
1110+
return html`
1111+
<div class="dreams-diary__daychips">
1112+
${labels.map(
1113+
(label, index) => html`
1114+
<button
1115+
class="dreams-diary__day-chip ${index === selectedPage
1116+
? "dreams-diary__day-chip--active"
1117+
: ""}"
1118+
@click=${() => {
1119+
setDiaryPage(state, index, labels.length);
1120+
props.onViewStateChange();
1121+
}}
1122+
>
1123+
${label}
1124+
</button>
1125+
`,
1126+
)}
1127+
</div>
1128+
`;
1129+
}
1130+
11151131
function renderWikiClusterSection<
11161132
Cluster extends { key: string; label: string; items: { pagePath: string }[] },
11171133
>(
@@ -1127,7 +1143,7 @@ function renderWikiClusterSection<
11271143
prose: (cluster: Cluster) => ReturnType<typeof html>;
11281144
renderItem: (item: Cluster["items"][number]) => ReturnType<typeof html>;
11291145
},
1130-
) {
1146+
): DiaryPanel {
11311147
const { clusters } = params;
11321148
if (clusters.length === 0) {
11331149
return html`
@@ -1150,31 +1166,21 @@ function renderWikiClusterSection<
11501166
? "selected imported insight cluster"
11511167
: "selected memory overview cluster",
11521168
);
1153-
return html`
1154-
<div class="dreams-diary__daychips">
1155-
${clusters.map(
1156-
(entry, index) => html`
1157-
<button
1158-
class="dreams-diary__day-chip ${index === clusterIndex
1159-
? "dreams-diary__day-chip--active"
1160-
: ""}"
1161-
@click=${() => {
1162-
setDiaryPage(state, index, clusters.length);
1163-
props.onViewStateChange();
1164-
}}
1165-
>
1166-
${entry.label}
1167-
</button>
1168-
`,
1169-
)}
1170-
</div>
1171-
<article class="dreams-diary__entry" key="${params.kind}-${cluster.key}">
1172-
<div class="dreams-diary__accent"></div>
1173-
<div class="dreams-diary__date">${params.date(cluster)}</div>
1174-
<div class="dreams-diary__prose">${params.prose(cluster)}</div>
1175-
<div class="dreams-diary__insights">${cluster.items.map(params.renderItem)}</div>
1176-
</article>
1177-
`;
1169+
return {
1170+
navigation: renderDiaryNavigation(
1171+
props,
1172+
clusters.map((entry) => entry.label),
1173+
clusterIndex,
1174+
),
1175+
content: html`
1176+
<article class="dreams-diary__entry" key="${params.kind}-${cluster.key}">
1177+
<div class="dreams-diary__accent"></div>
1178+
<div class="dreams-diary__date">${params.date(cluster)}</div>
1179+
<div class="dreams-diary__prose">${params.prose(cluster)}</div>
1180+
<div class="dreams-diary__insights">${cluster.items.map(params.renderItem)}</div>
1181+
</article>
1182+
`,
1183+
};
11781184
}
11791185

11801186
function renderDiaryImportsSection(props: DreamingProps) {
@@ -1262,7 +1268,7 @@ function renderWikiOverviewSection(props: DreamingProps) {
12621268
});
12631269
}
12641270

1265-
function renderDreamDiaryEntries(props: DreamingProps) {
1271+
function renderDreamDiaryEntries(props: DreamingProps): DiaryPanel {
12661272
const state = props.viewState;
12671273
if (typeof props.dreamDiaryContent !== "string") {
12681274
return html`
@@ -1289,41 +1295,31 @@ function renderDreamDiaryEntries(props: DreamingProps) {
12891295
`;
12901296
}
12911297

1292-
const reversed = buildDiaryNavigation(entries);
1298+
const reversed = entries.toReversed();
12931299
const page = Math.max(0, Math.min(state.diaryPage, reversed.length - 1));
12941300
const entry = expectDefined(reversed[page], "selected dreaming diary entry");
12951301

1296-
return html`
1297-
<div class="dreams-diary__daychips">
1298-
${reversed.map(
1299-
(e) => html`
1300-
<button
1301-
class="dreams-diary__day-chip ${e.page === page
1302-
? "dreams-diary__day-chip--active"
1303-
: ""}"
1304-
@click=${() => {
1305-
setDiaryPage(state, e.page, reversed.length);
1306-
props.onViewStateChange();
1307-
}}
1308-
>
1309-
${formatDiaryChipLabel(e.date)}
1310-
</button>
1311-
`,
1312-
)}
1313-
</div>
1314-
<article class="dreams-diary__entry" key="${page}">
1315-
<div class="dreams-diary__accent"></div>
1316-
${entry.date ? html`<time class="dreams-diary__date">${entry.date}</time>` : nothing}
1317-
<div class="dreams-diary__prose">
1318-
${flattenDiaryBody(entry.body).map(
1319-
(para, i) =>
1320-
html`<p class="dreams-diary__para" style="animation-delay: ${0.3 + i * 0.15}s;">
1321-
${unsafeHTML(toSanitizedMarkdownHtml(para))}
1322-
</p>`,
1323-
)}
1324-
</div>
1325-
</article>
1326-
`;
1302+
return {
1303+
navigation: renderDiaryNavigation(
1304+
props,
1305+
reversed.map((diaryEntry) => formatDiaryChipLabel(diaryEntry.date)),
1306+
page,
1307+
),
1308+
content: html`
1309+
<article class="dreams-diary__entry" key="${page}">
1310+
<div class="dreams-diary__accent"></div>
1311+
${entry.date ? html`<time class="dreams-diary__date">${entry.date}</time>` : nothing}
1312+
<div class="dreams-diary__prose">
1313+
${flattenDiaryBody(entry.body).map(
1314+
(para, i) =>
1315+
html`<p class="dreams-diary__para" style="animation-delay: ${0.3 + i * 0.15}s;">
1316+
${unsafeHTML(toSanitizedMarkdownHtml(para))}
1317+
</p>`,
1318+
)}
1319+
</div>
1320+
</article>
1321+
`,
1322+
};
13271323
}
13281324

13291325
// ── Diary section renderer ────────────────────────────────────────────
@@ -1347,6 +1343,15 @@ function renderDiarySection(props: DreamingProps) {
13471343
`;
13481344
}
13491345

1346+
const diaryPanel =
1347+
activeDiarySubTab === "dreams"
1348+
? renderDreamDiaryEntries(props)
1349+
: activeDiarySubTab === "insights"
1350+
? renderDiaryImportsSection(props)
1351+
: renderWikiOverviewSection(props);
1352+
const diaryNavigation = "navigation" in diaryPanel ? diaryPanel.navigation : nothing;
1353+
const diaryContent = "content" in diaryPanel ? diaryPanel.content : diaryPanel;
1354+
13501355
return html`
13511356
<section class="dreams-diary">
13521357
<div class="dreams-diary__chrome">
@@ -1409,6 +1414,7 @@ function renderDiarySection(props: DreamingProps) {
14091414
</button>
14101415
</div>
14111416
${renderDiarySubtabExplainer(activeDiarySubTab)}
1417+
${memoryWikiUnavailable ? nothing : diaryNavigation}
14121418
</div>
14131419
14141420
<div
@@ -1437,11 +1443,7 @@ function renderDiarySection(props: DreamingProps) {
14371443
</div>
14381444
</div>
14391445
`
1440-
: activeDiarySubTab === "dreams"
1441-
? renderDreamDiaryEntries(props)
1442-
: activeDiarySubTab === "insights"
1443-
? renderDiaryImportsSection(props)
1444-
: renderWikiOverviewSection(props)}
1446+
: diaryContent}
14451447
</div>
14461448
${renderWikiPreviewOverlay(props)}
14471449
</section>

0 commit comments

Comments
 (0)