1import{r as m,j as r}from"./react-vendor-CHQL15iE.js";import{l as T,u as G,T as W,a1 as v}from"./main-Dck1T9BT.js";import{am as ee,d as te,E as re,aT as se,J as ne,A as ie}from"./lucide-BFLOpGdB.js";import"./pdfjs-ji3DqnKJ.js";import"./markdown-W-rE_H7s.js";import"./katex-math-BxKHCrdI.js";const oe=20,M=10,_=12,J=(e,n)=>{const o=Number.isFinite(e)?Math.trunc(e):n;return Math.min(Math.max(o||n,1),oe)},F=()=>{try{return typeof localStorage>"u"?"":String(localStorage.getItem("p2g_token")||"")}catch{return""}},O=()=>{const e=F();return e?{Authorization:`Bearer ${e}`}:{}},p=e=>String(e??"").trim(),D=e=>Array.isArray(e)?e.map(p).filter(Boolean):[],ae=e=>p(e).replace(/^https?:\/\/(?:export\.)?arxiv\.org\/(?:abs|pdf)\//i,"").replace(/\.pdf$/i,""),ce=(e,n)=>{var d;const o=n||(e==null?void 0:e.source)||"arxiv",i=o==="arxiv"?ae(e==null?void 0:e.id):p(e==null?void 0:e.id),a=p(e==null?void 0:e.absUrl)||(o==="arxiv"&&i?`https://arxiv.org/abs/${i}`:""),s=p(e==null?void 0:e.pdfUrl)||(o==="arxiv"&&i?`https://export.arxiv.org/pdf/${i}`:"");return{source:o,id:i,title:p(e==null?void 0:e.title),summary:p((e==null?void 0:e.summary)||(e==null?void 0:e.abstract)),published:p(e==null?void 0:e.published),updated:p(e==null?void 0:e.updated),authors:D(e==null?void 0:e.authors),categories:D(e==null?void 0:e.categories),primaryCategory:p((e==null?void 0:e.primaryCategory)||((d=e==null?void 0:e.categories)==null?void 0:d[0])),pdfUrl:s,absUrl:a,...e!=null&&e.doi?{doi:p(e.doi)}:{},...e!=null&&e.pmcid?{pmcid:p(e.pmcid)}:{},...e!=null&&e.openAccessStatus?{openAccessStatus:p(e.openAccessStatus)}:{},...typeof(e==null?void 0:e.hasOpenAccessFullText)=="boolean"?{hasOpenAccessFullText:e.hasOpenAccessFullText}:{},...e!=null&&e.recommendation?{recommendation:p(e.recommendation)}:{},...e!=null&&e.recommendationKind?{recommendationKind:e.recommendationKind}:{},...e!=null&&e.matchedPreference?{matchedPreference:p(e.matchedPreference)}:{},...Number.isFinite(Number(e==null?void 0:e.citedByCount))?{citedByCount:Number(e.citedByCount)}:{},...Number.isFinite(Number(e==null?void 0:e.fwci))?{fwci:Number(e.fwci)}:{}}},X=(e,n)=>{const o=e==null?void 0:e.entries;return(Array.isArray(o)?o:[]).map(i=>ce(i,n))},de=e=>{if(typeof e!="string"||!e.trim()||e.includes("<"))return"";const n=e.replace(/[\u0000-\u001f\u007f]/g," ").replace(/\s+/g," ").trim();return n.length<=200?n:""},H=async(e,n)=>{let o="";const i=await e.text().catch(()=>"");if(i.trim())try{const a=JSON.parse(i);o=de(a==null?void 0:a.error)}catch{}throw new Error(o||n)},Q=async(e,n)=>{if(!e.ok)return H(e,n);try{return await e.json()}catch{throw new Error(n)}},le=(e,n,o)=>{const i=e==="arxiv"?[]:[`source=${encodeURIComponent(e)}`];return i.push(`q=${encodeURIComponent(n)}`,`max=${o}`),`${e==="arxiv"?"/api/arxiv/search":"/api/papers/search"}?${i.join("&")}`},pe=async(e,n=M)=>{const o=await T("/api/papers/recommend",{method:"POST",headers:{"Content-Type":"application/json",...O()},body:JSON.stringify({interests:Array.isArray(e)?e:[],max:J(n,M)})}),i=await Q(o,"æ æ³å è½½è®ºææ¨è");return X(i,"openalex")},me=async(e,n,o=_)=>{const i=String(e||"").toLowerCase(),a=p(n);if(!a)return[];if(!F())throw new Error(i==="arxiv"?"请å ç»å½ååä½¿ç¨ arXiv æç´¢":"请å ç»å½ååæç´¢è®ºæ");const s=await T(le(i,a,J(o,_)),{headers:O()}),d=await Q(s,"æ æ³æç´¢è®ºæ");return X(d,i)},xe=(e,n)=>{var s,d,x;const o=String(e||""),i=(s=o.match(/filename\*=(?:UTF-8'')?([^;]+)/i))==null?void 0:s[1],a=((d=o.match(/filename="([^"]+)"/i))==null?void 0:d[1])||((x=o.match(/filename=([^;]+)/i))==null?void 0:x[1]);try{return decodeURIComponent((i||a||"").trim()).replace(/[\\/\0\r\n"]+/g,"_").slice(0,120)||n}catch{return n}},fe=async e=>{var x,w,f,u;const n=String((e==null?void 0:e.source)||"").toLowerCase(),o=p(e==null?void 0:e.id);if(!F())throw new Error("请å ç»å½ååå¯¼å ¥è®ºæ");const i=await T("/api/papers/import",{method:"POST",headers:{"Content-Type":"application/json",...O()},body:JSON.stringify({source:n,id:o})});if(!i.ok)return H(i,"æ æ³å¯¼å ¥è®ºæ");const a=String(((w=(x=i.headers)==null?void 0:x.get)==null?void 0:w.call(x,"content-type"))||"").toLowerCase(),s=xe((u=(f=i.headers)==null?void 0:f.get)==null?void 0:u.call(f,"content-disposition"),`${n}-paper.${a.includes("pdf")?"pdf":"txt"}`);if(a.split(";")[0].trim()==="text/plain"){const S=await i.text();if(!S.trim())throw new Error("æ æ³å¯¼å ¥è®ºæ");return{text:S,fileName:s}}if(a.split(";")[0].trim()!=="application/pdf"||typeof File>"u")throw new Error("æ æ³å¯¼å ¥è®ºæ");const d=await i.blob();if(!d.size||d.size>30*1024*1024)throw new Error("æ æ³å¯¼å ¥è®ºæ");return{text:"",fileName:s,file:new File([d],s,{type:"application/pdf"})}},he=[],B=[{id:"openalex",label:"OpenAlex",description:"All fields"},{id:"europepmc",label:"Europe PMC",description:"Medicine & life sciences"},{id:"arxiv",label:"arXiv",description:"Preprints"}],be=e=>{const n=String(e||"").trim();return/^https?:\/\/[^\s]+$/i.test(n)?n:null},K=e=>`${e.source}:${e.id}`,ye=e=>[`Title: ${e.title}`,`Authors: ${(e.authors||[]).join(", ")||"Unknown"}`,`Published: ${e.published||"Unknown"}`,`Categories: ${(e.categories||[]).join(", ")||e.primaryCategory||"Unknown"}`,"","Abstract:",e.summary||""].join(` 2`),q=(e,n="txt")=>{var i;const o=((i=String(e.id||"paper").split("/").pop())==null?void 0:i.replace(/[^a-zA-Z0-9._-]/g,"-"))||"paper";return`${e.source}-${o}.${n}`},ue=e=>{const n=W[e];return m.useCallback((o,i,a)=>{let s=n[o]||i;return Object.entries(a||{}
2).forEach(([d,x])=>{s=s.replace(`{{${d}}}`,x)}),s},[n])},Ae=({onBack:e,onUsePaper:n,language:o,interests:i=he})=>{const a=G(),s=ue(o),[d,x]=m.useState("openalex"),[w,f]=m.useState([]),[u,S]=m.useState(""),[g,N]=m.useState(!1),[k,j]=m.useState(null),[P,C]=m.useState(!1),[A,U]=m.useState(null),b=m.useRef(0),h=m.useRef(!0);m.useEffect(()=>(h.current=!0,()=>{h.current=!1,b.current+=1}),[]);const E=m.useCallback(async()=>{const t=++b.current;x("openalex"),C(!1),N(!0),j(null),f([]);try{const c=await pe(i,10);if(!h.current||t!==b.current)return;f(Array.isArray(c)?c:[])}catch(c){if(!h.current||t!==b.current)return;j(c instanceof Error?c.message:s("paperLoadFailed","Unable to load papers.")),f([])}finally{h.current&&t===b.current&&N(!1)}},[i,s]);m.useEffect(()=>{E()},[E]);const $=m.useCallback(async(t=d)=>{const c=u.trim();if(!c)return;const l=++b.current;C(!1),N(!0),j(null),f([]);try{const y=await me(t,c,12);if(!h.current||l!==b.current)return;f(Array.isArray(y)?y:[])}catch(y){if(!h.current||l!==b.current)return;j(y instanceof Error?y.message:s("paperSearchFailed","Unable to search papers.")),f([])}finally{h.current&&l===b.current&&N(!1)}},[u,d,s]),V=t=>{t!==d&&(b.current+=1,x(t),u.trim()?$(t):t==="openalex"?E():(N(!1),j(null),f([]),C(!0)))},z=t=>{n({text:ye(t),fileName:q(t)})},Y=async t=>{const c=K(t);if(!A){U(c);try{const l=await fe({source:t.source,id:t.id});if(!h.current)return;if(l.file)n({text:"",fileName:l.fileName||l.file.name,file:l.file});else if(l.text.trim())n({text:l.text,fileName:l.fileName||q(t)});else throw new Error(s("paperFullEmpty","The imported full text is empty."))}catch(l){if(!h.current)return;const y=l instanceof Error?l.message:s("paperImportFailed","Full-text import failed.");await v(s("paperImportFallback",`${y} The abstract will be used instead.`,{error:y}),{variant:"warning"}),h.current&&z(t)}finally{h.current&&U(null)}}};return r.jsxs("div",{className:`fixed inset-0 z-50 flex flex-col bg-[#fff0f5] ${a?"overflow-y-auto overscroll-y-contain":"overflow-hidden"} animate-fade-in`,children:[r.jsxs("header",{className:"relative z-20 flex items-center gap-3 px-4 py-4 md:gap-6 md:px-12 md:py-8",children:[r.jsx("button",{type:"button",onClick:e,"aria-label":s("back","Back"),className:"shrink-0 rounded-xl border border-white bg-white/80 p-2 text-slate-400 shadow-sm transition hover:text-pink-500 md:p-3",children:r.jsx(ee,{size:24})}),r.jsxs("div",{className:"min-w-0",children:[r.jsx("h1",{className:"truncate text-2xl font-black tracking-tight text-slate-800 md:text-4xl",children:s("paperDiscoveryTitle","Discover papers")}),r.jsx("p",{className:"mt-1 text-[9px] font-bold uppercase tracking-[0.2em] text-slate-400",children:s("paperDiscoverySubtitle","Open access paper discovery")})]})]}),r.jsxs("main",{className:`relative z-10 px-4 pb-8 md:px-12 md:pb-12 ${a?"":"min-h-0 flex-1 overflow-y-auto"} custom-scrollbar`,children:[r.jsxs("section",{"data-p2g-context":"arxiv-search",className:"mb-4 rounded-2xl border border-white bg-white/80 p-3 shadow-sm md:p-4",children:[r.jsx("div",{className:"mb-3 grid grid-cols-1 gap-2 sm:grid-cols-3",children:B.map(t=>{const c=d===t.id;return r.jsxs("button",{type:"button","data-testid":`paper-source-${t.id}`,"aria-pressed":c,onClick:()=>V(t.id),className:`rounded-2xl border-2 px-3 py-3 text-left transition ${c?"border-pink-500 bg-gradient-to-br from-rose-400 to-pink-600 text-white shadow-md":"border-pink-100 bg-white text-slate-600 hover:border-pink-300"}`,children:[r.jsx("span",{className:"block text-sm font-black",children:t.label}),r.jsx("span",{className:`mt-1 block text-[10px] font-bold ${c?"text-white/80":"text-slate-400"}`,children:s(`paperSource_${t.id}`,t.description)})]},t.id)})}),r.jsxs("div",{className:"flex flex-col gap-2 md:flex-row",children:[r.jsx("button",{type:"button",onClick:()=>void E(),disabled:g,className:"rounded-xl bg-pink-500 px-4 py-2 text-sm font-black text-white transition hover:bg-pink-600 disabled:opacity-60",children:s("arxivDaily","Daily picks")}),r.jsxs("div",{className:"flex min-w-0 flex-1 gap-2",children:[r.jsx("input",{value:u,onChange:t=>S(t.target.value),onKeyDown:t=>{t.key==="Enter"&&$()},placeholder:s("arxivSearchPlaceholder","Search papers by keyword"),"aria-label":s("arxivSearchPlaceholder","Search papers by keyword"),
2className:"min-w-0 flex-1 rounded-xl border-2 border-slate-200 bg-white px-3 py-2 text-sm outline-none focus:border-pink-300"}),r.jsxs("button",{type:"button",onClick:()=>void $(),disabled:g,className:"flex items-center gap-1.5 rounded-xl border-2 border-slate-200 bg-white px-3 py-2 text-sm font-black text-slate-700 transition hover:border-pink-300 disabled:opacity-60",children:[r.jsx(te,{size:16})," ",s("arxivSearch","Search")]})]})]})]}),g&&r.jsxs("div",{"data-testid":"paper-discovery-skeleton","aria-busy":"true","aria-live":"polite",className:"space-y-3",children:[r.jsx("span",{className:"sr-only",children:s("loadingCommunity","Loading papers...")}),Array.from({length:a?3:4},(t,c)=>r.jsxs("div",{className:"animate-pulse rounded-2xl border-2 border-white bg-white/80 p-4",children:[r.jsx("div",{className:"h-5 w-3/5 rounded-full bg-slate-200"}),r.jsx("div",{className:"mt-3 h-3 w-2/5 rounded-full bg-slate-100"}),r.jsx("div",{className:"mt-3 h-3 w-full rounded-full bg-slate-100"}),r.jsx("div",{className:"mt-2 h-3 w-4/5 rounded-full bg-slate-100"})]},c))]}),!g&&k&&r.jsx("div",{"data-testid":"paper-discovery-error",className:"mb-4 rounded-xl border border-red-100 bg-red-50 p-3 text-sm font-bold text-red-500",children:k}),!g&&!k&&P&&r.jsx("div",{"data-testid":"paper-discovery-search-hint",className:"rounded-2xl border border-slate-100 bg-slate-50 p-4 text-sm font-bold text-slate-500",children:s("paperSearchHint","Enter keywords to search this source.")}),!g&&!k&&!P&&w.length===0&&r.jsx("div",{"data-testid":"paper-discovery-empty",className:"rounded-2xl border border-slate-100 bg-slate-50 p-4 text-sm font-bold text-slate-500",children:s("paperEmpty","No papers found.")}),r.jsx("div",{"data-p2g-context":"arxiv-results",className:"space-y-3",children:!g&&!k&&w.map(t=>{var I,R,L;const c=K(t),l=be(t.absUrl),y=A===c;return r.jsxs("article",{"data-testid":"paper-card","data-p2g-context":"arxiv-paper",className:"rounded-2xl border-2 border-slate-100 bg-white p-3 shadow-sm md:p-4",children:[r.jsxs("div",{className:"flex flex-col gap-3 md:flex-row md:items-start md:justify-between",children:[r.jsxs("div",{className:"min-w-0",children:[r.jsx("h2",{className:"text-sm font-black text-slate-800 md:text-base",children:t.title}),r.jsxs("p",{className:"mt-1 text-[11px] text-slate-500 md:text-xs",children:[(t.authors||[]).slice(0,5).join(", "),(t.authors||[]).length>5?" ...":""]}),r.jsxs("p",{className:"mt-1 text-[10px] font-bold text-pink-500 md:text-xs",children:[t.primaryCategory||((I=t.categories)==null?void 0:I[0])||"Paper"," · ",(t.published||"").slice(0,10)]}),r.jsxs("div",{className:"mt-2 flex flex-wrap gap-1.5",children:[t.recommendationKind&&r.jsx("span",{"data-testid":"paper-recommendation-tag",className:"rounded-full bg-pink-50 px-2 py-0.5 text-[10px] font-black text-pink-600",children:t.recommendationKind==="preference"?`${s("paperRecommended","Recommended")}${t.matchedPreference?` · ${t.matchedPreference}`:""}`:s("paperTrending","Trending")}),r.jsx("span",{className:"rounded-full bg-slate-100 px-2 py-0.5 text-[10px] font-black text-slate-500",children:((R=B.find(Z=>Z.id===t.source))==null?void 0:R.label)||t.source}),r.jsx("span",{className:`rounded-full px-2 py-0.5 text-[10px] font-black ${t.hasOpenAccessFullText||t.source==="arxiv"?"bg-emerald-50 text-emerald-600":"bg-amber-50 text-amber-600"}`,children:t.hasOpenAccessFullText||t.source==="arxiv"?s("paperFullAvailable","Open full text"):s("paperFullUnknown","Full text unconfirmed")})]})]}),r.jsxs("div",{className:"flex shrink-0 flex-wrap gap-2","data-p2g-context":"arxiv-actions",children:[l?r.jsxs("a",{"data-testid":"paper-detail-link",href:l,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 rounded-lg border-2 border-slate-200 px-3 py-1.5 text-xs font-black text-slate-600 hover:border-pink-300",children:[r.jsx(re,{size:12})," ",s("arxivOpenAbs","Open details")]}):r.jsx("span",{"data-testid":"paper-detail-link-invalid",className:"rounded-lg border-2 border-slate-100 px-3 py-1.5 text-xs font-black text-slate-300",children:s("arxivOpenAbs","Open details")}),r.jsxs("button",{type:"button","data-testid":"paper-use-abstract",onClick:()=>z(t),disabled:!!A,className:"flex items-center gap-1 rounded-lg border-2 border-slate-200 px-3 py-1.5 text-xs font-black text-slate-600 hover:border-pink-300 disabled:opacity-50",children:[r.jsx(se,{size:12})," ",s("paperUseAbstract","Use abstract")]}),r.jsxs("button",{type:"button","data-testid":"paper-import-full",onClick:()=>void Y(t),disabled:!!A,className:"flex items-center gap-1 rounded-lg bg-gradient-to-r from-rose-400 to-pink-600 px-3 py-1.5 text-xs font-black text-white disabled:opacity-50",children:[y?r.jsx(ne,{size:12,className:"animate-spin"}):r.jsx(ie,{size:12})," ",s("paperImportFull","Import full text")]})]})]}),r.jsx("p",{className:"mt-2 text-xs leading-relaxed text-slate-600 md:text-sm",children:((L=t.summary)==null?void 0:L.length)>480?`${t.summary.slice(0,480)}...`:t.summary})]},c)})})]})]})};export{Ae as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.