chore: debugging
This commit is contained in:
parent
b2a822bf08
commit
e6d296d6af
|
@ -6,7 +6,7 @@ import {S5Error} from "@lumeweb/s5-js/lib/client.js";
|
|||
export interface PinningStatus {
|
||||
id: string;
|
||||
progress: number;
|
||||
status: 'processing' | 'completed';
|
||||
status: "processing" | "completed";
|
||||
}
|
||||
|
||||
// biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
|
||||
|
@ -16,42 +16,59 @@ export class PinningProcess {
|
|||
private static sdk?: Sdk;
|
||||
|
||||
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)) {
|
||||
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);
|
||||
|
||||
await s5.pin(id);
|
||||
|
||||
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 }> {
|
||||
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" };
|
||||
}
|
||||
|
||||
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> {
|
||||
let allStatuses = Array.from(PinningProcess.instances.values());
|
||||
let inProgress = allStatuses.some(status => {
|
||||
let inProgress = allStatuses.some((status) => {
|
||||
PinningProcess.checkStatus(status.id);
|
||||
return status.status !== 'completed';
|
||||
return status.status !== "completed";
|
||||
});
|
||||
|
||||
while (inProgress) {
|
||||
yield allStatuses;
|
||||
allStatuses = Array.from(PinningProcess.instances.values());
|
||||
inProgress = allStatuses.some(status => {
|
||||
inProgress = allStatuses.some((status) => {
|
||||
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) {
|
||||
const s5 = PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk()!;
|
||||
const s5 = PinningProcess.sdk
|
||||
?.protocols()
|
||||
.get<S5Client>(PROTOCOL_S5)!
|
||||
.getSdk()!;
|
||||
try {
|
||||
const ret = await s5.pinStatus(id);
|
||||
const status = PinningProcess.instances.get(id);
|
||||
|
@ -73,7 +93,7 @@ export class PinningProcess {
|
|||
if ((e as S5Error).statusCode == 404) {
|
||||
PinningProcess.instances.delete(id);
|
||||
}
|
||||
return
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,9 +21,11 @@ export const usePinning = () => {
|
|||
message: `Error pinning ${cid}`,
|
||||
description: response.message,
|
||||
});
|
||||
return Promise.reject(response);
|
||||
}
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ["pin-progress", "file"] });
|
||||
return Promise.resolve(response);
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -39,8 +41,10 @@ export const usePinning = () => {
|
|||
message: `Error removing ${cid}`,
|
||||
description: response.message,
|
||||
});
|
||||
return Promise.reject(response);
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: ["pin-progress"] });
|
||||
return Promise.resolve(response);
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -88,8 +88,10 @@ export const columns: ColumnDef<FileItem>[] = [
|
|||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuGroup>
|
||||
<DropdownMenuItem variant="destructive" onClick={() => {
|
||||
unpin(row.getValue("cid"))
|
||||
<DropdownMenuItem
|
||||
variant="destructive"
|
||||
onClick={() => {
|
||||
unpin(row.getValue("cid"));
|
||||
}}>
|
||||
<TrashIcon className="mr-2" />
|
||||
Delete
|
||||
|
@ -98,6 +100,7 @@ export const columns: ColumnDef<FileItem>[] = [
|
|||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
)},
|
||||
);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
|
|
@ -112,7 +112,7 @@ const PinFilesSchema = z.object({
|
|||
});
|
||||
|
||||
const PinFilesForm = ({ close }: { close: () => void }) => {
|
||||
const { bulkPin, pinStatus } = usePinning();
|
||||
const { bulkPin, pinStatus, pinData } = usePinning();
|
||||
const [form, fields] = useForm({
|
||||
id: "pin-files",
|
||||
constraint: getZodConstraint(PinFilesSchema),
|
||||
|
@ -130,6 +130,8 @@ const PinFilesForm = ({ close }: { close: () => void }) => {
|
|||
},
|
||||
});
|
||||
|
||||
console.log({pinStatus, pinData})
|
||||
|
||||
useEffect(() => {
|
||||
if (pinStatus === "success") {
|
||||
close();
|
||||
|
@ -148,7 +150,7 @@ const PinFilesForm = ({ close }: { close: () => void }) => {
|
|||
placeholder: "Comma separated CIDs",
|
||||
}}
|
||||
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">
|
||||
|
|
Loading…
Reference in New Issue