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

fix(admin): frequent query requests in the search #7854

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { useQuery } from '@affine/core/hooks/use-query';
import { getUserByEmailQuery } from '@affine/graphql';
import { PlusIcon } from 'lucide-react';
import type { SetStateAction } from 'react';
import { startTransition, useCallback, useEffect, useState } from 'react';
import {
startTransition,
useCallback,
useEffect,
useMemo,
useState,
} from 'react';

import { useRightPanel } from '../../layout';
import { DiscardChanges } from './discard-changes';
Expand All @@ -15,6 +21,21 @@ interface DataTableToolbarProps<TData> {
setDataTable: (data: TData[]) => void;
}

const useSearch = () => {
const [value, setValue] = useState('');
const { data } = useQuery({
query: getUserByEmailQuery,
variables: { email: value },
});

const result = useMemo(() => data?.userByEmail, [data]);

return {
result,
query: setValue,
};
};

function useDebouncedValue<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState(value);

Expand All @@ -37,9 +58,10 @@ export function DataTableToolbar<TData>({
}: DataTableToolbarProps<TData>) {
const [value, setValue] = useState('');
const [dialogOpen, setDialogOpen] = useState(false);
const debouncedValue = useDebouncedValue(value, 500);
const debouncedValue = useDebouncedValue(value, 1000);
const { setRightPanelContent, openPanel, closePanel, isOpen } =
useRightPanel();
const { result, query } = useSearch();

const handleConfirm = useCallback(() => {
setRightPanelContent(<CreateUserForm onComplete={closePanel} />);
Expand All @@ -51,12 +73,9 @@ export function DataTableToolbar<TData>({
}
}, [setRightPanelContent, closePanel, dialogOpen, isOpen, openPanel]);

const result = useQuery({
query: getUserByEmailQuery,
variables: {
email: value,
},
}).data.userByEmail;
useEffect(() => {
query(debouncedValue);
}, [debouncedValue, query]);

useEffect(() => {
startTransition(() => {
Expand All @@ -68,13 +87,11 @@ export function DataTableToolbar<TData>({
setDataTable([]);
}
});
}, [data, debouncedValue, result, setDataTable, value]);
}, [data, debouncedValue, result, setDataTable]);

const onValueChange = useCallback(
(e: { currentTarget: { value: SetStateAction<string> } }) => {
startTransition(() => {
setValue(e.currentTarget.value);
});
setValue(e.currentTarget.value);
},
[]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
TableCell,
TableRow,
} from '@affine/admin/components/ui/table';
import { useQuery } from '@affine/core/hooks/use-query';
import { getUsersCountQuery } from '@affine/graphql';
import type { ColumnDef, PaginationState } from '@tanstack/react-table';
import {
flexRender,
Expand All @@ -17,6 +15,7 @@ import { type Dispatch, type SetStateAction, useEffect, useState } from 'react';

import { DataTablePagination } from './data-table-pagination';
import { DataTableToolbar } from './data-table-toolbar';
import { useUserCount } from './use-user-management';

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
Expand All @@ -36,11 +35,7 @@ export function DataTable<TData, TValue>({
pagination,
onPaginationChange,
}: DataTableProps<TData, TValue>) {
const {
data: { usersCount },
} = useQuery({
query: getUsersCountQuery,
});
const usersCount = useUserCount();

const [tableData, setTableData] = useState(data);
const table = useReactTable({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {
useMutateQueryResource,
useMutation,
} from '@affine/core/hooks/use-mutation';
import { useQuery } from '@affine/core/hooks/use-query';
import {
createChangePasswordUrlMutation,
createUserMutation,
deleteUserMutation,
getUsersCountQuery,
listUsersQuery,
updateAccountFeaturesMutation,
updateAccountMutation,
Expand Down Expand Up @@ -159,3 +161,12 @@ export const useDeleteUser = () => {

return deleteById;
};

export const useUserCount = () => {
const {
data: { usersCount },
} = useQuery({
query: getUsersCountQuery,
});
return usersCount;
};
20 changes: 2 additions & 18 deletions packages/frontend/admin/src/modules/accounts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { Separator } from '@affine/admin/components/ui/separator';
import { useQuery } from '@affine/core/hooks/use-query';
import { listUsersQuery } from '@affine/graphql';
import { useState } from 'react';

import { columns } from './components/columns';
import { DataTable } from './components/data-table';
import { useUserList } from './use-user-list';

export function AccountPage() {
const [pagination, setPagination] = useState({
pageIndex: 0,
pageSize: 10,
});
const {
data: { users },
} = useQuery({
query: listUsersQuery,
variables: {
filter: {
first: pagination.pageSize,
skip: pagination.pageIndex * pagination.pageSize,
},
},
});
const { users, pagination, setPagination } = useUserList();

return (
<div className=" h-screen flex-1 flex-col flex">
Expand Down
27 changes: 27 additions & 0 deletions packages/frontend/admin/src/modules/accounts/use-user-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useQuery } from '@affine/core/hooks/use-query';
import { listUsersQuery } from '@affine/graphql';
import { useState } from 'react';

export const useUserList = () => {
const [pagination, setPagination] = useState({
pageIndex: 0,
pageSize: 10,
});
const {
data: { users },
} = useQuery({
query: listUsersQuery,
variables: {
filter: {
first: pagination.pageSize,
skip: pagination.pageIndex * pagination.pageSize,
},
},
});

return {
users,
pagination,
setPagination,
};
};
52 changes: 8 additions & 44 deletions packages/frontend/admin/src/modules/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,14 @@ import {
} from '@affine/admin/components/ui/card';
import { ScrollArea } from '@affine/admin/components/ui/scroll-area';
import { Separator } from '@affine/admin/components/ui/separator';
import { useQueryImmutable } from '@affine/core/hooks/use-query';
import { getServerServiceConfigsQuery } from '@affine/graphql';

import { AboutAFFiNE } from './about';

type ServerConfig = {
externalUrl: string;
https: boolean;
host: string;
port: number;
path: string;
};

type MailerConfig = {
host: string;
port: number;
sender: string;
};

type DatabaseConfig = {
host: string;
port: number;
user: string;
database: string;
};

type ServerServiceConfig = {
name: string;
config: ServerConfig | MailerConfig | DatabaseConfig;
};
import type {
DatabaseConfig,
MailerConfig,
ServerConfig,
} from './use-server-service-configs';
import { useServerServiceConfigs } from './use-server-service-configs';

export function ConfigPage() {
return (
Expand Down Expand Up @@ -166,22 +144,8 @@ const MailerCard = ({ mailerConfig }: { mailerConfig?: MailerConfig }) => {
};

export function ServerServiceConfig() {
const { data } = useQueryImmutable({
query: getServerServiceConfigsQuery,
});
const server = data.serverServiceConfigs.find(
(service: ServerServiceConfig) => service.name === 'server'
);
const mailer = data.serverServiceConfigs.find(
(service: ServerServiceConfig) => service.name === 'mailer'
);
const database = data.serverServiceConfigs.find(
(service: ServerServiceConfig) => service.name === 'database'
);

const serverConfig = server?.config as ServerConfig | undefined;
const mailerConfig = mailer?.config as MailerConfig | undefined;
const databaseConfig = database?.config as DatabaseConfig | undefined;
const { serverConfig, mailerConfig, databaseConfig } =
useServerServiceConfigs();

return (
<div className="flex flex-col py-5 px-6">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useQueryImmutable } from '@affine/core/hooks/use-query';
import { getServerServiceConfigsQuery } from '@affine/graphql';
import { useMemo } from 'react';

export type ServerConfig = {
externalUrl: string;
https: boolean;
host: string;
port: number;
path: string;
};

export type MailerConfig = {
host: string;
port: number;
sender: string;
};

export type DatabaseConfig = {
host: string;
port: number;
user: string;
database: string;
};

export type ServerServiceConfig = {
name: string;
config: ServerConfig | MailerConfig | DatabaseConfig;
};

export const useServerServiceConfigs = () => {
const { data } = useQueryImmutable({
query: getServerServiceConfigsQuery,
});
const server = useMemo(
() =>
data.serverServiceConfigs.find(
(service: ServerServiceConfig) => service.name === 'server'
),
[data.serverServiceConfigs]
);
const mailer = useMemo(
() =>
data.serverServiceConfigs.find(
(service: ServerServiceConfig) => service.name === 'mailer'
),
[data.serverServiceConfigs]
);
const database = useMemo(
() =>
data.serverServiceConfigs.find(
(service: ServerServiceConfig) => service.name === 'database'
),
[data.serverServiceConfigs]
);

const serverConfig = server?.config as ServerConfig | undefined;
const mailerConfig = mailer?.config as MailerConfig | undefined;
const databaseConfig = database?.config as DatabaseConfig | undefined;

return { serverConfig, mailerConfig, databaseConfig };
};
Loading