feat(store): add revision tracking to fileChangeStore

This commit is contained in:
2026-03-18 17:59:06 +08:00
parent cf903872b8
commit c0dca4477b

View File

@@ -31,6 +31,8 @@ interface AppFileState {
initialized: boolean;
/** Original order for each section: key is `${platform}:${fileType}`, value is ordered file IDs */
originalSectionOrders: Record<string, string[]>;
/** Server revision string from Kintone API, used to detect remote changes */
serverRevision: string | null;
}
interface FileChangeState {
@@ -93,6 +95,15 @@ interface FileChangeState {
hasPendingChanges: (domainId: string, appId: string) => boolean;
isInitialized: (domainId: string, appId: string) => boolean;
/** Store the revision string from Kintone API */
setServerRevision: (domainId: string, appId: string, revision: string) => void;
/** Get the stored revision string */
getServerRevision: (domainId: string, appId: string) => string | null;
/** Check if the revision has changed (indicating remote modifications) */
hasRemoteChange: (domainId: string, appId: string, currentRevision: string) => boolean;
}
const appKey = (domainId: string, appId: string) => `${domainId}:${appId}`;
@@ -125,7 +136,7 @@ export const useFileChangeStore = create<FileChangeState>()(
set((state) => ({
appFiles: {
...state.appFiles,
[key]: { files: entries, initialized: true, originalSectionOrders },
[key]: { files: entries, initialized: true, originalSectionOrders, serverRevision: null },
},
}));
},
@@ -137,6 +148,7 @@ export const useFileChangeStore = create<FileChangeState>()(
files: [],
initialized: true,
originalSectionOrders: {},
serverRevision: null,
};
return {
appFiles: {
@@ -244,6 +256,7 @@ export const useFileChangeStore = create<FileChangeState>()(
files: [],
initialized: false,
originalSectionOrders: {},
serverRevision: null,
},
},
}));
@@ -295,6 +308,36 @@ export const useFileChangeStore = create<FileChangeState>()(
const key = appKey(domainId, appId);
return get().appFiles[key]?.initialized ?? false;
},
setServerRevision: (domainId, appId, revision) => {
const key = appKey(domainId, appId);
set((state) => {
const existing = state.appFiles[key] ?? {
files: [],
initialized: false,
originalSectionOrders: {},
serverRevision: null,
};
return {
appFiles: {
...state.appFiles,
[key]: { ...existing, serverRevision: revision },
},
};
});
},
getServerRevision: (domainId, appId) => {
const key = appKey(domainId, appId);
return get().appFiles[key]?.serverRevision ?? null;
},
hasRemoteChange: (domainId, appId, currentRevision) => {
const key = appKey(domainId, appId);
const storedRevision = get().appFiles[key]?.serverRevision;
if (storedRevision === null) return false;
return storedRevision !== currentRevision;
},
}),
{
name: "file-change-storage",