think/packages/client/src/data/document.ts

220 lines
5.4 KiB
TypeScript
Raw Normal View History

2022-03-12 02:31:03 +00:00
import type { IUser, IDocument } from '@think/domains';
import useSWR from 'swr';
import { useState, useCallback, useEffect } from 'react';
import { useAsyncLoading } from 'hooks/useAsyncLoading';
import { HttpClient } from 'services/HttpClient';
import { getPublicDocumentDetail } from 'services/document';
2022-02-20 11:51:55 +00:00
interface IAuthority {
id: string;
documentId: string;
userId: string;
readable: boolean;
editable: boolean;
}
2022-03-12 02:31:03 +00:00
type ICreateDocument = Partial<Pick<IDocument, 'wikiId' | 'parentDocumentId'>>;
2022-02-20 11:51:55 +00:00
type IDocumentWithAuth = { document: IDocument; authority: IAuthority };
2022-03-12 02:31:03 +00:00
type IUpdateDocument = Partial<Pick<IDocument, 'title' | 'content'>>;
2022-02-20 11:51:55 +00:00
/**
*
* @returns
*/
export const useCreateDocument = () => {
2022-03-12 02:31:03 +00:00
const [create, loading] = useAsyncLoading((data: ICreateDocument): Promise<IDocument> => {
return HttpClient.post('/document/create', data);
});
2022-02-20 11:51:55 +00:00
return { create, loading };
};
/**
*
* @param id
* @returns
*/
export const updateDocumentViews = (id: string) => {
2022-03-12 02:31:03 +00:00
return HttpClient.get('/document/views/' + id);
2022-02-20 11:51:55 +00:00
};
/**
*
* @param id
* @returns
*/
export const useDeleteDocument = (id) => {
const [deleteDocument, loading] = useAsyncLoading((): Promise<IDocument> => {
2022-03-12 02:31:03 +00:00
return HttpClient.delete('/document/delete/' + id);
2022-02-20 11:51:55 +00:00
});
return { deleteDocument, loading };
};
/**
*
* @param documentId
* @returns
*/
export const useDocumentDetail = (documentId, options = null) => {
const { data, error, mutate } = useSWR<IDocumentWithAuth>(
`/document/detail/${documentId}`,
(url) => HttpClient.get(url),
options
);
const loading = !data && !error;
const update = async (data: IUpdateDocument) => {
2022-03-12 02:31:03 +00:00
const res = await HttpClient.post('/document/update/' + documentId, data);
2022-02-20 11:51:55 +00:00
mutate();
return res;
};
2022-03-12 02:31:03 +00:00
const toggleStatus = async (data: Partial<Pick<IDocument, 'sharePassword'>>) => {
const ret = await HttpClient.post('/document/share/' + documentId, data);
2022-02-20 11:51:55 +00:00
mutate();
return ret;
};
return { data, loading, error, update, toggleStatus };
};
/**
* 10
* @returns
*/
export const useRecentDocuments = () => {
2022-03-22 05:37:53 +00:00
const { data, error, mutate } = useSWR<IDocument[]>('/document/recent', (url) => HttpClient.get(url));
2022-02-20 11:51:55 +00:00
const loading = !data && !error;
return { data, error, loading, refresh: mutate };
};
/**
*
* @param documentId
* @returns
*/
export const useDocumentStar = (documentId) => {
2022-03-12 02:31:03 +00:00
const { data, error, mutate } = useSWR<boolean>(`/collector/check/${documentId}`, () =>
HttpClient.post(`/collector/check`, {
type: 'document',
targetId: documentId,
})
2022-02-20 11:51:55 +00:00
);
const toggleStar = async () => {
2022-03-12 02:31:03 +00:00
await HttpClient.post('/collector/toggle/', {
type: 'document',
2022-02-20 11:51:55 +00:00
targetId: documentId,
});
mutate();
};
return { data, error, toggleStar };
};
/**
*
* @returns
*/
export const useStaredDocuments = () => {
2022-03-22 05:37:53 +00:00
const { data, error, mutate } = useSWR<IDocument[]>('/collector/documents', (url) => HttpClient.post(url));
2022-02-20 11:51:55 +00:00
const loading = !data && !error;
return { data, error, loading, refresh: mutate };
};
/**
*
* @param documentId
* @returns
*/
export const usePublicDocument = (documentId: string) => {
const [fetch] = useAsyncLoading(getPublicDocumentDetail);
const [document, setDocument] = useState<IDocument | null>(null);
2022-03-12 02:31:03 +00:00
const [error, setError] = useState<(Error & { statusCode?: number }) | null>(null);
2022-02-20 11:51:55 +00:00
const loading = !document && !error;
const queryData = useCallback(
2022-03-12 02:31:03 +00:00
(sharePassword = '') => {
2022-02-20 11:51:55 +00:00
fetch(documentId, { sharePassword })
.then((doc) => {
setDocument(doc);
setError(null);
})
.catch(setError);
},
[documentId, error]
);
useEffect(() => {
queryData();
}, [documentId]);
return {
data: document,
loading,
error,
query: queryData,
};
};
/**
*
* @param documentId
* @returns
*/
export const useCollaborationDocument = (documentId) => {
const { data, error, mutate } = useSWR<Array<IAuthority & IUser>>(
`/document/user/${documentId}`,
(url) => HttpClient.get(url),
{ shouldRetryOnError: false }
);
const loading = !data && !error;
const addUser = async (userName) => {
const ret = await HttpClient.post(`/document/user/${documentId}/add`, {
documentId,
userName,
readable: true,
editable: false,
});
mutate();
return ret;
};
const updateUser = async (docAuth) => {
const ret = await HttpClient.post(`/document/user/${documentId}/update`, {
documentId,
...docAuth,
});
mutate();
return ret;
};
const deleteUser = async (docAuth) => {
const ret = await HttpClient.post(`/document/user/${documentId}/delete`, {
documentId,
...docAuth,
});
mutate();
return ret;
};
return { users: data, loading, error, addUser, updateUser, deleteUser };
};
/**
*
* @param documentId
* @param isShare 访
* @returns
*/
2022-03-12 02:31:03 +00:00
export const useChildrenDocument = ({ wikiId, documentId, isShare = false }) => {
2022-02-20 11:51:55 +00:00
const { data, error, mutate } = useSWR<Array<IDocument>>(
2022-03-12 02:31:03 +00:00
isShare ? '/document/public/children' : `/document/children`,
wikiId || documentId ? (url) => HttpClient.post(url, { wikiId, documentId, isShare }) : null,
2022-02-20 11:51:55 +00:00
{ shouldRetryOnError: false }
);
const loading = !data && !error;
return { data, loading, error, refresh: mutate };
};