Files
omnivore/packages/web/lib/networking/queries/useGetRecentEmails.tsx
Jackson Harper 253297aeda Allow displaying recent email HTML
This should help with users not being able to access confirmation
links.
2023-05-04 10:36:43 +08:00

81 lines
1.6 KiB
TypeScript

import { gql } from 'graphql-request'
import useSWR from 'swr'
import { publicGqlFetcher } from '../networkHelpers'
export interface RecentEmail {
id: string
from: string
to: string
subject: string
type: string
text: string
html: string
createdAt: string
}
interface RecentEmailsResponse {
isValidating: boolean
recentEmails: RecentEmail[]
revalidate: () => void
}
interface RecentEmailsResponseData {
recentEmails: RecentEmailsData
}
interface RecentEmailsData {
recentEmails: RecentEmail[]
}
export function useGetRecentEmailsQuery(): RecentEmailsResponse {
const query = gql`
query GetRecentEmails {
recentEmails {
... on RecentEmailsSuccess {
recentEmails {
id
from
to
subject
type
text
html
createdAt
}
}
... on RecentEmailsError {
errorCodes
}
}
}
`
const { data, mutate, error, isValidating } = useSWR(query, publicGqlFetcher)
try {
if (error) {
throw error
}
if (data) {
const result = data as RecentEmailsResponseData
const recentEmails = result.recentEmails.recentEmails as RecentEmail[]
return {
isValidating,
recentEmails,
revalidate: () => {
mutate()
},
}
}
} catch (error) {
console.log('error', error)
}
return {
isValidating: true,
recentEmails: [],
// eslint-disable-next-line @typescript-eslint/no-empty-function
revalidate: () => {},
}
}