4 lines
8.5 KiB
JavaScript
4 lines
8.5 KiB
JavaScript
|
import{getCookie,isLocalImage,resources,useEventListener}from"./chunk-7JONXWSO.js";import{k as k2}from"./chunk-RG4ZUIOR.js";import{loader_2_default,search_default,star_default,star_filled_default}from"./chunk-PE2ZHJ3V.js";import{IS_BROWSER,asset}from"./chunk-P55SGJLG.js";import{K,T,j,k,w}from"./chunk-ICN4RY3D.js";import{v}from"./chunk-CUT5GMLI.js";import{A}from"./chunk-YC2TUGGU.js";function useDebouncedCallback(func,wait,options){let lastCallTime=w(null),lastInvokeTime=w(0),timerId=w(null),lastArgs=w([]),lastThis=w(),result=w(),funcRef=w(func),mounted=w(!0);j(()=>{funcRef.current=func},[func]);let useRAF=!wait&&wait!==0&&typeof window<"u";if(typeof func!="function")throw new TypeError("Expected a function");wait=+wait||0,options=options||{};let leading=!!options.leading,trailing="trailing"in options?!!options.trailing:!0,maxing="maxWait"in options,maxWait=maxing?Math.max(+options.maxWait||0,wait):null;return j(()=>(mounted.current=!0,()=>{mounted.current=!1}),[]),T(()=>{let invokeFunc=time=>{let args=lastArgs.current,thisArg=lastThis.current;return lastArgs.current=lastThis.current=null,lastInvokeTime.current=time,result.current=funcRef.current.apply(thisArg,args)},startTimer=(pendingFunc,wait2)=>{useRAF&&cancelAnimationFrame(timerId.current),timerId.current=useRAF?requestAnimationFrame(pendingFunc):setTimeout(pendingFunc,wait2)},shouldInvoke=time=>{if(!mounted.current)return!1;let timeSinceLastCall=time-lastCallTime.current,timeSinceLastInvoke=time-lastInvokeTime.current;return!lastCallTime.current||timeSinceLastCall>=wait||timeSinceLastCall<0||maxing&&timeSinceLastInvoke>=maxWait},trailingEdge=time=>(timerId.current=null,trailing&&lastArgs.current?invokeFunc(time):(lastArgs.current=lastThis.current=null,result.current)),timerExpired=()=>{let time=Date.now();if(shouldInvoke(time))return trailingEdge(time);if(!mounted.current)return;let timeSinceLastCall=time-lastCallTime.current,timeSinceLastInvoke=time-lastInvokeTime.current,timeWaiting=wait-timeSinceLastCall,remainingWait=maxing?Math.min(timeWaiting,maxWait-timeSinceLastInvoke):timeWaiting;startTimer(timerExpired,remainingWait)},func2=(...args)=>{let time=Date.now(),isInvoking=shouldInvoke(time);if(lastArgs.current=args,lastThis.current=this,lastCallTime.current=time,isInvoking){if(!timerId.current&&mounted.current)return lastInvokeTime.current=lastCallTime.current,startTimer(timerExpired,wait),leading?invokeFunc(lastCallTime.current):result.current;if(maxing)return startTimer(timerExpired,wait),invokeFunc(lastCallTime.current)}return timerId.current||startTimer(timerExpired,wait),result.current};return func2.cancel=()=>{timerId.current&&(useRAF?cancelAnimationFrame(timerId.current):clearTimeout(timerId.current)),lastInvokeTime.current=0,lastArgs.current=lastCallTime.current=lastThis.current=timerId.current=null},func2.isPending=()=>!!timerId.current,func2.flush=()=>timerId.current?trailingEdge(Date.now()):result.current,func2},[leading,maxing,wait,maxWait,trailing,useRAF])}var Checkbox=({label,checked=k2(!1)})=>{let id=`checkbox-${K()}`;return v("label",{className:"flex items-center py-3 px-4 rounded-xl",style:{color:"var(--foreground)",background:"var(--background)"},children:[v("input",{type:"checkbox",checked:checked.value,name:"checkbox-one",id,onChange:ev=>{checked.value=ev.currentTarget.checked},class:`bg-gray-200 hover:bg-gray-300 cursor-pointer
|
||
|
w-5 h-5 border-3 border-amber-500 focus:outline-none rounded-lg`}),v("span",{class:"ml-3",children:label})]})},Checkbox_default=Checkbox;var Rating=props=>{let[rating,setRating]=k(props.rating),[hover,setHover]=k(0),max=k2(props.max||5);return v("div",{class:"flex gap-2 px-5 rounded-2xl bg-gray-200 z-10",style:{color:"var(--foreground)",background:"var(--background)"},children:Array.from({length:max.value}).map((_,i)=>v("span",{class:`my-5 cursor-pointer opacity-${i+1<=rating?100:i+1<=hover?20:100}`,onMouseOver:()=>setHover(i+1),onClick:()=>setRating(i+1),children:i+1<=rating||i+1<=hover?v(star_filled_default,{class:"w-4 h-4"}):v(star_default,{class:"w-4 h-4"})}))})};function generateResponsiveAttributes(imageUrl,widths2,baseApiUrl,sizes="100vw"){let srcsets=[];for(let width of widths2){let apiUrl=`${baseApiUrl}?image=${imageUrl}&width=${width}`;srcsets.push(`${apiUrl} ${width}w`)}return{srcset:srcsets.join(", "),sizes}}var widths=[320,640,960,1280],Image=props=>{let responsiveAttributes=isLocalImage(props.src)?generateResponsiveAttributes(props.src,widths,"/api/images"):{srcset:"",sizes:""};return v("span",{style:{position:props.fill?"absolute":"",width:props.fill?"100%":"",height:props.fill?"100%":"",zIndex:props.fill?-1:""},"data-thumb":props.thumbnail,children:v("img",{"data-thumb":props.thumbnail,"data-thumb-img":!0,loading:"lazy",alt:props.alt,style:props.style,srcset:responsiveAttributes.srcset,sizes:responsiveAttributes.sizes,src:asset(props.src),width:props.width,height:props.height,class:props.class})})},Image_default=Image;var Emoji=props=>props.name?props.name.endsWith(".png")?v("img",{class:`${props?.class||""}`,src:asset(`/emojis/${props.name}`)}):v("span",{children:props.name}):v(A,{});async function fetchQueryResource(url,type=""){let query=url.searchParams.get("q"),status=url.searchParams.get("status");try{return url.pathname="/api/resources",url.searchParams.set("q",encodeURIComponent(query||"*")),status&&url.searchParams.set("status","not-seen"),type&&url.searchParams.set("type",type),await(await fetch(url)).json()}catch(error){console.error("Error fetching data:",error)}}var RedirectSearchHandler=()=>(getCookie("session_cookie")&&useEventListener("keydown",e=>{e?.target?.nodeName!="INPUT"&&e.key==="?"&&window.location.search===""&&(window.location.href+="?q=*")},IS_BROWSER?document?.body:void 0),v(A,{})),SearchResultImage=({src})=>{let imageSrc=isLocalImage(src)?`/api/images?image=${src}&width=50&height=50`:src;return v(Image_default,{class:"object-cover w-12 h-12 rounded-full",src:imageSrc,alt:"preview image"})},SearchResultItem=({item,showEmoji=!1})=>{let doc=item.document,resourceType=resources[doc.type],href=resourceType?`${resourceType.link}/${doc.id}`:"";return v("a",{href,class:"p-2 text-white flex gap-4 items-center rounded-2xl hover:bg-gray-700",children:[showEmoji&&resourceType?v(Emoji,{class:"w-7 h-7",name:resourceType.emoji}):"",doc?.image&&v(SearchResultImage,{src:doc.image}),doc?.name]})},SearchResultList=({result,showEmoji})=>v("div",{class:"mt-4",children:result?.hits?v("div",{class:"flex flex-col gap-4",children:result.hits.map(hit=>v(SearchResultItem,{item:hit,showEmoji}))}):v("div",{style:{color:"#818181"},children:"No Results"})}),Search=({q="*",type,results})=>{let searchQuery=k2(q),data=k2(results),isLoading=k2(!1),showSeenStatus=k2(!1),inputRef=w(null);if("history"in globalThis){let u=new URL(window.location.href);u.searchParams.get("q")!==searchQuery.value&&u.searchParams.set("q",searchQuery.value),showSeenStatus.value?u.searchParams.set("rating","0"):u.searchParams.delete("rating"),window.history.replaceState({},"",u)}let debouncedFetchData=useDebouncedCallback(async()=>{try{isLoading.value=!0;let jsonData=await fetchQueryResource(new URL(window?.location.href),type);data.value=jsonData,isLoading.value=!1}catch(error){console.error("Error fetching data:",error),isLoading.value=!1}},500);j(()=>{inputRef.current&&searchQuery?.value.length===0&&inputRef.current?.focus()},[inputRef.current,searchQuery]);let handleInputChange=event=>{let target=event.target;target.value!==sear
|
||
|
//# sourceMappingURL=chunk-IJ6I6D4H.js.map
|