UNPKG

mira-app-server

Version:

Mira Server - standalone server application using mira-app-core

2 lines (1 loc) 1.7 kB
import{aK as p,c8 as c,al as w,bb as A,z as l}from"./index-OOzZz4T6.js";const m=100,u="view-history",T=p("viewHistory",()=>{const r=c({}),i=c(!1),s=c(null),g=w(()=>Object.values(r.value).flat().sort((e,t)=>new Date(t.viewedAt).getTime()-new Date(e.viewedAt).getTime())),v=e=>(r.value[e]||[]).slice().sort((t,o)=>new Date(o.viewedAt).getTime()-new Date(t.viewedAt).getTime()),h=w(()=>e=>v(e)),y=async(e,t)=>{if(!e?.id||!t)return;const o={fileId:String(e.id),libraryId:t,name:e.name||e.title||A.global.t("stores.viewHistory.unknownFile"),mimeType:e.mimeType||"",thumbnailPath:e.thumbnailPath,url:e.url,path:e.path,size:e.size,viewedAt:new Date().toISOString()},a=(r.value[t]?[...r.value[t]]:[]).filter(f=>f.fileId!==o.fileId);a.unshift(o),a.length>m&&(a.length=m),r.value={...r.value,[t]:a},await d(t)},S=async e=>{if(!r.value[e])return;const t={...r.value};delete t[e],r.value=t,await l.removeItem(u,e)},d=async e=>{try{const t={records:r.value[e]||[],lastUpdated:new Date().toISOString()};await l.setItem(u,JSON.stringify(t),e)}catch(t){console.error("Failed to persist view history:",t),s.value="Failed to save view history"}};return{recordsByLibrary:r,isLoading:i,error:s,allRecords:g,recordsOf:h,getLibraryRecords:v,addViewRecord:y,clearLibraryRecords:S,persistToStorage:d,restoreFromStorage:async e=>{try{i.value=!0;const t=await l.getItem(u,e);if(!t)return;const o=JSON.parse(t),n=Array.isArray(o?.records)?o.records:[];if(n.length===0)return;const a=n[0]?.libraryId||e;if(!a)return;r.value={...r.value,[a]:n},s.value=null}catch(t){console.error("Failed to restore view history:",t),s.value="Failed to load view history"}finally{i.value=!1}},clearError:()=>{s.value=null}}});export{T as u};