fixing detail
This commit is contained in:
parent
1c030badef
commit
06712d6c7a
|
|
@ -1,7 +1,7 @@
|
||||||
import { useLocal } from "@/utils/use-local";
|
import { useLocal } from "@/utils/use-local";
|
||||||
import { cx } from "class-variance-authority";
|
import { cx } from "class-variance-authority";
|
||||||
import { ArrowRight } from "lucide-react";
|
import { ArrowRight } from "lucide-react";
|
||||||
import { FC, useEffect } from "react";
|
import { FC } from "react";
|
||||||
import { Skeleton } from "../ui/skeleton";
|
import { Skeleton } from "../ui/skeleton";
|
||||||
import { formatName } from "lib/gen/utils";
|
import { formatName } from "lib/gen/utils";
|
||||||
|
|
||||||
|
|
@ -24,9 +24,12 @@ export const Detail: FC<{
|
||||||
mode: mode,
|
mode: mode,
|
||||||
on_load,
|
on_load,
|
||||||
bound: false,
|
bound: false,
|
||||||
|
resolved_value: null as any,
|
||||||
});
|
});
|
||||||
|
|
||||||
let detail: any = _detail;
|
local.on_load = on_load;
|
||||||
|
|
||||||
|
let detail = _detail;
|
||||||
if (typeof detail !== "function") {
|
if (typeof detail !== "function") {
|
||||||
detail = (load: any) => {
|
detail = (load: any) => {
|
||||||
const result: any = {};
|
const result: any = {};
|
||||||
|
|
@ -40,77 +43,34 @@ export const Detail: FC<{
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isEditor) {
|
|
||||||
if (
|
|
||||||
location.pathname !== local.pathname ||
|
|
||||||
mode !== local.mode ||
|
|
||||||
local.on_load !== on_load
|
|
||||||
) {
|
|
||||||
local.status = "init";
|
|
||||||
local.on_load = on_load;
|
|
||||||
local.mode = mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof on_load === "object") {
|
|
||||||
local.detail = detail(on_load);
|
|
||||||
local.status = "ready";
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (local.status === "init" && typeof on_load === "function") {
|
|
||||||
local.status = "loading";
|
|
||||||
if (location.pathname === "") {
|
|
||||||
local.detail = detail({});
|
|
||||||
} else {
|
|
||||||
local.detail = detail({});
|
|
||||||
local.pathname = location.pathname;
|
|
||||||
}
|
|
||||||
local.render();
|
|
||||||
|
|
||||||
const res = on_load({
|
|
||||||
params: {},
|
|
||||||
bind: (fn) => {
|
|
||||||
if (!local.bound) {
|
|
||||||
local.bound = true;
|
|
||||||
local.render();
|
|
||||||
|
|
||||||
fn(async () => {
|
|
||||||
local.status = "loading";
|
|
||||||
local.render();
|
|
||||||
const item = await on_load({} as any);
|
|
||||||
local.detail = detail(item);
|
|
||||||
local.status = "ready";
|
|
||||||
local.render();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (typeof res === "object" && res instanceof Promise) {
|
|
||||||
res.then((item) => {
|
|
||||||
local.detail = detail(item);
|
|
||||||
local.status = "ready";
|
|
||||||
local.render();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
local.detail = detail(res);
|
|
||||||
local.status = "ready";
|
|
||||||
local.render();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [local.status]);
|
|
||||||
}
|
|
||||||
let values = {};
|
let values = {};
|
||||||
|
|
||||||
if (!isEditor) {
|
if (local.status !== "loading") {
|
||||||
values = local.detail || {};
|
|
||||||
} else {
|
|
||||||
if (typeof on_load === "function") {
|
if (typeof on_load === "function") {
|
||||||
values = detail(on_load({} as any));
|
if (local.resolved_value) {
|
||||||
|
values = detail(local.resolved_value);
|
||||||
|
local.resolved_value = null;
|
||||||
|
local.status = "ready";
|
||||||
|
} else {
|
||||||
|
const result = on_load({} as any);
|
||||||
|
if (result instanceof Promise) {
|
||||||
|
local.status = "loading";
|
||||||
|
values = detail({});
|
||||||
|
result.then((e) => {
|
||||||
|
local.resolved_value = e;
|
||||||
|
local.status = "ready";
|
||||||
|
local.render();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
values = detail(result);
|
||||||
|
local.status = "ready";
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
values = detail(on_load);
|
values = detail(on_load);
|
||||||
}
|
|
||||||
local.status = "ready";
|
local.status = "ready";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof values !== "object" || values === null) return null;
|
if (typeof values !== "object" || values === null) return null;
|
||||||
const entries = Object.entries(values);
|
const entries = Object.entries(values);
|
||||||
|
|
@ -264,7 +224,7 @@ const Linkable: FC<{
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!link) {
|
if (!link) {
|
||||||
if (status !== "ready") return loading;
|
if (status !== "ready") return status;
|
||||||
return sample || "-";
|
return sample || "-";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,8 @@ export const masterDetailParseHash = (md: MDLocal) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
md.params.links = md.params.links.filter((e) => e);
|
||||||
|
|
||||||
const parsed_link = parseLink();
|
const parsed_link = parseLink();
|
||||||
let changed = parsed_link.length !== md.params.links.length;
|
let changed = parsed_link.length !== md.params.links.length;
|
||||||
|
|
||||||
|
|
@ -92,6 +94,7 @@ const cleanHash = (hash: string) => {
|
||||||
export const breadcrumbPrefix = (md: MDLocal) => {
|
export const breadcrumbPrefix = (md: MDLocal) => {
|
||||||
let prefix: (BreadItem & { url: string })[] = [];
|
let prefix: (BreadItem & { url: string })[] = [];
|
||||||
if (md.params.links && md.params.links.length > 0) {
|
if (md.params.links && md.params.links.length > 0) {
|
||||||
|
md.params.links = md.params.links.filter((e) => e);
|
||||||
const hashes: string[] = [];
|
const hashes: string[] = [];
|
||||||
for (const link of md.params.links) {
|
for (const link of md.params.links) {
|
||||||
if (!hashes.includes(link.hash)) {
|
if (!hashes.includes(link.hash)) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue