Skip to content

Commit

Permalink
feat: implement pagination inboxes (#617)
Browse files Browse the repository at this point in the history
* esllint

* refactor

* feat: pagination wip

* feat: change inbox name updated to v2 and fixes

* fixes

* updates
  • Loading branch information
paulclindo authored Jan 29, 2025
1 parent aa1e5b0 commit c4b85c0
Show file tree
Hide file tree
Showing 21 changed files with 410 additions and 283 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { flattenDirectoryContents } from '@shinkai_network/shinkai-node-state/lib/utils/files';
import { useGetListDirectoryContents } from '@shinkai_network/shinkai-node-state/v2/queries/getDirectoryContents/useGetListDirectoryContents';
import { useGetHealth } from '@shinkai_network/shinkai-node-state/v2/queries/getHealth/useGetHealth';
import { useGetInboxes } from '@shinkai_network/shinkai-node-state/v2/queries/getInboxes/useGetInboxes';
import { useGetInboxesWithPagination } from '@shinkai_network/shinkai-node-state/v2/queries/getInboxes/useGetInboxesWithPagination';
import { useGetLLMProviders } from '@shinkai_network/shinkai-node-state/v2/queries/getLLMProviders/useGetLLMProviders';
import { useMap } from '@shinkai_network/shinkai-ui/hooks';
import { useEffect } from 'react';
Expand All @@ -27,7 +27,7 @@ export const useOnboardingSteps = () => {
token: auth?.api_v2_key ?? '',
});

const { inboxes } = useGetInboxes({
const { data: inboxesPagination } = useGetInboxesWithPagination({
nodeAddress: auth?.node_address ?? '',
token: auth?.api_v2_key ?? '',
});
Expand Down Expand Up @@ -69,6 +69,9 @@ export const useOnboardingSteps = () => {
}, [VRFiles]);

useEffect(() => {
const inboxes =
inboxesPagination?.pages.flatMap((page) => page.inboxes) ?? [];

if (inboxes.length > 1) {
currentStepsMap.set(GetStartedSteps.CreateAIChat, GetStartedStatus.Done);
}
Expand All @@ -87,7 +90,7 @@ export const useOnboardingSteps = () => {
GetStartedStatus.Done,
);
}
}, [inboxes]);
}, [inboxesPagination]);

// useEffect(() => {
// if ((subscriptionFolder ?? [])?.length > 0) {
Expand Down
138 changes: 138 additions & 0 deletions apps/shinkai-desktop/src/components/reset-connection-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { useSubmitRegistrationNoCode } from '@shinkai_network/shinkai-node-state/v2/mutations/submitRegistation/useSubmitRegistrationNoCode';
import { useGetEncryptionKeys } from '@shinkai_network/shinkai-node-state/v2/queries/getEncryptionKeys/useGetEncryptionKeys';
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
Button,
} from '@shinkai_network/shinkai-ui';
import { submitRegistrationNoCodeError } from '@shinkai_network/shinkai-ui/helpers';
import { XIcon } from 'lucide-react';
import { useNavigate } from 'react-router-dom';

import {
useShinkaiNodeKillMutation,
useShinkaiNodeRemoveStorageMutation,
useShinkaiNodeSpawnMutation,
} from '../lib/shinkai-node-manager/shinkai-node-manager-client';
import { useAuth } from '../store/auth';
import { useShinkaiNodeManager } from '../store/shinkai-node-manager';

