Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

snap wip

Manav Rathi221c79a6b83e
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 73280c0f70..f28496349f 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -71,9 +71,8 @@ class MLState {
isSyncing = false;
/**
- * Subscriptions to {@link MLStatus} updates.
- *
- * See {@link mlStatusSubscribe}.
+ * Subscriptions to {@link MLStatus} updates attached using
+ * {@link mlStatusSubscribe}.
*/
mlStatusListeners: (() => void)[] = [];
@@ -84,15 +83,14 @@ class MLState {
mlStatusSnapshot: MLStatus | undefined;
/**
- * Subscriptions to updates to the {@link PeopleState}.
- *
- * See {@link peopleStateSubscribe}.
+ * Subscriptions to updates to the {@link PeopleState} attached using
+ * {@link peopleStateSubscribe}.
*/
peopleStateListeners: (() => void)[] = [];
/**
- * Snapshot of the {@link PeopleState}s. Use the {@link peopleStateSnapshot}
- * function to access this data.
+ * Snapshot of the {@link PeopleState} return by the
+ * {@link peopleStateSnapshot} function.
*
* It will be `undefined` only if ML is disabled. Otherwise, it will be an
* empty array even if the snapshot is pending its first sync.
@@ -416,13 +414,7 @@ export type MLStatus =
/**
* A function that can be used to subscribe to updates in the ML status.
*
- * This, along with {@link mlStatusSnapshot}, is meant to be used as arguments
- * to React's {@link useSyncExternalStore}.
- *
- * @param callback A function that will be invoked whenever the result of
- * {@link mlStatusSnapshot} changes.
- *
- * @returns A function that can be used to clear the subscription.
+ * See: [Note: Snapshots and useSyncExternalStore].
*/
export const mlStatusSubscribe = (onChange: () => void): (() => void) => {
_state.mlStatusListeners.push(onChange);
@@ -436,8 +428,7 @@ export const mlStatusSubscribe = (onChange: () => void): (() => void) => {
/**
* Return the last known, cached {@link MLStatus}.
*
- * This, along with {@link mlStatusSnapshot}, is meant to be used as arguments
- * to React's {@link useSyncExternalStore}.
+ * See also {@link mlStatusSubscribe}.
*
* A return value of `undefined` indicates that we're still performing the
* asynchronous tasks that are needed to get the status.
@@ -537,13 +528,7 @@ const workerDidUpdateStatus = throttled(updateMLStatusSnapshot, 2000);
/**
* A function that can be used to subscribe to updates to {@link Person}s.
*
- * This, along with {@link peopleStateSnapshot}, is meant to be used as
- * arguments to React's {@link useSyncExternalStore}.
- *
- * @param callback A function that will be invoked whenever the result of
- * {@link peopleStateSnapshot} changes.
- *
- * @returns A function that can be used to clear the subscription.
+ * See: [Note: Snapshots and useSyncExternalStore].
*/
export const peopleStateSubscribe = (onChange: () => void): (() => void) => {
_state.peopleStateListeners.push(onChange);
@@ -570,8 +555,7 @@ const resetPeopleStateSnapshot = () =>
/**
* Return the last known, cached {@link PeopleState}.
*
- * This, along with {@link peopleStateSubscribe}, is meant to be used as
- * arguments to React's {@link useSyncExternalStore}.
+ * See also {@link peopleStateSubscribe}.
*
* A return value of `undefined` indicates that ML is disabled. In all other
* cases, the list of people will be either empty (if we're either still loading
diff --git a/web/packages/new/photos/services/settings.ts b/web/packages/new/photos/services/settings.ts
index 92a6a2d149..39f3919eea 100644
--- a/web/packages/new/photos/services/settings.ts
+++ b/web/packages/new/photos/services/settings.ts
@@ -82,9 +82,8 @@ class SettingsState {
}
/**
- * Subscriptions to {@link Settings} updates.
- *
- * See {@link settingsSubscribe}.
+ * Subscriptions to {@link Settings} updates attached using
+ * {@link settingsSubscribe}.
*/
settingsListeners: (() => void)[] = [];
@@ -157,13 +156,7 @@ const syncSettingsSnapshotWithLocalStorage = () => {
/**
* A function that can be used to subscribe to updates to {@link Settings}.
*
- * This, along with {@link settingsSnapshot}, is meant to be used as arguments
- * to React's {@link useSyncExternalStore}.
- *
- * @param callback A function that will be invoked whenever the result of
- * {@link settingsSnapshot} changes.
- *
- * @returns A function that can be used to clear the subscription.
+ * See: [Note: Snapshots and useSyncExternalStore].
*/
export const settingsSubscribe = (onChange: () => void): (() => void) => {
_state.settingsListeners.push(onChange);
@@ -177,8 +170,7 @@ export const settingsSubscribe = (onChange: () => void): (() => void) => {
/**
* Return the last known, cached {@link Settings}.
*
- * This, along with {@link settingsSubscribe}, is meant to be used as
- * arguments to React's {@link useSyncExternalStore}.
+ * See also {@link settingsSubscribe}.
*/
export const settingsSnapshot = () => _state.settingsSnapshot;
diff --git a/web/packages/new/photos/services/user.ts b/web/packages/new/photos/services/user.ts
index ca0a1c42b2..650e37f2b5 100644
--- a/web/packages/new/photos/services/user.ts
+++ b/web/packages/new/photos/services/user.ts
@@ -1,4 +1,5 @@
import { authenticatedRequestHeaders, ensureOk } from "@/base/http";
+import { getKV } from "@/base/kv";
import { apiURL } from "@/base/origins";
import { z } from "zod";
import { FamilyData } from "./family";
@@ -10,10 +11,7 @@ const BonusData = z.object({
*/
storageBonuses: z
.object({
- /**
- * The type of the bonus.
- */
- type: z.string(),
+ type: z.string() /** The type of the bonus. */,
})
.array(),
});
@@ -45,9 +43,19 @@ export type UserDetails = z.infer<typeof UserDetails>;
*/
class UserState {
/**
- * Subscriptions to {@link UserDetails} updates.
+ * An arbitrary token to identify the current login.
*
- * See {@link userDetailsSubscribe}.
+ * It is used to discard stale completions.
+ */
+ id: number;
+
+ constructor() {
+ this.id = Math.random();
+ }
+
+ /**
+ * Subscriptions to {@link UserDetails} updates attached using
+ * {@link userDetailsSubscribe}.
*/
userDetailsListeners: (() => void)[] = [];
@@ -58,6 +66,78 @@ class UserState {
userDetailsSnapshot: UserDetails | undefined;
}
+/** State shared by the functions in this module. See {@link UserState}. */
+let _state = new UserState();
+
+export const logoutUserDetails = () => {
+ _state = new UserState();
+};
+
+/**
+ * Read in the locally persisted settings into memory, but otherwise do not
+ * initate any network requests to fetch the latest values.
+ *
+ * This assumes that the user is already logged in.
+ */
+export const initUserDetails = async () => {
+ return syncUserDetailsSnapshotWithLocalDB();
+};
+/**
+ * Fetch the user's details from remote and save them in local storage for
+ * subsequent lookup, and also update our in-memory snapshots.
+ */
+export const syncUserDetails = async () => {};
+
+/**
+ * Fetch user details from remote.
+ */
+export const getUserDetailsV2 = async () => {
+ const res = await fetch(await apiURL("/users/details/v2"), {
+ headers: await authenticatedRequestHeaders(),
+ });
+ ensureOk(res);
+ return UserDetails.parse(await res.json());
+};
+
+const syncUserDetailsSnapshotWithLocalDB = async () => {
+ const userDetails = UserDetails.parse(getKV("userDetails"));
+};
+
+/**
+ * A function that can be used to subscribe to updates to {@link UserDetails}.
+ *
+ * [Note: Snapshots and useSyncExternalStore]
+ *
+ * This subscribe function, along with {@link userDetailsSnapshot}, is meant to
+ * be used as arguments to React's {@link useSyncExternalStore}.
+ *
+ * @param callback A function that will be invoked whenever the result of
+ * {@link settingsSnapshot} changes.
+ *
+ * @returns A function that can be used to clear the subscription.
+ */
+export const userDetailsSubscribe = (onChange: () => void): (() => void) => {
+ _state.userDetailsListeners.push(onChange);
+ return () => {
+ _state.userDetailsListeners = _state.userDetailsListeners.filter(
+ (l) => l != onChange,
+ );
+ };
+};
+
+/**
+ * Return the last known, cached {@link UserDetails}.
+ *
+ * This, along with {@link userDetailsSubscribe}, is meant to be used as
+ * arguments to React's {@link useSyncExternalStore}.
+ */
+export const settingsSnapshot = () => _state.settingsSnapshot;
+
+const setSettingsSnapshot = (snapshot: Settings) => {
+ _state.settingsSnapshot = snapshot;
+ _state.settingsListeners.forEach((l) => l());
+};
+
/**
* Fetch the two-factor status (whether or not it is enabled) from remote.
*/