MCPcopy
hub / github.com/TanShilongMario/PromptFill / useStickyState

Function useStickyState

src/hooks/useStickyState.js:4–29  ·  view source on GitHub ↗
(defaultValue, key)

Source from the content-addressed store, hash-verified

2import { useState, useEffect } from 'react';
3
4export const useStickyState = (defaultValue, key) => {
5 const [value, setValue] = useState(() => {
6 try {
7 const stickyValue = window.localStorage.getItem(key);
8 return stickyValue !== null ? JSON.parse(stickyValue) : defaultValue;
9 } catch (error) {
10 console.error(`读取 localStorage 失败 (${key}):`, error);
11 return defaultValue;
12 }
13 });
14
15 useEffect(() => {
16 try {
17 const storageMode = window.localStorage.getItem('app_storage_mode') || 'browser';
18 // 在使用本地文件夹模式时,不再写入 localStorage,避免大图触发配额弹窗
19 if (storageMode === 'folder') return;
20
21 const serialized = JSON.stringify(value);
22 window.localStorage.setItem(key, serialized);
23 } catch (error) {
24 console.error(`保存到 localStorage 失败 (${key}):`, error);
25 }
26 }, [key, value]);
27
28 return [value, setValue];
29};

Callers 4

AppFunction · 0.90
PrivacyPageFunction · 0.90
RootProviderFunction · 0.90
VideoLayoutFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected