fixing table list loading link params

This commit is contained in:
rizky 2024-08-15 11:11:15 -07:00
parent 732aca1abf
commit 59c8ddc716
3 changed files with 297 additions and 215 deletions

View File

@ -36,6 +36,7 @@ import { MDLocal } from "../md/utils/typings";
import { Skeleton } from "../ui/skeleton"; import { Skeleton } from "../ui/skeleton";
import { toast } from "../ui/toast"; import { toast } from "../ui/toast";
import { sortTree } from "./utils/sort-tree"; import { sortTree } from "./utils/sort-tree";
import { getPathname } from "lib/exports";
type OnRowClick = (arg: { type OnRowClick = (arg: {
row: any; row: any;
@ -129,7 +130,9 @@ export const TableList: FC<TableListProp> = ({
if (ls_sort) { if (ls_sort) {
ls_sort = JSON.parse(ls_sort as any); ls_sort = JSON.parse(ls_sort as any);
} }
const local = useLocal({ const local = useLocal(
{
times: 0,
selectedRows: [] as { selectedRows: [] as {
pk: string | number; pk: string | number;
rows: any; rows: any;
@ -175,8 +178,11 @@ export const TableList: FC<TableListProp> = ({
collapsed: new Set<number>(), collapsed: new Set<number>(),
cached_row: new WeakMap<any, ReactElement>(), cached_row: new WeakMap<any, ReactElement>(),
filtering: "" as ReactNode | string | true, filtering: "" as ReactNode | string | true,
reloading: null as any,
reload: (arg?: { toast: boolean }) => { reload: (arg?: { toast: boolean }) => {
return new Promise<void>((done) => { if (local.reloading) return local.reloading;
local.reloading = new Promise<void>(async (done) => {
let should_toast = true; let should_toast = true;
if (arg?.toast === false) should_toast = false; if (arg?.toast === false) should_toast = false;
local.should_toast = should_toast; local.should_toast = should_toast;
@ -210,6 +216,16 @@ export const TableList: FC<TableListProp> = ({
} }
if (md) { if (md) {
if (md.header.loading) {
await new Promise<void>((resolve) => {
const ival = setInterval(() => {
if (!md.header.loading) {
clearInterval(ival);
resolve();
}
}, 10);
});
}
const last = md.params.links[md.params.links.length - 1]; const last = md.params.links[md.params.links.length - 1];
if (last && last.where) { if (last && last.where) {
@ -221,7 +237,10 @@ export const TableList: FC<TableListProp> = ({
} }
} }
call_prasi_events("tablelist", "where", [__props?.gen__table, where]); call_prasi_events("tablelist", "where", [
__props?.gen__table,
where,
]);
const load_args: any = { const load_args: any = {
async reload() {}, async reload() {},
@ -244,10 +263,12 @@ export const TableList: FC<TableListProp> = ({
} }
local.status = "ready"; local.status = "ready";
local.reloading = null;
local.render(); local.render();
done(); done();
if (local.grid_ref && !id_parent) local.paging.scroll(local.grid_ref); if (local.grid_ref && !id_parent)
local.paging.scroll(local.grid_ref);
}; };
if (result instanceof Promise) { if (result instanceof Promise) {
@ -276,6 +297,8 @@ export const TableList: FC<TableListProp> = ({
} else callback(result); } else callback(result);
} }
}); });
return local.reloading;
}, },
sort: { sort: {
columns: (ls_sort?.columns || []) as SortColumn[], columns: (ls_sort?.columns || []) as SortColumn[],
@ -354,10 +377,15 @@ export const TableList: FC<TableListProp> = ({
soft_delete: { soft_delete: {
field: null as any, field: null as any,
}, },
}); },
({ setDelayedRender }) => {
setDelayedRender(true);
}
);
const reload = local.reload; const reload = local.reload;
if (md) { if (md) {
md.master.list = local;
md.master.reload = reload; md.master.reload = reload;
} }
@ -712,9 +740,12 @@ export const TableList: FC<TableListProp> = ({
if (!isEditor) { if (!isEditor) {
let should_toast = true; let should_toast = true;
if (md && md.props.mode !== "full") { if (md) {
if (md.props.mode !== "full") {
should_toast = false; should_toast = false;
} }
}
if (should_toast) { if (should_toast) {
if (local.status === "loading") { if (local.status === "loading") {
toast.dismiss(); toast.dismiss();
@ -730,12 +761,6 @@ export const TableList: FC<TableListProp> = ({
} }
} }
if (local.status === "resizing" && !isEditor) {
local.status = "ready";
local.render();
return null;
}
if (document.getElementsByClassName("prasi-toaster").length === 0) { if (document.getElementsByClassName("prasi-toaster").length === 0) {
const elemDiv = document.createElement("div"); const elemDiv = document.createElement("div");
elemDiv.className = "prasi-toaster"; elemDiv.className = "prasi-toaster";
@ -744,6 +769,11 @@ export const TableList: FC<TableListProp> = ({
const toaster_el = document.getElementsByClassName("prasi-toaster")[0]; const toaster_el = document.getElementsByClassName("prasi-toaster")[0];
if (mode === "table") { if (mode === "table") {
if (local.status === "resizing" && !isEditor) {
local.status = "ready";
local.render();
}
return ( return (
<div <div
className={cx( className={cx(
@ -929,7 +959,26 @@ export const TableList: FC<TableListProp> = ({
<Skeleton className={cx("c-w-[180px] c-h-[11px]")} /> <Skeleton className={cx("c-w-[180px] c-h-[11px]")} />
</div> </div>
) : ( ) : (
<div className="c-absolute c-inset-0"> <div
className={cx(
"c-absolute c-inset-0",
css`
@keyframes flasher {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0px);
}
}
.list-row {
animation: flasher 0.5s;
}
`
)}
>
<> <>
{Array.isArray(data) && data.length > 0 ? ( {Array.isArray(data) && data.length > 0 ? (
<div <div
@ -942,7 +991,7 @@ export const TableList: FC<TableListProp> = ({
{data.map((e, idx) => { {data.map((e, idx) => {
return ( return (
<div <div
className="c-flex-grow c-flex" className={cx("list-row c-flex-grow c-flex")}
onClick={(ev) => { onClick={(ev) => {
if (!isEditor && typeof row_click === "function") { if (!isEditor && typeof row_click === "function") {
row_click({ row_click({
@ -1047,9 +1096,14 @@ const genRows = (total: number) => {
}; };
const dataGridStyle = (local: { height: number }) => css` const dataGridStyle = (local: { height: number }) => css`
${local.height
? css`
.rdg { .rdg {
block-size: ${local.height}px; block-size: ${local.height}px;
} }
`
: ``}
div[role="row"]:hover { div[role="row"]:hover {
background: #e2f1ff; background: #e2f1ff;
.num-edit { .num-edit {

View File

@ -58,6 +58,7 @@ export type MDLocalInternal = {
master: { master: {
reload: (arg?: { toast: boolean }) => void; reload: (arg?: { toast: boolean }) => void;
render: () => void; render: () => void;
list?: any;
pk?: string; pk?: string;
}; };
params: { params: {

View File

@ -4,6 +4,7 @@ export const useLocal = <T extends object>(
data: T, data: T,
effect?: (arg: { effect?: (arg: {
init: boolean; init: boolean;
setDelayedRender: (arg: boolean) => void;
}) => Promise<void | (() => void)> | void | (() => void), }) => Promise<void | (() => void)> | void | (() => void),
deps?: any[] deps?: any[]
): { ): {
@ -19,12 +20,20 @@ export const useLocal = <T extends object>(
_loading: {} as any, _loading: {} as any,
lastRender: 0, lastRender: 0,
lastRenderCount: 0, lastRenderCount: 0,
delayedRender: false,
delayedRenderTimeout: null as any,
}); });
const local = _.current; const local = _.current;
useEffect(() => { useEffect(() => {
local.ready = true; local.ready = true;
if (effect) effect({ init: true }); if (effect)
effect({
init: true,
setDelayedRender(arg) {
local.delayedRender = arg;
},
});
}, []); }, []);
if (local.ready === false) { if (local.ready === false) {
@ -32,6 +41,17 @@ export const useLocal = <T extends object>(
local.data.render = () => { local.data.render = () => {
if (local.ready) { if (local.ready) {
if (local.delayedRender) {
if (Date.now() - local.lastRender > 100) {
local.lastRender = Date.now();
_render({});
} else {
clearTimeout(local.delayedRenderTimeout);
local.delayedRenderTimeout = setTimeout(local.data.render, 50);
}
return;
}
if (Date.now() - local.lastRender < 300) { if (Date.now() - local.lastRender < 300) {
local.lastRenderCount++; local.lastRenderCount++;
} else { } else {
@ -39,7 +59,9 @@ export const useLocal = <T extends object>(
} }
if (local.lastRenderCount > 300) { if (local.lastRenderCount > 300) {
throw new Error("local.render more than 300 times in less than 300ms"); throw new Error(
"local.render more than 300 times in less than 300ms"
);
} }
local.lastRender = Date.now(); local.lastRender = Date.now();
@ -54,7 +76,12 @@ export const useLocal = <T extends object>(
if (effect) { if (effect) {
setTimeout(() => { setTimeout(() => {
effect({ init: false }); effect({
init: false,
setDelayedRender(arg) {
local.delayedRender = arg;
},
});
}); });
} }
break; break;