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,12 +1,12 @@
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>
@ -16,42 +16,59 @@ export class PinningProcess {
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 {
const s5 = PinningProcess.sdk?.protocols().get<S5Client>("s5")!.getSdk()!;
console.log({ s5 });
if (PinningProcess.instances.has(id)) { if (PinningProcess.instances.has(id)) {
return {success: false, message: "ID is already being processed"}; return { success: false, message: "ID is already being processed" };
} }
const pinningStatus: PinningStatus = {id, progress: 0, status: 'processing'}; const pinningStatus: PinningStatus = {
id,
progress: 0,
status: "processing",
};
const response = await s5.pin(id);
console.log({ response });
PinningProcess.instances.set(id, pinningStatus); PinningProcess.instances.set(id, pinningStatus);
await s5.pin(id); return { success: true, message: "Pinning process started" };
} catch (e) {
return {success: true, message: "Pinning process started"}; console.error(e);
return { success: false, message: "Pinning process failed" };
}
} }
static async unpin(id: string): Promise<{ success: boolean; message: string }> { static async unpin(
id: string,
): Promise<{ success: boolean; message: string }> {
if (!PinningProcess.instances.has(id)) { if (!PinningProcess.instances.has(id)) {
return {success: false, message: "ID not found or not being processed"}; return { success: false, message: "ID not found or not being processed" };
} }
await PinningProcess.sdk?.protocols().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> { static *pollAllProgress(): Generator<PinningStatus[], void, unknown> {
let allStatuses = Array.from(PinningProcess.instances.values()); let allStatuses = Array.from(PinningProcess.instances.values());
let inProgress = allStatuses.some(status => { let inProgress = allStatuses.some((status) => {
PinningProcess.checkStatus(status.id); PinningProcess.checkStatus(status.id);
return status.status !== 'completed'; return status.status !== "completed";
}); });
while (inProgress) { while (inProgress) {
yield allStatuses; yield allStatuses;
allStatuses = Array.from(PinningProcess.instances.values()); allStatuses = Array.from(PinningProcess.instances.values());
inProgress = allStatuses.some(status => { inProgress = allStatuses.some((status) => {
PinningProcess.checkStatus(status.id); PinningProcess.checkStatus(status.id);
return status.status !== 'completed'; return status.status !== "completed";
}); });
} }
@ -59,7 +76,10 @@ export class PinningProcess {
} }
private static async checkStatus(id: string) { private static async checkStatus(id: string) {
const s5 = PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk()!; const s5 = PinningProcess.sdk
?.protocols()
.get<S5Client>(PROTOCOL_S5)!
.getSdk()!;
try { try {
const ret = await s5.pinStatus(id); const ret = await s5.pinStatus(id);
const status = PinningProcess.instances.get(id); const status = PinningProcess.instances.get(id);
@ -73,7 +93,7 @@ export class PinningProcess {
if ((e as S5Error).statusCode == 404) { if ((e as S5Error).statusCode == 404) {
PinningProcess.instances.delete(id); PinningProcess.instances.delete(id);
} }
return return;
} }
} }

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,7 +75,7 @@ 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>
@ -88,8 +88,10 @@ export const columns: ColumnDef<FileItem>[] = [
</DropdownMenuTrigger> </DropdownMenuTrigger>
<DropdownMenuContent align="end"> <DropdownMenuContent align="end">
<DropdownMenuGroup> <DropdownMenuGroup>
<DropdownMenuItem variant="destructive" onClick={() => { <DropdownMenuItem
unpin(row.getValue("cid")) variant="destructive"
onClick={() => {
unpin(row.getValue("cid"));
}}> }}>
<TrashIcon className="mr-2" /> <TrashIcon className="mr-2" />
Delete Delete
@ -98,6 +100,7 @@ export const columns: ColumnDef<FileItem>[] = [
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
</div> </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">