inogen/app/admin/pages/IdeasPage.tsx
2026-10-18 18:12:59 +03:30

38 lines
4.6 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Lightbulb, Pencil, Plus } from "lucide-react";
import { useEffect, useState } from "react";
import { Link } from "react-router";
import { Card, EmptyState, ErrorBanner, PageTitle, Select } from "../components/Ui";
import { api } from "../lib/api";
import type { IdeaItem, PagedResult } from "../types";
import { RecordAvatar, useFirstImages } from "../components/RecordImage";
import { ListToolbar, PaginationControls, useDebouncedValue } from "../components/ListControls";
import { ArchiveButton } from "../components/ArchiveButton";
import { BulkArchiveActions, SelectionCheckbox } from "../components/BulkArchive";
export function IdeasPage() {
const [data, setData] = useState<PagedResult<IdeaItem> | null>(null);
const [error, setError] = useState("");
const [page,setPage]=useState(1); const [pageSize,setPageSize]=useState(25); const [search,setSearch]=useState("");
const [status,setStatus]=useState(""); const [loading,setLoading]=useState(true); const debouncedSearch=useDebouncedValue(search);
const [selected,setSelected]=useState<Set<string>>(new Set());
useEffect(() => {let active=true;setLoading(true);setError("");const query=new URLSearchParams({page:String(page),pageSize:String(pageSize)});if(debouncedSearch)query.set("search",debouncedSearch);if(status)query.set("status",status);
void api<PagedResult<IdeaItem>>(`/api/v1/ideas?${query}`).then(x=>{if(active)setData(x);}).catch(e=>{if(active)setError(e.message);}).finally(()=>{if(active)setLoading(false);});return()=>{active=false;}; }, [page,pageSize,debouncedSearch,status]);
const images = useFirstImages("Idea", data?.items.map(x => x.id));
const remove=(id:string)=>setData(old=>old?{...old,items:old.items.filter(x=>x.id!==id),totalCount:old.totalCount-1}:old);
return <><PageTitle title="ایده‌ها و دستاوردها" description="ایده، مقاله و اختراع با رابطه شناسه‌ای مدیریت می‌شوند"
action={<div className="row-actions"><Link className="text-link" to="/admin/achievements">مقاله‌ها و اختراع‌ها</Link><Link className="button link-button" to="/admin/ideas/new"><Plus size={18}/>ایده جدید</Link></div>}/>
{error && <ErrorBanner message={error}/>}<Card>
<ListToolbar search={search} onSearch={value=>{setSearch(value);setPage(1);}} total={data?.totalCount??0} placeholder="عنوان، ایده‌پرداز یا وضعیت">
<label className="list-filter">وضعیت انتشار<Select value={status} onChange={e=>{setStatus(e.target.value);setPage(1);}}><option value="">همه</option><option value="0">پیش‌نویس</option><option value="1">منتشرشده</option><option value="2">بایگانی</option></Select></label>
</ListToolbar>
{data?.items.length?<BulkArchiveActions entityType="Idea" records={data.items} selected={selected} onSelectionChange={setSelected} onArchived={ids=>setData(old=>old?{...old,items:old.items.filter(x=>!ids.includes(x.id)),totalCount:old.totalCount-ids.length}:old)}/>:null}
{!data ? <div className="loading-line">در حال دریافت اطلاعات…</div> : data.items.length === 0
? <EmptyState title="هنوز ایده‌ای ثبت نشده" description="ایده را همراه با مقاله‌ها و اختراع‌های وابسته ثبت کنید."/>
: <><div className={`table-wrap${loading?" is-loading":""}`}><table><thead><tr><th>انتخاب</th><th>تصویر</th><th>عنوان ایده</th><th>ایده‌پرداز</th><th>تاریخ ثبت</th><th>وضعیت ایده</th><th>مقاله</th><th>اختراع</th><th>افزایش درآمد</th><th>عملیات</th></tr></thead>
<tbody>{data.items.map(x => <tr key={x.id}><td><SelectionCheckbox checked={selected.has(x.id)} onChange={checked=>setSelected(old=>{const next=new Set(old);checked?next.add(x.id):next.delete(x.id);return next;})} label={`انتخاب ${x.title}`}/></td><td><RecordAvatar attachmentId={images[x.id]} label={x.innovatorFullName}/></td><td><Lightbulb size={15}/> {x.title}</td><td>{x.innovatorFullName}</td><td className="latin">{x.registrationDate}</td><td>{x.ideaStatus}</td><td>{x.articleCount}</td><td>{x.patentCount}</td><td>{x.increasedRevenue ?? "—"}</td><td><div className="row-actions"><Link className="row-action" to={`/admin/ideas/${x.id}/edit`}><Pencil size={15}/>ویرایش</Link><ArchiveButton entityType="Idea" id={x.id} rowVersion={x.rowVersion} onArchived={()=>remove(x.id)}/></div></td></tr>)}</tbody></table></div>
<PaginationControls page={page} pageSize={pageSize} total={data.totalCount} loading={loading} onPage={setPage} onPageSize={value=>{setPageSize(value);setPage(1);}}/></>}
</Card></>;
}