Created
March 17, 2025 20:06
-
-
Save chrisoverstreet/8135f8aeb46b124709ba63662fa53205 to your computer and use it in GitHub Desktop.
bad-code.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use client'; | |
import { graphql } from '__autogen__/gql'; | |
import mime from 'mime-types'; | |
import { useCallback } from 'react'; | |
import { | |
GetSignedContractUploadUrlMutation, | |
GetSignedContractUploadUrlMutationVariables, | |
GetSignedMediaUploadUrlDocument, | |
} from '@/__autogen__/gql/graphql'; | |
import { isInArray } from '@/utils/isInArray'; | |
import { SUPPORTED_MIME_TYPES } from '@/utils/media/mime-types'; | |
import { MediaDomain } from '../media/hooks/use-manage-media'; | |
import { generateKey, uploadMedia } from '../media/upload-media'; | |
import { useMutation } from '@tanstack/react-query'; | |
import { useGraphqlClient } from '@/hooks/use-graphql-client'; | |
/** | |
* @param userEmail - Not required if the user is logged in. | |
* @param eventId - Not required if the user is logged in. | |
*/ | |
export function useUploadMedia(userEmail?: string, eventId?: string) { | |
// const [, getSignedMediaUploadUrl] = useMutation(GetSignedMediaUploadUrlDocument); | |
const { graphqlClient } = useGraphqlClient(); | |
const { mutate: getSignedMediaUploadUrl } = useMutation({ | |
mutationFn: (args: GetSignedContractUploadUrlMutationVariables) => graphqlClient.request<GetSignedContractUploadUrlMutation, GetSignedContractUploadUrlMutationVariables>(GetSignedMediaUploadUrlDocument); | |
onSuccess: () => { | |
await uploadMedia(contentType, file, data.getSignedMediaUploadUrl.uploadUrl); | |
}, | |
onSettled, | |
onError: () => { | |
if (!data?.getSignedMediaUploadUrl.uploadUrl) { | |
console.error(error); | |
throw new Error('Failed to upload media'); | |
} | |
}, | |
}) | |
return getSignedMediaUploadUrl; | |
// return useCallback( | |
// async (prefix: MediaDomain, file: File) => { | |
// const key = generateKey(prefix, file); | |
// const contentType = mime.contentType(file.type); | |
// | |
// if (!contentType || !isInArray(contentType, SUPPORTED_MIME_TYPES)) { | |
// throw new Error('Invalid file type'); | |
// } | |
// | |
// const { data, error } = await getSignedMediaUploadUrl({ contentType, key, email: userEmail, eventId }); | |
// | |
// | |
// | |
// | |
// return { | |
// id: data.getSignedMediaUploadUrl.id, | |
// url: data.getSignedMediaUploadUrl.url, | |
// }; | |
// }, | |
// [eventId, userEmail, getSignedMediaUploadUrl] | |
// ); | |
} | |
export function useUploadExternalMedia() { | |
const [, uploadExternalMedia] = useMutation(createExternalMediaDocument); | |
return useCallback( | |
async ({ url, contentType }: { contentType: string; url: string }) => { | |
const { data } = await uploadExternalMedia({ url, contentType }); | |
return data?.createExternalMedia; | |
}, | |
[uploadExternalMedia] | |
); | |
} | |
const createExternalMediaDocument = graphql(` | |
mutation CreateExternalMedia($contentType: String!, $url: String!) { | |
createExternalMedia(contentType: $contentType, url: $url) { | |
id | |
url | |
} | |
} | |
`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment