chore: debugging

This commit is contained in:
Juan Di Toro 2024-03-26 15:54:51 +01:00
parent b2a822bf08
commit e6d296d6af
4 changed files with 116 additions and 87 deletions

View File

@ -1,83 +1,103 @@
import {PROTOCOL_S5, Sdk} from "@lumeweb/portal-sdk"; import { PROTOCOL_S5, Sdk } from "@lumeweb/portal-sdk";
import {useSdk} from "~/components/lib/sdk-context.js"; import { useSdk } from "~/components/lib/sdk-context.js";
import {S5Client} from "@lumeweb/s5-js"; import { S5Client } from "@lumeweb/s5-js";
import {S5Error} from "@lumeweb/s5-js/lib/client.js"; import { S5Error } from "@lumeweb/s5-js/lib/client.js";
export interface PinningStatus { export interface PinningStatus {
id: string; id: string;
progress: number; progress: number;
status: 'processing' | 'completed'; status: "processing" | "completed";
} }
// biome-ignore lint/complexity/noStaticOnlyClass: <explanation> // biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
export class PinningProcess { export class PinningProcess {
private static instances: Map<string, PinningStatus> = new Map(); private static instances: Map<string, PinningStatus> = new Map();
private static sdk?: Sdk; private static sdk?: Sdk;
static async pin(id: string): Promise<{ success: boolean; message: string }> { static async pin(id: string): Promise<{ success: boolean; message: string }> {
const s5 = PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk()!; try {
if (PinningProcess.instances.has(id)) { const s5 = PinningProcess.sdk?.protocols().get<S5Client>("s5")!.getSdk()!;
return {success: false, message: "ID is already being processed"}; console.log({ s5 });
} if (PinningProcess.instances.has(id)) {
return { success: false, message: "ID is already being processed" };
}
const pinningStatus: PinningStatus = {id, progress: 0, status: 'processing'}; const pinningStatus: PinningStatus = {
PinningProcess.instances.set(id, pinningStatus); id,
progress: 0,
status: "processing",
};
await s5.pin(id); const response = await s5.pin(id);
console.log({ response });
PinningProcess.instances.set(id, pinningStatus);
return {success: true, message: "Pinning process started"}; return { success: true, message: "Pinning process started" };
} catch (e) {
console.error(e);
return { success: false, message: "Pinning process failed" };
}
}
static async unpin(
id: string,
): Promise<{ success: boolean; message: string }> {
if (!PinningProcess.instances.has(id)) {
return { success: false, message: "ID not found or not being processed" };
} }
static async unpin(id: string): Promise<{ success: boolean; message: string }> { await PinningProcess.sdk
if (!PinningProcess.instances.has(id)) { ?.protocols()
return {success: false, message: "ID not found or not being processed"}; .get<S5Client>(PROTOCOL_S5)!
} .getSdk()
.unpin(id);
await PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk().unpin(id); return { success: true, message: "Pinning process removed" };
}
return {success: true, message: "Pinning process removed"} static *pollAllProgress(): Generator<PinningStatus[], void, unknown> {
let allStatuses = Array.from(PinningProcess.instances.values());
let inProgress = allStatuses.some((status) => {
PinningProcess.checkStatus(status.id);
return status.status !== "completed";
});
while (inProgress) {
yield allStatuses;
allStatuses = Array.from(PinningProcess.instances.values());
inProgress = allStatuses.some((status) => {
PinningProcess.checkStatus(status.id);
return status.status !== "completed";
});
} }
static* pollAllProgress(): Generator<PinningStatus[], void, unknown> { yield allStatuses ?? []; // Yield the final statuses
let allStatuses = Array.from(PinningProcess.instances.values()); }
let inProgress = allStatuses.some(status => {
PinningProcess.checkStatus(status.id);
return status.status !== 'completed';
});
while (inProgress) { private static async checkStatus(id: string) {
yield allStatuses; const s5 = PinningProcess.sdk
allStatuses = Array.from(PinningProcess.instances.values()); ?.protocols()
inProgress = allStatuses.some(status => { .get<S5Client>(PROTOCOL_S5)!
PinningProcess.checkStatus(status.id); .getSdk()!;
return status.status !== 'completed'; try {
}); const ret = await s5.pinStatus(id);
} const status = PinningProcess.instances.get(id);
if (!status) {
return;
}
yield allStatuses ?? []; // Yield the final statuses status.progress = ret.progress;
status.status = ret.status as any;
} catch (e) {
if ((e as S5Error).statusCode == 404) {
PinningProcess.instances.delete(id);
}
return;
} }
}
private static async checkStatus(id: string) { public static setupSdk(sdk: Sdk) {
const s5 = PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk()!; PinningProcess.sdk = sdk;
try { }
const ret = await s5.pinStatus(id);
const status = PinningProcess.instances.get(id);
if (!status) {
return;
}
status.progress = ret.progress;
status.status = ret.status as any;
} catch (e) {
if ((e as S5Error).statusCode == 404) {
PinningProcess.instances.delete(id);
}
return
}
}
public static setupSdk(sdk: Sdk) {
PinningProcess.sdk = sdk;
}
} }

View File

@ -21,9 +21,11 @@ export const usePinning = () => {
message: `Error pinning ${cid}`, message: `Error pinning ${cid}`,
description: response.message, description: response.message,
}); });
return Promise.reject(response);
} }
queryClient.invalidateQueries({ queryKey: ["pin-progress", "file"] }); queryClient.invalidateQueries({ queryKey: ["pin-progress", "file"] });
return Promise.resolve(response);
}, },
}); });
@ -39,8 +41,10 @@ export const usePinning = () => {
message: `Error removing ${cid}`, message: `Error removing ${cid}`,
description: response.message, description: response.message,
}); });
return Promise.reject(response);
} }
queryClient.invalidateQueries({ queryKey: ["pin-progress"] }); queryClient.invalidateQueries({ queryKey: ["pin-progress"] });
return Promise.resolve(response);
}, },
}); });

View File

@ -75,29 +75,32 @@ export const columns: ColumnDef<FileItem>[] = [
header: () => null, header: () => null,
size: 20, size: 20,
cell: ({ row }) => { cell: ({ row }) => {
const {unpin} = usePinning(); const { unpin } = usePinning();
return ( return (
<div className="flex w-5 items-center justify-between"> <div className="flex w-5 items-center justify-between">
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger <DropdownMenuTrigger
className={cn( className={cn(
"hidden group-hover:block data-[state=open]:block", "hidden group-hover:block data-[state=open]:block",
row.getIsSelected() && "block", row.getIsSelected() && "block",
)}> )}>
<MoreIcon /> <MoreIcon />
</DropdownMenuTrigger> </DropdownMenuTrigger>
<DropdownMenuContent align="end"> <DropdownMenuContent align="end">
<DropdownMenuGroup> <DropdownMenuGroup>
<DropdownMenuItem variant="destructive" onClick={() => { <DropdownMenuItem
unpin(row.getValue("cid")) variant="destructive"
}}> onClick={() => {
<TrashIcon className="mr-2" /> unpin(row.getValue("cid"));
Delete }}>
</DropdownMenuItem> <TrashIcon className="mr-2" />
</DropdownMenuGroup> Delete
</DropdownMenuContent> </DropdownMenuItem>
</DropdownMenu> </DropdownMenuGroup>
</div> </DropdownMenuContent>
)}, </DropdownMenu>
</div>
);
},
}, },
]; ];

View File

@ -112,7 +112,7 @@ const PinFilesSchema = z.object({
}); });
const PinFilesForm = ({ close }: { close: () => void }) => { const PinFilesForm = ({ close }: { close: () => void }) => {
const { bulkPin, pinStatus } = usePinning(); const { bulkPin, pinStatus, pinData } = usePinning();
const [form, fields] = useForm({ const [form, fields] = useForm({
id: "pin-files", id: "pin-files",
constraint: getZodConstraint(PinFilesSchema), constraint: getZodConstraint(PinFilesSchema),
@ -130,6 +130,8 @@ const PinFilesForm = ({ close }: { close: () => void }) => {
}, },
}); });
console.log({pinStatus, pinData})
useEffect(() => { useEffect(() => {
if (pinStatus === "success") { if (pinStatus === "success") {
close(); close();
@ -148,7 +150,7 @@ const PinFilesForm = ({ close }: { close: () => void }) => {
placeholder: "Comma separated CIDs", placeholder: "Comma separated CIDs",
}} }}
labelProps={{ htmlFor: "cids", children: "Content to Pin" }} labelProps={{ htmlFor: "cids", children: "Content to Pin" }}
errors={fields.cids.errors} errors={fields.cids.errors || pinStatus === "error" ? ["An error occurred, please try again"] : []}
/> />
<Button type="submit" className="w-full"> <Button type="submit" className="w-full">