Skip to content

Commit

Permalink
refactor: remove unused file content hooks (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia authored Jan 22, 2025
1 parent a5348e4 commit 6c61d88
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 491 deletions.
10 changes: 0 additions & 10 deletions src/item/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,6 @@ export const getSharedItems = async ({
.then(({ data }) => data),
);

export const getFileContent = async (
id: UUID,
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
axios
.get<Blob>(`${API_HOST}/${buildDownloadFilesRoute(id)}`, {
responseType: 'blob',
})
.then(({ data }) => data);

export const getFileContentUrl = async (
id: UUID,
{ API_HOST, axios }: PartialQueryConfigForApi,
Expand Down
19 changes: 9 additions & 10 deletions src/item/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import nock from 'nock';
import { afterEach, describe, expect, it } from 'vitest';

import {
FILE_RESPONSE,
THUMBNAIL_URL_RESPONSE,
UNAUTHORIZED_RESPONSE,
buildResultOfData,
generateFolders,
Expand Down Expand Up @@ -413,22 +413,21 @@ describe('useItems', () => {
// TODO: errors, contains errors, full errors
});

describe('useFileContent', () => {
describe('useFileContentUrl', () => {
afterEach(() => {
nock.cleanAll();
queryClient.clear();
});

const response = FILE_RESPONSE;
const response = THUMBNAIL_URL_RESPONSE;
const { id } = LocalFileItemFactory();
const route = `/${buildDownloadFilesRoute(id)}`;
const hook = () => hooks.useFileContent(id);
const key = itemKeys.single(id).file({ replyUrl: false });
const route = `/${buildDownloadFilesRoute(id)}?replyUrl=true`;
const hook = () => hooks.useFileContentUrl(id);
const key = itemKeys.single(id).file({ replyUrl: true });

it(`Receive file content`, async () => {
it(`Receive file url`, async () => {
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });

expect(data).toBeTruthy();
// verify cache keys
expect(queryClient.getQueryData(key)).toBeTruthy();
Expand All @@ -438,7 +437,7 @@ describe('useFileContent', () => {
const endpoints = [{ route, response }];
const { data, isFetched } = await mockHook({
endpoints,
hook: () => hooks.useFileContent(undefined),
hook: () => hooks.useFileContentUrl(undefined),
wrapper,
enabled: false,
});
Expand All @@ -453,7 +452,7 @@ describe('useFileContent', () => {
// build endpoint for each item
const endpoints: Endpoint[] = [];
const { data, isFetched } = await mockHook({
hook: () => hooks.useFileContent(id, { enabled: false }),
hook: () => hooks.useFileContentUrl(id, { enabled: false }),
endpoints,
wrapper,
enabled: false,
Expand Down
25 changes: 1 addition & 24 deletions src/item/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from './accessible/hooks.js';
import * as Api from './api.js';
import { useDescendants } from './descendants/hooks.js';
import { useItemThumbnail, useItemThumbnailUrl } from './thumbnail/hooks.js';
import { useItemThumbnailUrl } from './thumbnail/hooks.js';
import { ItemChildrenParams } from './types.js';

const config = (
Expand Down Expand Up @@ -170,28 +170,6 @@ const config = (
});
},

/**
* @deprecated use url alternative when possible
* @param id itemId to download content from
* @returns Blob of the content
*/
useFileContent: (
id?: UUID,
{ enabled = true }: { enabled?: boolean } = {},
) =>
useQuery({
queryKey: itemKeys.single(id).file({ replyUrl: false }),
queryFn: () => {
if (!id) {
throw new UndefinedArgument();
}
return Api.getFileContent(id, queryConfig);
},
enabled: Boolean(id) && enabled,
...defaultQueryOptions,
staleTime: CONSTANT_KEY_STALE_TIME_MILLISECONDS,
}),

useFileContentUrl: (
id?: UUID,
{ enabled = true }: { enabled?: boolean } = {},
Expand All @@ -211,7 +189,6 @@ const config = (

useItemFeedbackUpdates: itemWsHooks?.useItemFeedbackUpdates,

useItemThumbnail: useItemThumbnail(queryConfig),
useItemThumbnailUrl: useItemThumbnailUrl(queryConfig),
};
};
Expand Down
17 changes: 0 additions & 17 deletions src/item/thumbnail/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,6 @@ import {
buildUploadItemThumbnailRoute,
} from '../routes.js';

export const downloadItemThumbnail = async (
{ id, size = DEFAULT_THUMBNAIL_SIZE }: { id: UUID; size?: string },
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
axios
.get<Blob>(
`${API_HOST}/${buildDownloadItemThumbnailRoute({
id,
size,
replyUrl: false,
})}`,
{
responseType: 'blob',
},
)
.then(({ data }) => data);

export const downloadItemThumbnailUrl = async (
{ id, size = DEFAULT_THUMBNAIL_SIZE }: { id: UUID; size?: string },
{ API_HOST, axios }: PartialQueryConfigForApi,
Expand Down
129 changes: 0 additions & 129 deletions src/item/thumbnail/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import nock from 'nock';
import { afterEach, describe, expect, it } from 'vitest';

import {
THUMBNAIL_BLOB_RESPONSE,
THUMBNAIL_URL_RESPONSE,
UNAUTHORIZED_RESPONSE,
} from '../../../test/constants.js';
Expand All @@ -15,134 +14,6 @@ import { buildDownloadItemThumbnailRoute } from '../routes.js';

const { hooks, wrapper, queryClient } = setUpTest();

describe('useItemThumbnail', () => {
afterEach(() => {
nock.cleanAll();
queryClient.clear();
});

const item = FolderItemFactory();
const replyUrl = false;
const key = itemKeys.single(item.id).thumbnail({ replyUrl });
const response = THUMBNAIL_BLOB_RESPONSE;
const route = `/${buildDownloadItemThumbnailRoute({
id: item.id,
replyUrl,
})}`;
const hook = () => hooks.useItemThumbnail({ id: item.id });

it(`Receive default thumbnail`, async () => {
const endpoints = [
{
route,
response,
headers: {
'Content-Type': 'image/jpeg',
},
},
];
const { data } = await mockHook({ endpoints, hook, wrapper });

expect(data).toBeTruthy();
// verify cache keys
expect(queryClient.getQueryData(key)).toBeTruthy();
});

it(`Receive large thumbnail`, async () => {
const size = ThumbnailSize.Large;
const routeLarge = `/${buildDownloadItemThumbnailRoute({
id: item.id,
size,
replyUrl,
})}`;
const hookLarge = () => hooks.useItemThumbnail({ id: item.id, size });
const keyLarge = itemKeys.single(item.id).thumbnail({ size });

const endpoints = [
{
route: routeLarge,
response,
headers: {
'Content-Type': 'image/jpeg',
},
},
];
const { data } = await mockHook({
endpoints,
hook: hookLarge,
wrapper,
});

expect(data).toBeTruthy();
// verify cache keys
expect(queryClient.getQueryData(keyLarge)).toBeTruthy();
});

it(`Undefined id does not fetch`, async () => {
const endpoints = [
{
route,
response,
},
];
const { data, isFetched } = await mockHook({
endpoints,
hook: () => hooks.useItemThumbnail({ id: undefined }),
wrapper,
enabled: false,
});

expect(data).toBeFalsy();
expect(isFetched).toBeFalsy();
// verify cache keys
expect(queryClient.getQueryData(key)).toBeFalsy();
});

it(`Does not fetch if item has no thumbnail`, async () => {
const itemWithoutThumbnail = {
...item,
settings: { hasThumbnail: false },
};
queryClient.setQueryData(
itemKeys.single(itemWithoutThumbnail.id).content,
itemWithoutThumbnail,
);
const endpoints = [
{
route,
response,
},
];
const { data, isFetched } = await mockHook({
endpoints,
hook: () => hooks.useItemThumbnail({ id: itemWithoutThumbnail.id }),
wrapper,
enabled: false,
});

expect(data).toBeFalsy();
expect(isFetched).toBeFalsy();
// verify cache keys
expect(queryClient.getQueryData(key)).toBeFalsy();
});

it(`Unauthorized`, async () => {
const endpoints = [
{
route,
response: UNAUTHORIZED_RESPONSE,
statusCode: StatusCodes.UNAUTHORIZED,
},
];
const { data, isError } = await mockHook({ endpoints, hook, wrapper });

expect(data).toBeFalsy();
expect(isError).toBeTruthy();
// verify cache keys
expect(queryClient.getQueryData(key)).toBeFalsy();
});
});

describe('useItemThumbnailUrl', () => {
afterEach(() => {
nock.cleanAll();
Expand Down
30 changes: 1 addition & 29 deletions src/item/thumbnail/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,7 @@ import {
import { UndefinedArgument } from '../../config/errors.js';
import { itemKeys } from '../../keys.js';
import { QueryClientConfig } from '../../types.js';
import { downloadItemThumbnail, downloadItemThumbnailUrl } from './api.js';

/**
* @deprecated use useItemThumbnailUrl
*/
export const useItemThumbnail =
(queryConfig: QueryClientConfig) =>
({ id, size = DEFAULT_THUMBNAIL_SIZE }: { id?: UUID; size?: string }) => {
const { defaultQueryOptions } = queryConfig;
const queryClient = useQueryClient();
let shouldFetch = true;
if (id) {
shouldFetch =
queryClient.getQueryData<PackedItem>(itemKeys.single(id).content)
?.settings?.hasThumbnail ?? true;
}
return useQuery({
queryKey: itemKeys.single(id).thumbnail({ size, replyUrl: false }),
queryFn: () => {
if (!id) {
throw new UndefinedArgument();
}
return downloadItemThumbnail({ id, size }, queryConfig);
},
...defaultQueryOptions,
enabled: Boolean(id) && shouldFetch,
staleTime: CONSTANT_KEY_STALE_TIME_MILLISECONDS,
});
};
import { downloadItemThumbnailUrl } from './api.js';

// create a new thumbnail hook because of key content
/**
Expand Down
Loading

0 comments on commit 6c61d88

Please sign in to comment.