{"ast":null,"code":"import React from'react';import useRequest from'use-request';import api from'./api';import{jsx as _jsx,jsxs as _jsxs}from\"react/jsx-runtime\";const PatchExample=()=>{const{value:todos,patchValue,resetPatch,patched,execute}=useRequest(api.getTodos,[]);const inputRef=React.useRef(null);const addOptimistic=async()=>{var _inputRef$current;const text=(_inputRef$current=inputRef.current)===null||_inputRef$current===void 0?void 0:_inputRef$current.value;if(!text)return;inputRef.current.value='';patchValue([...(todos||[]),text]);try{const updated=await api.addTodo(text);patchValue(updated);}catch{resetPatch();}};const addAndFail=async()=>{var _inputRef$current2;const text=((_inputRef$current2=inputRef.current)===null||_inputRef$current2===void 0?void 0:_inputRef$current2.value)||'New todo';if(inputRef.current)inputRef.current.value='';patchValue([...(todos||[]),text]);try{await api.addTodoFail(text);}catch{setTimeout(resetPatch,600);}};return/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"input\",{className:\"demo-input\",ref:inputRef,placeholder:\"New todo...\"}),/*#__PURE__*/_jsxs(\"div\",{style:{display:'flex',gap:8,marginTop:8},children:[/*#__PURE__*/_jsx(\"button\",{className:\"demo-btn demo-btn-sm\",onClick:addOptimistic,children:\"Add\"}),/*#__PURE__*/_jsx(\"button\",{className:\"demo-btn demo-btn-sm demo-btn-danger\",onClick:addAndFail,children:\"Add (will fail)\"})]}),todos&&/*#__PURE__*/_jsx(\"ul\",{className:\"resource-list\",children:todos.map((t,i)=>/*#__PURE__*/_jsx(\"li\",{children:t},i))})]});};export default PatchExample;","map":{"version":3,"names":["React","useRequest","api","jsx","_jsx","jsxs","_jsxs","PatchExample","value","todos","patchValue","resetPatch","patched","execute","getTodos","inputRef","useRef","addOptimistic","_inputRef$current","text","current","updated","addTodo","addAndFail","_inputRef$current2","addTodoFail","setTimeout","children","className","ref","placeholder","style","display","gap","marginTop","onClick","map","t","i"],"sources":["/root/.openclaw/workspace/projects/termosa-use-request/example/src/PatchExample.js"],"sourcesContent":["import React from 'react'\nimport useRequest from 'use-request'\nimport api from './api'\n\nconst PatchExample = () => {\n  const { value: todos, patchValue, resetPatch, patched, execute } = useRequest(api.getTodos, [])\n  const inputRef = React.useRef(null)\n\n  const addOptimistic = async () => {\n    const text = inputRef.current?.value\n    if (!text) return\n    inputRef.current.value = ''\n\n    patchValue([...(todos || []), text])\n\n    try {\n      const updated = await api.addTodo(text)\n      patchValue(updated)\n    } catch {\n      resetPatch()\n    }\n  }\n\n  const addAndFail = async () => {\n    const text = inputRef.current?.value || 'New todo'\n    if (inputRef.current) inputRef.current.value = ''\n\n    patchValue([...(todos || []), text])\n\n    try {\n      await api.addTodoFail(text)\n    } catch {\n      setTimeout(resetPatch, 600)\n    }\n  }\n\n  return (\n    <div>\n      <input className=\"demo-input\" ref={inputRef} placeholder=\"New todo...\" />\n      <div style={{ display: 'flex', gap: 8, marginTop: 8 }}>\n        <button className=\"demo-btn demo-btn-sm\" onClick={addOptimistic}>Add</button>\n        <button className=\"demo-btn demo-btn-sm demo-btn-danger\" onClick={addAndFail}>\n          Add (will fail)\n        </button>\n      </div>\n      {todos && (\n        <ul className=\"resource-list\">\n          {todos.map((t, i) => <li key={i}>{t}</li>)}\n        </ul>\n      )}\n    </div>\n  )\n}\n\nexport default PatchExample\n"],"mappings":"AAAA,MAAO,CAAAA,KAAK,KAAM,OAAO,CACzB,MAAO,CAAAC,UAAU,KAAM,aAAa,CACpC,MAAO,CAAAC,GAAG,KAAM,OAAO,QAAAC,GAAA,IAAAC,IAAA,CAAAC,IAAA,IAAAC,KAAA,yBAEvB,KAAM,CAAAC,YAAY,CAAGA,CAAA,GAAM,CACzB,KAAM,CAAEC,KAAK,CAAEC,KAAK,CAAEC,UAAU,CAAEC,UAAU,CAAEC,OAAO,CAAEC,OAAQ,CAAC,CAAGZ,UAAU,CAACC,GAAG,CAACY,QAAQ,CAAE,EAAE,CAAC,CAC/F,KAAM,CAAAC,QAAQ,CAAGf,KAAK,CAACgB,MAAM,CAAC,IAAI,CAAC,CAEnC,KAAM,CAAAC,aAAa,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