Skip to content

Commit 2d06507

Browse files
committed
feat(pages): make recent pages update in real-time
1 parent d735c6d commit 2d06507

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

apps/client/src/code/pages/page/page.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,12 @@ export class Page implements IPageRegion {
358358

359359
// Bump on recent pages
360360

361-
pull(this.app.react.recentPageIds, this.id);
362-
this.app.react.recentPageIds.unshift(this.id);
361+
internals.pages.recentPageIdsKeepOverride = true;
362+
internals.pages.react.recentPageIdsOverride = [
363+
...internals.pages.react.recentPageIds,
364+
];
365+
pull(this.app.react.recentPageIdsOverride!, this.id);
366+
this.app.react.recentPageIdsOverride!.unshift(this.id);
363367

364368
// Bump on server
365369

apps/client/src/code/pages/pages.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import type { Serialization } from './serialization';
1414

1515
export interface IAppReact {
1616
pathPageIds: string[];
17-
recentPageIds: string[];
17+
18+
recentPageIdsOverride?: string[];
19+
recentPageIds: ComputedRef<string[]>;
1820

1921
page: ShallowRef<Page>;
2022
pageId: ComputedRef<string | undefined>;
@@ -42,6 +44,8 @@ export class Pages {
4244

4345
parentPageId?: string;
4446

47+
recentPageIdsKeepOverride?: boolean;
48+
4549
constructor(input: { factories: Factories }) {
4650
this.factories = input.factories;
4751

@@ -50,7 +54,21 @@ export class Pages {
5054

5155
this.react = reactive({
5256
pathPageIds: [],
53-
recentPageIds: [],
57+
recentPageIds: computed(() => {
58+
if (this.recentPageIdsKeepOverride) {
59+
this.recentPageIdsKeepOverride = undefined;
60+
} else {
61+
this.react.recentPageIdsOverride = undefined;
62+
}
63+
64+
const recentPageIds = internals.realtime.globalCtx.hget(
65+
'user',
66+
authStore().userId,
67+
'recent-page-ids',
68+
);
69+
70+
return this.react.recentPageIdsOverride ?? recentPageIds ?? [];
71+
}),
5472

5573
page: shallowRef(null) as any,
5674
pageId: computed(() => this.react.page?.id),
@@ -82,17 +100,12 @@ export class Pages {
82100

83101
promises.push(
84102
(async () => {
85-
const [
86-
encryptedDefaultNote,
87-
encryptedDefaultArrow,
88-
recentPageIdsJSON,
89-
isNewUser,
90-
] = await internals.realtime.hmget('user', authStore().userId, [
91-
'encrypted-default-note',
92-
'encrypted-default-arrow',
93-
'recent-page-ids',
94-
'new',
95-
]);
103+
const [encryptedDefaultNote, encryptedDefaultArrow, isNewUser] =
104+
await internals.realtime.hmget('user', authStore().userId, [
105+
'encrypted-default-note',
106+
'encrypted-default-arrow',
107+
'new',
108+
]);
96109

97110
this.defaultNote = unpack(
98111
internals.symmetricKeyring.decrypt(encryptedDefaultNote, {
@@ -113,8 +126,6 @@ export class Pages {
113126
}),
114127
);
115128

116-
this.react.recentPageIds = recentPageIdsJSON ?? [];
117-
118129
this.react.isNewUser = !!isNewUser;
119130

120131
if (isNewUser) {

apps/client/src/layouts/PagesLayout/LeftSidebar/RecentPages.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ async function removeRecentPage(pageId: string) {
8383
try {
8484
await trpcClient.users.pages.removeRecentPage.mutate({ pageId });
8585
86-
pull(internals.pages.react.recentPageIds, pageId);
86+
internals.pages.recentPageIdsKeepOverride = true;
87+
internals.pages.react.recentPageIdsOverride = [
88+
...internals.pages.react.recentPageIds,
89+
];
90+
pull(internals.pages.react.recentPageIdsOverride, pageId);
8791
} catch (error) {
8892
handleError(error);
8993
}

packages/@deeplib/data/src/data-hashes/user/recent-page-ids.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { UserModel } from '@deeplib/db';
22
import type { DataField } from '@stdlib/data';
33

44
export const recentPageIds: DataField<UserModel> = {
5+
notifyUpdates: true,
6+
57
userGettable: ({ userId, suffix }) => userId === suffix,
68

79
columns: ['recent_page_ids'],

0 commit comments

Comments
 (0)