Skip to content

Commit

Permalink
Refactor webhook components to remove response_body handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chimpdev committed Oct 22, 2024
1 parent f90d687 commit f6d8bb0
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 70 deletions.
29 changes: 0 additions & 29 deletions client/app/(bots)/bots/[id]/manage/components/Webhook/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import useLanguageStore, { t } from '@/stores/language';
import cn from '@/lib/cn';
import CodeBlock from '@/app/components/CodeBlock';
import { BiCodeCurly } from 'react-icons/bi';
import { FaFileCode } from 'react-icons/fa';
import revalidateBot from '@/lib/revalidate/bot';

export default function Webhook({ botId, webhookURL: currentWebhookURL, webhookToken: currentWebhookToken, records }) {
Expand Down Expand Up @@ -76,14 +75,6 @@ export default function Webhook({ botId, webhookURL: currentWebhookURL, webhookT
const [selectedRecord, setSelectedRecord] = useState(records?.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime())?.[0] || null);
const language = useLanguageStore(state => state.language);

let responseIsJSON = false;

try {
JSON.parse(selectedRecord?.response_body);
responseIsJSON = true;
// eslint-disable-next-line no-empty
} catch {}

return (
<div className='flex w-full flex-col gap-y-4'>
<div className='flex w-full flex-col items-center justify-between sm:flex-row'>
Expand Down Expand Up @@ -187,26 +178,6 @@ export default function Webhook({ botId, webhookURL: currentWebhookURL, webhookT

<div className='flex w-full lg:max-w-[50%]'>
<div className='flex w-full flex-col gap-y-2 text-xs'>
{responseIsJSON ? (
<CodeBlock
language='json'
fileName='response-body.json'
FileIcon={<BiCodeCurly />}
className='max-h-[300px]'
>
{JSON.stringify(JSON.parse(selectedRecord?.response_body), null, 2)}
</CodeBlock>
) : (
<CodeBlock
language='txt'
fileName='response-body.txt'
FileIcon={<FaFileCode />}
className='max-h-[300px]'
>
{selectedRecord?.response_body || 'N/A'}
</CodeBlock>
)}

<CodeBlock
language='json'
fileName='request-body.json'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import useLanguageStore, { t } from '@/stores/language';
import cn from '@/lib/cn';
import CodeBlock from '@/app/components/CodeBlock';
import { BiCodeCurly } from 'react-icons/bi';
import { FaFileCode } from 'react-icons/fa';
import revalidateServer from '@/lib/revalidate/server';

export default function Webhook({ serverId, webhookURL: currentWebhookURL, webhookToken: currentWebhookToken, records }) {
Expand Down Expand Up @@ -76,14 +75,6 @@ export default function Webhook({ serverId, webhookURL: currentWebhookURL, webho
const [selectedRecord, setSelectedRecord] = useState(records?.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime())?.[0] || null);
const language = useLanguageStore(state => state.language);

let responseIsJSON = false;

try {
JSON.parse(selectedRecord?.response_body);
responseIsJSON = true;
// eslint-disable-next-line no-empty
} catch {}

return (
<div className='flex w-full flex-col gap-y-4'>
<div className='flex w-full flex-col items-center justify-between sm:flex-row'>
Expand Down Expand Up @@ -187,26 +178,6 @@ export default function Webhook({ serverId, webhookURL: currentWebhookURL, webho

<div className='flex w-full lg:max-w-[50%]'>
<div className='flex w-full flex-col gap-y-2 text-xs'>
{responseIsJSON ? (
<CodeBlock
language='json'
fileName='response-body.json'
FileIcon={<BiCodeCurly />}
className='max-h-[300px]'
>
{JSON.stringify(JSON.parse(selectedRecord?.response_body), null, 2)}
</CodeBlock>
) : (
<CodeBlock
language='txt'
fileName='response-body.txt'
FileIcon={<FaFileCode />}
className='max-h-[300px]'
>
{selectedRecord?.response_body || 'N/A'}
</CodeBlock>
)}

<CodeBlock
language='json'
fileName='request-body.json'
Expand Down
4 changes: 0 additions & 4 deletions server/src/schemas/Bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ const BotSchema = new Schema({
type: Object,
required: false
},
response_body: {
type: String,
required: false
},
created_at: {
type: Date,
required: true
Expand Down
4 changes: 0 additions & 4 deletions server/src/schemas/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ const ServerSchema = new Schema({
type: Object,
required: false
},
response_body: {
type: String,
required: false
},
created_at: {
type: Date,
required: true
Expand Down
3 changes: 1 addition & 2 deletions server/src/utils/bots/incrementVote.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async function incrementVote(botId, userId, botWebhook) {

const response = await axios(requestOptions)
.catch(error => {
logger.error(`Error while sending webhook request for bot ${bot.id}: ${error}`);
logger.error(`Error while sending webhook request for bot ${bot.id}:`, error);

return error.response;
});
Expand All @@ -152,7 +152,6 @@ async function incrementVote(botId, userId, botWebhook) {
bot: bot.id,
user: user.id
},
response_body: response?.data,
created_at: new Date()
};

Expand Down
3 changes: 1 addition & 2 deletions server/src/utils/servers/incrementVote.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async function incrementVote(guildId, userId) {

const response = await axios(requestOptions)
.catch(error => {
logger.error(`Error while sending webhook request for guild ${guild.id}: ${error}`);
logger.error(`Error while sending webhook request for guild ${guild.id}:`, error);

return error.response;
});
Expand All @@ -201,7 +201,6 @@ async function incrementVote(guildId, userId) {
server: guild.id,
user: user.id
},
response_body: response?.data,
created_at: Date.now()
};

Expand Down

0 comments on commit f6d8bb0

Please sign in to comment.