export const ResetConnectionDialog = ({
isOpen,
onOpenChange,
allowClose = false,
}: {
isOpen: boolean;
onOpenChange: (open: boolean) => void;
allowClose?: boolean;
}) => {
const { mutateAsync: shinkaiNodeKill, isPending: isShinkaiNodeKillPending } =
useShinkaiNodeKillMutation();
const {
mutateAsync: shinkaiNodeSpawn,
isPending: isShinkaiNodeSpawnPending,
} = useShinkaiNodeSpawnMutation({
onSuccess: async () => {
if (!encryptionKeys) return;
await submitRegistrationNoCode({
profile: 'main',
registration_name: 'main_device',
node_address: 'http://127.0.0.1:9550',
...encryptionKeys,
});
},
});
const {
mutateAsync: shinkaiNodeRemoveStorage,
isPending: isShinkaiNodeRemoveStoragePending,
} = useShinkaiNodeRemoveStorageMutation();
const { setShinkaiNodeOptions } = useShinkaiNodeManager();
const { encryptionKeys } = useGetEncryptionKeys();
const setAuth = useAuth((state) => state.setAuth);
const navigate = useNavigate();

const isResetLoading =
isShinkaiNodeKillPending ||
isShinkaiNodeRemoveStoragePending ||
isShinkaiNodeSpawnPending;

const { mutateAsync: submitRegistrationNoCode } = useSubmitRegistrationNoCode(
{
onSuccess: (response, setupPayload) => {
if (response.status !== 'success') {
shinkaiNodeKill();
}
if (response.status === 'success' && encryptionKeys) {
const updatedSetupData = {
...encryptionKeys,
...setupPayload,
permission_type: '',
shinkai_identity: response.data?.node_name ?? '',
node_signature_pk: response.data?.identity_public_key ?? '',
node_encryption_pk: response.data?.encryption_public_key ?? '',
api_v2_key: response.data?.api_v2_key ?? '',
};
setAuth(updatedSetupData);
navigate('/ai-model-installation');
onOpenChange(false);
} else {
submitRegistrationNoCodeError();
}
},
},
);

const handleReset = async () => {
await shinkaiNodeKill();
useAuth.getState().setLogout(); // clean up local storage
await shinkaiNodeRemoveStorage({ preserveKeys: true });
setShinkaiNodeOptions(null);
await shinkaiNodeSpawn();
};

return (
<AlertDialog onOpenChange={onOpenChange} open={isOpen}>
<AlertDialogContent className="w-[75%]">
{allowClose && (
<AlertDialogCancel
className="absolute right-3 top-3 border-0"
disabled={isResetLoading}
>
<XIcon className="h-4 w-4" />
</AlertDialogCancel>
)}
<AlertDialogHeader>
<AlertDialogTitle>App Reset Required</AlertDialogTitle>
<AlertDialogDescription>
<div className="flex flex-col space-y-3 text-left text-white/70">
<div className="text-sm">
We&apos;re currently in beta and we made some significant
updates to improve your experience. To apply these updates, we
need to reset your data.
<br /> <br />
If you need assistance, please contact our support team.
</div>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter className="mt-4 flex items-center justify-end gap-2.5">
<Button
className="min-w-32 text-sm"
disabled={isResetLoading}
isLoading={isResetLoading}
onClick={handleReset}
size="sm"
variant={'destructive'}
>
Reset App
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@ import { save } from '@tauri-apps/plugin-dialog';
import * as fs from '@tauri-apps/plugin-fs';
import { BaseDirectory } from '@tauri-apps/plugin-fs';
import { open } from '@tauri-apps/plugin-shell';
import {
CheckCircle,
DownloadIcon,
MoreVertical,
PlayCircle,
Rocket,
} from 'lucide-react';
import { InfoCircleIcon } from 'primereact/icons/infocircle';
import { DownloadIcon, MoreVertical, PlayCircle, Rocket } from 'lucide-react';
import { useEffect, useState } from 'react';
import { Link, useParams } from 'react-router-dom';
import { toast } from 'sonner';
Expand Down
10 changes: 6 additions & 4 deletions apps/shinkai-desktop/src/hooks/use-current-inbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGetInboxes } from '@shinkai_network/shinkai-node-state/v2/queries/getInboxes/useGetInboxes';
import { useGetInboxesWithPagination } from '@shinkai_network/shinkai-node-state/v2/queries/getInboxes/useGetInboxesWithPagination';
import { useMemo } from 'react';
import { useLocation } from 'react-router-dom';

Expand All @@ -8,16 +8,18 @@ export const useGetCurrentInbox = (inboxId?: string) => {
const auth = useAuth((state) => state.auth);
const location = useLocation();

const { inboxes } = useGetInboxes({
const { data: inboxesPagination } = useGetInboxesWithPagination({
nodeAddress: auth?.node_address ?? '',
token: auth?.api_v2_key ?? '',
});
const currentInbox = useMemo(() => {
const currentInboxId = inboxId ?? location.pathname.split('/')?.[2];

const decodedInboxId = decodeURIComponent(currentInboxId);
return inboxes.find((inbox) => decodedInboxId === inbox.inbox_id);
}, [inboxId, inboxes, location.pathname]);
return inboxesPagination?.pages
.flatMap((page) => page.inboxes)
.find((inbox) => decodedInboxId === inbox.inbox_id);
}, [inboxId, inboxesPagination, location.pathname]);

return currentInbox;
};
2 changes: 1 addition & 1 deletion apps/shinkai-desktop/src/lib/shinkai-node-overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DownloadIcon, Loader2 } from 'lucide-react';
import React, { useState } from 'react';
import { toast } from 'sonner';

import { ResetConnectionDialog } from '../pages/layout/main-layout';
import { ResetConnectionDialog } from '../components/reset-connection-dialog';
import { useAuth } from '../store/auth';
import { useShinkaiNodeManager } from '../store/shinkai-node-manager';
import { useRetrieveLogsQuery } from './shinkai-logs/logs-client';
Expand Down
Loading

0 comments on commit c4b85c0

Please sign in to comment.