{"record":{"id":"917e5def8b2cdc5e","repo":"siyuan-note/siyuan","slug":"view-state-name-must-not-be-empty","errorCode":null,"errorMessage":"View state ${name} must not be empty","messagePattern":"View state (.+?) must not be empty","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/util/viewState.ts","lineNumber":64,"sourceCode":"};\n\nconst defaultTransport: IViewStateTransport = {\n    async get(key) {\n        const {fetchSyncPost} = await import(\"./fetch\");\n        const response = await fetchSyncPost(\"/api/storage/getViewState\", {key});\n        checkResponse(response, \"get\");\n        return isViewStateData(response.data) ? response.data : {};\n    },\n    async patch(key, values, removeKeys) {\n        const {fetchSyncPost} = await import(\"./fetch\");\n        const response = await fetchSyncPost(\"/api/storage/patchViewState\", {key, values, removeKeys});\n        checkResponse(response, \"patch\");\n    },\n};\n\nconst validateIdentityPart = (name: string, value: string) => {\n    if (!value) {\n        throw new Error(`View state ${name} must not be empty`);\n    }\n};\n\nconst validateField = (field: string) => {\n    if (!field || field.length > 2048) {\n        throw new Error(\"Invalid view state field\");\n    }\n};\n\nexport const getViewStateKey = (identity: IViewStateIdentity) => {\n    validateIdentityPart(\"scope\", identity.scope);\n    validateIdentityPart(\"surface\", identity.surface);\n    validateIdentityPart(\"hostID\", identity.hostID);\n    const key = [identity.scope, identity.surface, identity.hostID].map(encodeURIComponent).join(\":\");\n    if (key.length > 1024) {\n        throw new Error(\"View state key is too long\");\n    }\n    return key;","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/util/viewState.ts#L46-L82","documentation":"validateIdentityPart requires every component of a view state identity (scope, surface, hostID) to be a non-empty string. When one is empty or falsy, the library throws naming the offending part. View state keys are composed of these three parts, so an empty part would create ambiguous keys.","triggerScenarios":"Constructing ViewStateService or calling getViewStateKey (via validateIdentityPart, app/src/util/viewState.ts:64) with identity such as {scope: \"\", surface: \"dock\", hostID: \"abc\"} — typically an uninitialized hostID because the hosting element or document ID was not resolved yet.","commonSituations":"Creating a view-state service before the document/editor ID is assigned; passing an empty surface string for a plugin panel; hostID derived from an attribute that is missing on first render.","solutions":["Ensure hostID (e.g. document/panel ID) is resolved before constructing ViewStateService","Provide non-empty scope and surface constants for the component","Defer service construction until identity is known, or pass a stable placeholder that is non-empty","Guard construction: if (!identity.hostID) skip persisting view state"],"exampleFix":"// before\nnew ViewStateService({scope: \"protyle\", surface: \"\", hostID: id});\n// after\nnew ViewStateService({scope: \"protyle\", surface: \"editor\", hostID: id});","handlingStrategy":"validation","validationCode":"if (!identity.scope || !identity.surface || !identity.hostID) return; // skip persistence","typeGuard":"const hasIdentity = (i) => typeof i.scope === \"string\" && i.scope.length > 0 && typeof i.surface === \"string\" && i.surface.length > 0 && typeof i.hostID === \"string\" && i.hostID.length > 0;","tryCatchPattern":"try { const svc = new ViewStateService(identity); } catch (e) { if (e.message.includes(\"must not be empty\")) svc = null; }","preventionTips":["Resolve document/panel IDs before constructing the service","Use fixed non-empty scope/surface constants per component","Defer persistence until identity is fully known"],"tags":["validation","view-state","empty-value"],"backgroundTag":"empty-required-field","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}