Refactor giveaway job processing and update environment documentation
- Enhanced the handling of the `giveawayCreate` job in the bot's job processing for improved consistency. - Clarified the usage of `BOT_TOKEN` in the `.env.example` file for both the bot and WebUI. - Integrated the `bullmq` dependency into the WebUI package to bolster job management capabilities.
This commit is contained in:
118
apps/webui/src/components/modules/starboard-form.tsx
Normal file
118
apps/webui/src/components/modules/starboard-form.tsx
Normal file
@@ -0,0 +1,118 @@
|
||||
'use client';
|
||||
|
||||
import type { StarboardConfigDashboard } from '@nexumi/shared';
|
||||
import { useId } from 'react';
|
||||
import { useTranslations } from '@/components/locale-provider';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import type { SettingsSaveResult } from '@/components/settings/settings-form';
|
||||
import { SettingsForm } from '@/components/settings/settings-form';
|
||||
|
||||
interface LocalValue {
|
||||
enabled: boolean;
|
||||
channelId: string;
|
||||
emoji: string;
|
||||
threshold: number;
|
||||
allowSelfStar: boolean;
|
||||
ignoredChannelIdsText: string;
|
||||
}
|
||||
|
||||
function toLocal(config: StarboardConfigDashboard): LocalValue {
|
||||
return {
|
||||
enabled: config.enabled,
|
||||
channelId: config.channelId,
|
||||
emoji: config.emoji,
|
||||
threshold: config.threshold,
|
||||
allowSelfStar: config.allowSelfStar,
|
||||
ignoredChannelIdsText: config.ignoredChannelIds.join(', ')
|
||||
};
|
||||
}
|
||||
|
||||
function toRemote(value: LocalValue): StarboardConfigDashboard {
|
||||
return {
|
||||
enabled: value.enabled,
|
||||
channelId: value.channelId,
|
||||
emoji: value.emoji,
|
||||
threshold: value.threshold,
|
||||
allowSelfStar: value.allowSelfStar,
|
||||
ignoredChannelIds: value.ignoredChannelIdsText
|
||||
.split(',')
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean)
|
||||
};
|
||||
}
|
||||
|
||||
async function saveStarboard(guildId: string, value: LocalValue): Promise<SettingsSaveResult> {
|
||||
try {
|
||||
const response = await fetch(`/api/guilds/${guildId}/starboard`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(toRemote(value))
|
||||
});
|
||||
if (!response.ok) {
|
||||
const body = (await response.json().catch(() => null)) as { error?: string } | null;
|
||||
return { ok: false, error: body?.error ?? `Request failed with status ${response.status}` };
|
||||
}
|
||||
return { ok: true };
|
||||
} catch {
|
||||
return { ok: false, error: 'Network error' };
|
||||
}
|
||||
}
|
||||
|
||||
interface StarboardFormProps {
|
||||
guildId: string;
|
||||
initialValue: StarboardConfigDashboard;
|
||||
}
|
||||
|
||||
export function StarboardForm({ guildId, initialValue }: StarboardFormProps) {
|
||||
const t = useTranslations();
|
||||
const channelId = useId();
|
||||
const emojiId = useId();
|
||||
const thresholdId = useId();
|
||||
const ignoredId = useId();
|
||||
|
||||
return (
|
||||
<SettingsForm<LocalValue> initialValue={toLocal(initialValue)} onSave={(value) => saveStarboard(guildId, value)}>
|
||||
{({ value, setValue }) => (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>{t('modulePages.starboard.title')}</CardTitle>
|
||||
<CardDescription>{t('modulePages.starboard.description')}</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between rounded-md border border-border p-4">
|
||||
<Label>{t('modulePages.starboard.enabledLabel')}</Label>
|
||||
<Switch checked={value.enabled} onCheckedChange={(enabled) => setValue((prev) => ({ ...prev, enabled }))} />
|
||||
</div>
|
||||
<div className="grid gap-4 sm:grid-cols-3">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor={channelId}>{t('modulePages.starboard.channelId')}</Label>
|
||||
<Input id={channelId} value={value.channelId} onChange={(event) => setValue((prev) => ({ ...prev, channelId: event.target.value.trim() }))} placeholder="123456789012345678" />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor={emojiId}>{t('modulePages.starboard.emoji')}</Label>
|
||||
<Input id={emojiId} value={value.emoji} onChange={(event) => setValue((prev) => ({ ...prev, emoji: event.target.value }))} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor={thresholdId}>{t('modulePages.starboard.threshold')}</Label>
|
||||
<Input id={thresholdId} type="number" min={1} value={value.threshold} onChange={(event) => setValue((prev) => ({ ...prev, threshold: Number(event.target.value) || 1 }))} />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center justify-between rounded-md border border-border p-4">
|
||||
<Label>{t('modulePages.starboard.allowSelfStar')}</Label>
|
||||
<Switch checked={value.allowSelfStar} onCheckedChange={(allowSelfStar) => setValue((prev) => ({ ...prev, allowSelfStar }))} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor={ignoredId}>{t('modulePages.starboard.ignoredChannelIds')}</Label>
|
||||
<Textarea id={ignoredId} value={value.ignoredChannelIdsText} onChange={(event) => setValue((prev) => ({ ...prev, ignoredChannelIdsText: event.target.value }))} />
|
||||
<p className="text-xs text-muted-foreground">{t('modulePages.starboard.idsHint')}</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</SettingsForm>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user