Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | 1x 2x 2x 1x 1x 33x 11x 20x 7x 1x 6x 6x 2x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 11x 11x 11x 11x 11x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 11x 11x 24x 10x 1x 6x 6x 6x 7x 6x 1x 9x 9x 9x 9x 9x 9x 9x 1x 8x 9x 9x 9x 8x 8x 7x 7x 7x 9x 9x 9x 3x 1x 2x 3x 9x 8x 1x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x | import { computed, ref } from "vue";
import { defineStore } from "pinia";
import {
ApiError,
createApiClient,
createNotificationApi,
type BusinessId,
type InAppNotification,
type NotificationApi,
} from "@plain-journal/foundation";
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim() ?? "";
export interface NotificationAccessContext {
authenticated: boolean;
ownerId: BusinessId | null;
accessToken: string | null;
}
interface ActiveNotificationAccess {
ownerId: BusinessId;
accessToken: string;
revision: number;
api: NotificationApi;
}
export type NotificationReadRecovery =
| "confirmed-read"
| "still-unread"
| "missing"
| "unavailable";
export class NotificationAccessChangedError extends Error {
constructor() {
super("账户或会话已切换,旧的通知请求结果不会写入当前页面。");
this.name = "NotificationAccessChangedError";
}
}
export class NotificationContractError extends Error {
constructor(message: string) {
super(message);
this.name = "NotificationContractError";
}
}
function isActiveContext(context: NotificationAccessContext): context is {
authenticated: true;
ownerId: BusinessId;
accessToken: string;
} {
return context.authenticated
&& typeof context.ownerId === "string"
&& context.ownerId.length > 0
&& typeof context.accessToken === "string"
&& context.accessToken.length > 0;
}
function notificationApi(accessToken: string): NotificationApi {
return createNotificationApi(createApiClient({
baseUrl: apiBaseUrl,
timeoutMs: 8000,
tokenProvider: () => accessToken,
}));
}
function validateNotification(
value: InAppNotification,
): InAppNotification {
if (typeof value.id !== "string" || value.id.length === 0) {
throw new NotificationContractError(
"通知服务返回了非字符串业务 ID,页面已拒绝执行已读命令。",
);
}
Iif (!["UNREAD", "READ"].includes(value.status)) {
throw new NotificationContractError(
`通知服务返回了未识别状态 ${value.status},页面不会猜测其已读含义。`,
);
}
return value;
}
function resultMayBeUnknown(cause: unknown): boolean {
return cause instanceof ApiError && (
cause.kind === "network"
|| cause.kind === "timeout"
|| cause.kind === "invalid-response"
|| (cause.kind === "http" && (cause.status ?? 500) >= 500)
);
}
export const useNotificationStore = defineStore("customer-notifications", () => {
const notifications = ref<InAppNotification[]>([]);
const unreadCount = ref(0);
const nextCursor = ref<string | null>(null);
const hasMore = ref(false);
const loading = ref(false);
const loadingOlder = ref(false);
const markingReadId = ref<BusinessId | null>(null);
const error = ref<string | null>(null);
const readError = ref<string | null>(null);
const readUnknown = ref(false);
const pendingReadId = ref<BusinessId | null>(null);
const activeOwnerId = ref<BusinessId | null>(null);
let activeAccessToken: string | null = null;
let accessRevision = 0;
let loadRevision = 0;
let factRevision = 0;
const visibleUnreadCount = computed(() =>
notifications.value.filter((item) => item.status === "UNREAD").length);
function synchronizeAccess(
context: NotificationAccessContext,
): ActiveNotificationAccess | null {
const nextOwnerId = isActiveContext(context) ? context.ownerId : null;
const nextAccessToken = isActiveContext(context) ? context.accessToken : null;
const ownerChanged = activeOwnerId.value !== nextOwnerId;
const accessChanged = ownerChanged || activeAccessToken !== nextAccessToken;
if (accessChanged) {
activeOwnerId.value = nextOwnerId;
activeAccessToken = nextAccessToken;
accessRevision += 1;
loadRevision += 1;
loading.value = false;
loadingOlder.value = false;
markingReadId.value = null;
error.value = null;
readError.value = null;
readUnknown.value = false;
pendingReadId.value = null;
Eif (ownerChanged) {
notifications.value = [];
unreadCount.value = 0;
nextCursor.value = null;
hasMore.value = false;
}
}
Iif (!isActiveContext(context)) {
notifications.value = [];
unreadCount.value = 0;
nextCursor.value = null;
hasMore.value = false;
return null;
}
return {
ownerId: context.ownerId,
accessToken: context.accessToken,
revision: accessRevision,
api: notificationApi(context.accessToken),
};
}
function accessIsCurrent(access: ActiveNotificationAccess): boolean {
return access.revision === accessRevision
&& access.ownerId === activeOwnerId.value
&& access.accessToken === activeAccessToken;
}
function requireCurrent(access: ActiveNotificationAccess) {
if (!accessIsCurrent(access)) {
throw new NotificationAccessChangedError();
}
}
function mergeItems(
incoming: InAppNotification[],
append: boolean,
) {
const values = append ? [...notifications.value, ...incoming] : incoming;
const byId = new Map<BusinessId, InAppNotification>();
for (const item of values) {
byId.set(item.id, item);
}
notifications.value = [...byId.values()].sort((left, right) =>
Date.parse(right.createdAt) - Date.parse(left.createdAt));
}
async function load(
context: NotificationAccessContext,
options: { append?: boolean } = {},
): Promise<InAppNotification[]> {
const access = synchronizeAccess(context);
Iif (!access) {
return [];
}
const append = Boolean(options.append);
Iif (append && (!hasMore.value || !nextCursor.value || loadingOlder.value)) {
return notifications.value;
}
const requestRevision = ++loadRevision;
const requestedFactRevision = factRevision;
if (append) {
loadingOlder.value = true;
} else {
loading.value = true;
}
error.value = null;
try {
const [page, count] = await Promise.all([
access.api.notifications(append ? nextCursor.value ?? undefined : undefined, 20),
access.api.unreadCount(),
]);
requireCurrent(access);
Iif (
requestRevision !== loadRevision
|| requestedFactRevision !== factRevision
) {
throw new NotificationAccessChangedError();
}
const incoming = page.items.map(validateNotification);
mergeItems(incoming, append);
unreadCount.value = Math.max(0, Number(count.count) || 0);
nextCursor.value = page.nextCursor;
hasMore.value = page.hasMore;
return notifications.value;
} catch (cause) {
if (
!accessIsCurrent(access)
|| requestRevision !== loadRevision
|| requestedFactRevision !== factRevision
) {
throw new NotificationAccessChangedError();
}
error.value = cause instanceof Error
? cause.message
: "通知事实暂时无法读取。";
return notifications.value;
} finally {
if (accessIsCurrent(access) && requestRevision === loadRevision) {
if (append) {
loadingOlder.value = false;
} else {
loading.value = false;
}
}
}
}
async function markRead(
context: NotificationAccessContext,
notificationId: BusinessId,
): Promise<boolean> {
const access = synchronizeAccess(context);
Iif (!access) {
return false;
}
const current = notifications.value.find((item) => item.id === notificationId);
Iif (!current || current.status === "READ") {
return Boolean(current);
}
markingReadId.value = notificationId;
readError.value = null;
readUnknown.value = false;
pendingReadId.value = null;
try {
await access.api.markRead(notificationId);
requireCurrent(access);
factRevision += 1;
notifications.value = notifications.value.map((item) =>
item.id === notificationId
? { ...item, status: "READ", readAt: new Date().toISOString() }
: item);
unreadCount.value = Math.max(0, unreadCount.value - 1);
return true;
} catch (cause) {
requireCurrent(access);
Eif (resultMayBeUnknown(cause)) {
readUnknown.value = true;
pendingReadId.value = notificationId;
readError.value = "已读结果尚未确认。请先重新读取通知,不要把响应丢失当作未执行。";
return false;
}
readError.value = cause instanceof Error ? cause.message : "通知未能标记为已读。";
return false;
} finally {
Eif (accessIsCurrent(access) && markingReadId.value === notificationId) {
markingReadId.value = null;
}
}
}
async function reconcilePendingRead(
context: NotificationAccessContext,
): Promise<NotificationReadRecovery> {
const notificationId = pendingReadId.value;
Iif (!notificationId) {
await load(context);
return error.value ? "unavailable" : "missing";
}
await load(context);
if (error.value) {
readUnknown.value = true;
readError.value = "权威通知列表仍无法读取,已读结果继续保持待确认。";
return "unavailable";
}
const current = notifications.value.find((item) => item.id === notificationId);
readUnknown.value = false;
pendingReadId.value = null;
Iif (!current) {
readError.value = "权威通知列表中已找不到这条通知,请重新核对账户与通知入口。";
return "missing";
}
Eif (current.status === "READ") {
readError.value = null;
return "confirmed-read";
}
readError.value = "权威通知列表仍显示未读;现在可以再次提交幂等的已读命令。";
return "still-unread";
}
return {
notifications,
unreadCount,
visibleUnreadCount,
nextCursor,
hasMore,
loading,
loadingOlder,
markingReadId,
error,
readError,
readUnknown,
pendingReadId,
activeOwnerId,
load,
markRead,
reconcilePendingRead,
};
});
|