refactor: move tabby specific libs to lib/tabby (#970)
* refactor: move useGraphQL to useGraphQLQuery * refactor: tabby-gql-client -> tabby/gql * refactor: tabby-fetcher -> tabby/fetcher * fixsupport-auth-token
parent
27d2f18302
commit
bfc2de49a3
|
|
@ -16,9 +16,9 @@ import { PropsWithChildren, useEffect, useState } from 'react'
|
||||||
import WorkerCard from './components/worker-card'
|
import WorkerCard from './components/worker-card'
|
||||||
import { useWorkers } from '@/lib/hooks/use-workers'
|
import { useWorkers } from '@/lib/hooks/use-workers'
|
||||||
import { WorkerKind } from '@/lib/gql/generates/graphql'
|
import { WorkerKind } from '@/lib/gql/generates/graphql'
|
||||||
import { useGraphQL } from '@/lib/hooks/use-graphql'
|
|
||||||
import { CopyButton } from '@/components/copy-button'
|
import { CopyButton } from '@/components/copy-button'
|
||||||
import { graphql } from '@/lib/gql/generates'
|
import { graphql } from '@/lib/gql/generates'
|
||||||
|
import { useGraphQLQuery } from '@/lib/tabby/gql'
|
||||||
|
|
||||||
const COMMUNITY_DIALOG_SHOWN_KEY = 'community-dialog-shown'
|
const COMMUNITY_DIALOG_SHOWN_KEY = 'community-dialog-shown'
|
||||||
|
|
||||||
|
|
@ -84,7 +84,7 @@ const getRegistrationTokenDocument = graphql(/* GraphQL */ `
|
||||||
function MainPanel() {
|
function MainPanel() {
|
||||||
const { data: healthInfo } = useHealth()
|
const { data: healthInfo } = useHealth()
|
||||||
const workers = useWorkers(healthInfo)
|
const workers = useWorkers(healthInfo)
|
||||||
const { data: registrationTokenRes } = useGraphQL(
|
const { data: registrationTokenRes } = useGraphQLQuery(
|
||||||
getRegistrationTokenDocument
|
getRegistrationTokenDocument
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
import { graphql } from '@/lib/gql/generates'
|
import { graphql } from '@/lib/gql/generates'
|
||||||
import { UserAuthForm } from './user-auth-form'
|
import { UserAuthForm } from './user-auth-form'
|
||||||
import { useGraphQL } from '@/lib/hooks/use-graphql'
|
|
||||||
import { useSearchParams } from 'next/navigation'
|
import { useSearchParams } from 'next/navigation'
|
||||||
|
import { useGraphQLQuery } from '@/lib/tabby/gql'
|
||||||
|
|
||||||
export const getIsAdminInitialized = graphql(/* GraphQL */ `
|
export const getIsAdminInitialized = graphql(/* GraphQL */ `
|
||||||
query GetIsAdminInitialized {
|
query GetIsAdminInitialized {
|
||||||
|
|
@ -12,7 +12,7 @@ export const getIsAdminInitialized = graphql(/* GraphQL */ `
|
||||||
`)
|
`)
|
||||||
|
|
||||||
export default function Signup() {
|
export default function Signup() {
|
||||||
const { data } = useGraphQL(getIsAdminInitialized)
|
const { data } = useGraphQLQuery(getIsAdminInitialized)
|
||||||
const title = data?.isAdminInitialized
|
const title = data?.isAdminInitialized
|
||||||
? 'Create an account'
|
? 'Create an account'
|
||||||
: 'Create an admin account'
|
: 'Create an admin account'
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ import {
|
||||||
FormMessage
|
FormMessage
|
||||||
} from '@/components/ui/form'
|
} from '@/components/ui/form'
|
||||||
import { graphql } from '@/lib/gql/generates'
|
import { graphql } from '@/lib/gql/generates'
|
||||||
import { useGraphQLForm } from '@/lib/tabby-gql-client'
|
import { useGraphQLForm } from '@/lib/tabby/gql'
|
||||||
|
|
||||||
export const registerUser = graphql(/* GraphQL */ `
|
export const registerUser = graphql(/* GraphQL */ `
|
||||||
mutation register(
|
mutation register(
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ import {
|
||||||
import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover'
|
import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover'
|
||||||
import { useEnterSubmit } from '@/lib/hooks/use-enter-submit'
|
import { useEnterSubmit } from '@/lib/hooks/use-enter-submit'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import fetcher from '@/lib/tabby-fetcher'
|
import fetcher from '@/lib/tabby/fetcher'
|
||||||
import { debounce, has } from 'lodash-es'
|
import { debounce, has } from 'lodash-es'
|
||||||
import type { ISearchHit, SearchReponse } from '@/lib/types'
|
import type { ISearchHit, SearchReponse } from '@/lib/types'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ const documents = {
|
||||||
types.GetRegistrationTokenDocument,
|
types.GetRegistrationTokenDocument,
|
||||||
'\n query GetIsAdminInitialized {\n isAdminInitialized\n }\n':
|
'\n query GetIsAdminInitialized {\n isAdminInitialized\n }\n':
|
||||||
types.GetIsAdminInitializedDocument,
|
types.GetIsAdminInitializedDocument,
|
||||||
'\n mutation register($email: String!, $password1: String!, $password2: String!, $invitationCode: String) {\n register(\n email: $email\n password1: $password1\n password2: $password2\n invitationCode: $invitationCode\n ) {\n accessToken\n refreshToken\n }\n }\n':
|
'\n mutation register(\n $email: String!\n $password1: String!\n $password2: String!\n $invitationCode: String\n ) {\n register(\n email: $email\n password1: $password1\n password2: $password2\n invitationCode: $invitationCode\n ) {\n accessToken\n refreshToken\n }\n }\n':
|
||||||
types.RegisterDocument,
|
types.RegisterDocument,
|
||||||
'\n query GetWorkers {\n workers {\n kind\n name\n addr\n device\n arch\n cpuInfo\n cpuCount\n cudaDevices\n }\n }\n':
|
'\n query GetWorkers {\n workers {\n kind\n name\n addr\n device\n arch\n cpuInfo\n cpuCount\n cudaDevices\n }\n }\n':
|
||||||
types.GetWorkersDocument,
|
types.GetWorkersDocument,
|
||||||
|
|
@ -55,8 +55,8 @@ export function graphql(
|
||||||
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
|
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
|
||||||
*/
|
*/
|
||||||
export function graphql(
|
export function graphql(
|
||||||
source: '\n mutation register($email: String!, $password1: String!, $password2: String!, $invitationCode: String) {\n register(\n email: $email\n password1: $password1\n password2: $password2\n invitationCode: $invitationCode\n ) {\n accessToken\n refreshToken\n }\n }\n'
|
source: '\n mutation register(\n $email: String!\n $password1: String!\n $password2: String!\n $invitationCode: String\n ) {\n register(\n email: $email\n password1: $password1\n password2: $password2\n invitationCode: $invitationCode\n ) {\n accessToken\n refreshToken\n }\n }\n'
|
||||||
): (typeof documents)['\n mutation register($email: String!, $password1: String!, $password2: String!, $invitationCode: String) {\n register(\n email: $email\n password1: $password1\n password2: $password2\n invitationCode: $invitationCode\n ) {\n accessToken\n refreshToken\n }\n }\n']
|
): (typeof documents)['\n mutation register(\n $email: String!\n $password1: String!\n $password2: String!\n $invitationCode: String\n ) {\n register(\n email: $email\n password1: $password1\n password2: $password2\n invitationCode: $invitationCode\n ) {\n accessToken\n refreshToken\n }\n }\n']
|
||||||
/**
|
/**
|
||||||
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
|
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
import useSWR, { SWRResponse, SWRConfiguration } from 'swr'
|
|
||||||
import { gqlClient } from '@/lib/tabby-gql-client'
|
|
||||||
import { Variables } from 'graphql-request'
|
|
||||||
import { TypedDocumentNode } from '@graphql-typed-document-node/core'
|
|
||||||
|
|
||||||
function useGraphQL<TResult, TVariables extends Variables | undefined>(
|
|
||||||
document: TypedDocumentNode<TResult, TVariables>,
|
|
||||||
variables?: TVariables,
|
|
||||||
swrConfiguration?: SWRConfiguration<TResult>
|
|
||||||
): SWRResponse<TResult> {
|
|
||||||
return useSWR(
|
|
||||||
[document, variables],
|
|
||||||
([document, variables]) => gqlClient.request(document, variables),
|
|
||||||
swrConfiguration
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export { useGraphQL }
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import useSWRImmutable from 'swr/immutable'
|
import useSWRImmutable from 'swr/immutable'
|
||||||
import { SWRResponse } from 'swr'
|
import { SWRResponse } from 'swr'
|
||||||
import fetcher from '@/lib/tabby-fetcher'
|
import fetcher from '@/lib/tabby/fetcher'
|
||||||
|
|
||||||
export interface HealthInfo {
|
export interface HealthInfo {
|
||||||
device: 'metal' | 'cpu' | 'cuda'
|
device: 'metal' | 'cpu' | 'cuda'
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { groupBy, findIndex, slice } from 'lodash-es'
|
import { groupBy, findIndex, slice } from 'lodash-es'
|
||||||
import { Worker, WorkerKind } from '@/lib/gql/generates/graphql'
|
import { Worker, WorkerKind } from '@/lib/gql/generates/graphql'
|
||||||
import { useGraphQL } from './use-graphql'
|
|
||||||
import type { HealthInfo } from './use-health'
|
import type { HealthInfo } from './use-health'
|
||||||
import { graphql } from '@/lib/gql/generates'
|
import { graphql } from '@/lib/gql/generates'
|
||||||
|
import { useGraphQLQuery } from '../tabby/gql'
|
||||||
|
|
||||||
const modelNameMap: Record<WorkerKind, 'chat_model' | 'model'> = {
|
const modelNameMap: Record<WorkerKind, 'chat_model' | 'model'> = {
|
||||||
[WorkerKind.Chat]: 'chat_model',
|
[WorkerKind.Chat]: 'chat_model',
|
||||||
|
|
@ -42,7 +42,7 @@ export const getAllWorkersDocument = graphql(/* GraphQL */ `
|
||||||
`)
|
`)
|
||||||
|
|
||||||
function useWorkers(healthInfo?: HealthInfo) {
|
function useWorkers(healthInfo?: HealthInfo) {
|
||||||
const { data } = useGraphQL(getAllWorkersDocument)
|
const { data } = useGraphQLQuery(getAllWorkersDocument)
|
||||||
let workers = data?.workers
|
let workers = data?.workers
|
||||||
|
|
||||||
const groupedWorkers = React.useMemo(() => {
|
const groupedWorkers = React.useMemo(() => {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
import { graphql } from '@/lib/gql/generates'
|
import { graphql } from '@/lib/gql/generates'
|
||||||
import useInterval from '@/lib/hooks/use-interval'
|
import useInterval from '@/lib/hooks/use-interval'
|
||||||
import { gqlClient } from '@/lib/tabby-gql-client'
|
import { gqlClient } from '@/lib/tabby/gql'
|
||||||
import { jwtDecode } from 'jwt-decode'
|
import { jwtDecode } from 'jwt-decode'
|
||||||
|
|
||||||
interface AuthData {
|
interface AuthData {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import useSWR, { SWRResponse, SWRConfiguration } from 'swr'
|
||||||
import { GraphQLClient, Variables } from 'graphql-request'
|
import { GraphQLClient, Variables } from 'graphql-request'
|
||||||
import { TypedDocumentNode } from '@graphql-typed-document-node/core'
|
import { TypedDocumentNode } from '@graphql-typed-document-node/core'
|
||||||
import { GraphQLResponse } from 'graphql-request/build/esm/types'
|
import { GraphQLResponse } from 'graphql-request/build/esm/types'
|
||||||
|
|
@ -45,3 +46,18 @@ export function useGraphQLForm<
|
||||||
}
|
}
|
||||||
return { onSubmit }
|
return { onSubmit }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useGraphQLQuery<
|
||||||
|
TResult,
|
||||||
|
TVariables extends Variables | undefined
|
||||||
|
>(
|
||||||
|
document: TypedDocumentNode<TResult, TVariables>,
|
||||||
|
variables?: TVariables,
|
||||||
|
swrConfiguration?: SWRConfiguration<TResult>
|
||||||
|
): SWRResponse<TResult> {
|
||||||
|
return useSWR(
|
||||||
|
[document, variables],
|
||||||
|
([document, variables]) => gqlClient.request(document, variables),
|
||||||
|
swrConfiguration
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue