Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing API key through Link socket connection #1118

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/components/Account/ApiKeysCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useDisclosure } from '@mantine/hooks';
import { openConfirmModal } from '@mantine/modals';
import { ApiKey } from '@prisma/client';
import { trpc } from '~/utils/trpc';
import {
Text,
Expand All @@ -16,7 +15,7 @@ import {
Center,
Paper,
} from '@mantine/core';
import { IconPlus, IconCopy, IconTrash } from '@tabler/icons-react';
import { IconPlus, IconTrash } from '@tabler/icons-react';
import { formatDate } from '~/utils/date-helpers';
import { ApiKeyModal } from '~/components/Account/ApiKeyModal';

Expand Down
23 changes: 21 additions & 2 deletions src/components/CivitaiLink/CivitaiLinkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '~/workers/civitai-link-worker-types';
import { MantineColor } from '@mantine/styles';
import { useFeatureFlags } from '~/providers/FeatureFlagsProvider';
import { trpc } from '~/utils/trpc';

type CivitaiLinkStatus = (typeof statuses)[number];
const statuses = [
Expand Down Expand Up @@ -146,6 +147,13 @@ const Provider = ({ children }: { children: React.ReactNode }) => {
const [connected, setConnected] = useState(false);
const [error, setError] = useState<string>();
const setActivities = useCivitaiLinkStore((state) => state.setActivities);
const trpcContext = trpc.useContext();
const trpcUtils = trpc.useUtils();
const { mutateAsync } = trpc.apiKey.add.useMutation({
async onSuccess() {
await trpcContext.apiKey.getAllUserKeys.invalidate();
},
});

//TODO.civitai-link - timeout when setting active instance

Expand Down Expand Up @@ -222,9 +230,20 @@ const Provider = ({ children }: { children: React.ReactNode }) => {
worker.port.postMessage(req);
};

const selectInstance = (id: number) => workerReq({ type: 'join', id });
const selectInstance = async (id: number) => {
// Look for existing API key when connecting to an instance
const apiKeys = await trpcUtils.apiKey.getAllUserKeys.fetch({});
const apiKey = apiKeys.find((x) => x.name === `link:${id}`);

return workerReq({ type: 'join', id });
};
const deselectInstance = () => workerReq({ type: 'leave' });
const createInstance = (id?: number) => workerReq({ type: 'create', id });
const createInstance = async (id?: number) => {
// Create an API key when creating a new instance
const apiKey = await mutateAsync({ name: `link:${id}`, scope: ['Read', 'Write'] });

return workerReq({ type: 'create', id });
};
const deleteInstance = (id: number) => workerReq({ type: 'delete', id });
const renameInstance = (id: number, name: string) => workerReq({ type: 'rename', id, name });

Expand Down