feat: add better duration selection to ban modal

This commit is contained in:
James Xylight committed 2025-02-08 13:49:19 -08:00
1 parent 5651ad475d
commit 98cb1992d2
6 files changed
+110 -41

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
<script lang="ts">
import { t } from '$lib/translations'
import { TextInput, Select, Option } from 'mono-svelte'
import type { Snippet } from 'svelte'
const multipliers = {
minute: 60,
hour: 60 * 60,
day: 60 * 60 * 24,
month: 60 * 60 * 24 * 30,
year: 60 * 60 * 24 * 365,
permanent: -1,
}
type Duration = keyof typeof multipliers
function calculateValue(number: number, duration: Duration): number {
return number * multipliers[duration]
}
interface Props {
value: number
max?: number
min?: number
number?: Snippet<[number]>
duration?: Snippet<[Duration]>
}
let {
value = $bindable(),
max,
min = 0,
number: numberSnippet,
duration: durationSnippet,
}: Props = $props()
let number = $state<number>(0)
let duration = $state<Duration>(value == -1 ? 'permanent' : 'day')
$effect(() => {
value = calculateValue(number, duration)
console.log(value)
})
</script>
<div class="flex flex-row items-end gap-2">
{#if duration != 'permanent'}
<TextInput
{min}
{max}
type="number"
label="Number"
class="w-24"
bind:value={number}
></TextInput>
{/if}
<Select
selectClass="h-[38px]"
baseClass="flex-1"
label={$t('form.time.duration')}
bind:value={duration}
>
<Option value="minute">{$t('form.time.minutes')}</Option>
<Option value="hour">{$t('form.time.hours')}</Option>
<Option value="day">{$t('form.time.days')}</Option>
<Option value="month">{$t('form.time.months')}</Option>
<Option value="year">{$t('form.time.years')}</Option>
<Option value="permanent">{$t('form.time.permanent')}</Option>
</Select>
</div>
@@ -10,6 +10,8 @@
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte' import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
import { t } from '$lib/translations' import { t } from '$lib/translations'
import CommunityLink from '../community/CommunityLink.svelte' import CommunityLink from '../community/CommunityLink.svelte'
import Duration from '$lib/components/form/Duration.svelte'
import SectionTitle from '$lib/components/ui/SectionTitle.svelte'
interface Props { interface Props {
open?: boolean open?: boolean
@@ -27,18 +29,17 @@
let reason = $state('') let reason = $state('')
let deleteData = $state(false) let deleteData = $state(false)
let expires = $state('') let expires = $state(-1)
let loading = $state(false) let loading = $state(false)
// hack due to svelte's reactive declarations // hack due to svelte's reactive declarations
const resetReason = () => { const resetReason = () => {
reason = '' reason = ''
deleteData = false deleteData = false
expires = '' expires = -1
} }
run(() => { $effect(() => {
if (item) resetReason() if (item) resetReason()
}) })
@@ -50,28 +51,8 @@
try { try {
let date: number | undefined let date: number | undefined
if (expires != '') { if (expires > 0) {
date = Date.parse(expires) date = Math.floor(Date.now() / 1000) + expires
if (Number.isNaN(date)) {
toast({
content: $t('toast.invalidDateAbsolute'),
type: 'error',
})
loading = false
return
}
if (date < Date.now()) {
toast({
content: $t('toast.invalidDateBeforeCurrent'),
type: 'error',
})
loading = false
return
}
} }
if (community) { if (community) {
@@ -81,7 +62,7 @@
person_id: item.id, person_id: item.id,
reason: reason || undefined, reason: reason || undefined,
remove_data: deleteData, remove_data: deleteData,
expires: date ? Math.floor(date / 1000) : undefined, expires: date,
}) })
} else { } else {
await getClient().banPerson({ await getClient().banPerson({
@@ -89,7 +70,7 @@
person_id: item.id, person_id: item.id,
reason: reason || undefined, reason: reason || undefined,
remove_data: deleteData, remove_data: deleteData,
expires: date ? Math.floor(date / 1000) : undefined, expires: date,
}) })
} }
@@ -137,11 +118,10 @@
{$t('moderation.ban.warning')} {$t('moderation.ban.warning')}
{/snippet} {/snippet}
</Checkbox> </Checkbox>
<TextInput <div>
bind:value={expires} <SectionTitle>{$t('moderation.ban.expires')}</SectionTitle>
label={$t('moderation.ban.expires')} <Duration bind:value={expires}></Duration>
placeholder="2024 August 5" </div>
/>
{/if} {/if}
<Button submit color="primary" {loading} disabled={loading} size="lg"> <Button submit color="primary" {loading} disabled={loading} size="lg">
{$t('form.submit')} {$t('form.submit')}
+10 -1
View File
@@ -190,7 +190,16 @@
"anotherInstance": "Find another instance" "anotherInstance": "Find another instance"
} }
}, },
"description": "Description" "description": "Description",
"time": {
"duration": "Duration",
"minutes": "Minutes",
"hours": "Hours",
"days": "Days",
"months": "Months",
"years": "Years",
"permanent": "Permanent"
}
}, },
"routes": { "routes": {
"frontpage": { "frontpage": {
-1
View File
@@ -15,7 +15,6 @@
interface Props extends Omit<HTMLInputAttributes, 'size' | 'prefix'> { interface Props extends Omit<HTMLInputAttributes, 'size' | 'prefix'> {
label?: string | undefined label?: string | undefined
value?: string
placeholder?: string placeholder?: string
disabled?: boolean disabled?: boolean
required?: boolean required?: boolean
+8 -4
View File
@@ -22,7 +22,7 @@
Icon, Icon,
type IconSource, type IconSource,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import type { HTMLSelectAttributes } from 'svelte/elements' import type { ClassValue, HTMLSelectAttributes } from 'svelte/elements'
let open = $state(false) let open = $state(false)
let element: HTMLSelectElement | undefined = $state() let element: HTMLSelectElement | undefined = $state()
@@ -47,7 +47,9 @@
size?: ButtonSize size?: ButtonSize
shadow?: ButtonShadow shadow?: ButtonShadow
id?: string id?: string
class?: string class?: ClassValue
baseClass?: ClassValue
selectClass?: ClassValue
customLabel?: import('svelte').Snippet customLabel?: import('svelte').Snippet
children?: import('svelte').Snippet children?: import('svelte').Snippet
customOption?: import('svelte').Snippet<[any]> customOption?: import('svelte').Snippet<[any]>
@@ -63,6 +65,8 @@
shadow = 'none', shadow = 'none',
id = generateID(), id = generateID(),
class: clazz = '', class: clazz = '',
baseClass = '',
selectClass = '',
customLabel, customLabel,
children, children,
customOption, customOption,
@@ -72,7 +76,7 @@
}: Props = $props() }: Props = $props()
</script> </script>
<div class="flex flex-col gap-1 {clazz}"> <div class="flex flex-col gap-1 {clazz} {baseClass}">
{#if customLabel || label} {#if customLabel || label}
<Label for={id} text={label}> <Label for={id} text={label}>
{@render customLabel?.()} {@render customLabel?.()}
@@ -90,7 +94,7 @@
shadow shadow
]} {buttonColor.secondary} appearance-none transition-colors rounded-lg text-sm ]} {buttonColor.secondary} appearance-none transition-colors rounded-lg text-sm
w-full min-w-full cursor-pointer pr-6 {buttonColor.secondary} w-full min-w-full cursor-pointer pr-6 {buttonColor.secondary}
{clazz}" {clazz} {selectClass}"
bind:value bind:value
onmousedown={(e) => { onmousedown={(e) => {
e.preventDefault() e.preventDefault()
+9 -2
View File
@@ -76,6 +76,9 @@ const timestamp = (when: string) => publishedToDate(when).getTime()
export const _toModLog = (item: ModAction): ModLog => { export const _toModLog = (item: ModAction): ModLog => {
if ('mod_ban_from_community' in item) { if ('mod_ban_from_community' in item) {
const expires = item.mod_ban_from_community.expires
? `until ${new Date(item.mod_ban_from_community.expires).toLocaleString()}`
: 'permanently'
return { return {
moderator: item.moderator, moderator: item.moderator,
moderatee: item.banned_person, moderatee: item.banned_person,
@@ -83,7 +86,7 @@ export const _toModLog = (item: ModAction): ModLog => {
actionName: item.mod_ban_from_community.banned actionName: item.mod_ban_from_community.banned
? 'banCommunity' ? 'banCommunity'
: 'unbanCommunity', : 'unbanCommunity',
reason: item.mod_ban_from_community.reason, reason: `${item.mod_ban_from_community.reason ?? ''} (banned ${expires})`,
timestamp: timestamp(item.mod_ban_from_community.when_), timestamp: timestamp(item.mod_ban_from_community.when_),
} }
} else if ('mod_remove_comment' in item) { } else if ('mod_remove_comment' in item) {
@@ -179,12 +182,16 @@ export const _toModLog = (item: ModAction): ModLog => {
reason: item.admin_purge_person.reason, reason: item.admin_purge_person.reason,
} }
} else if ('mod_ban' in item) { } else if ('mod_ban' in item) {
const expires = item.mod_ban.expires
? `until ${new Date(item.mod_ban.expires).toLocaleString()}`
: 'permanently'
return { return {
actionName: item.mod_ban.banned ? 'ban' : 'unban', actionName: item.mod_ban.banned ? 'ban' : 'unban',
timestamp: timestamp(item.mod_ban.when_), timestamp: timestamp(item.mod_ban.when_),
moderator: item.moderator, moderator: item.moderator,
moderatee: item.banned_person, moderatee: item.banned_person,
reason: item.mod_ban.reason, reason: `${item.mod_ban.reason ?? ''} (banned ${expires})`,
link: `/u/${fullUserName(item.banned_person)}`, link: `/u/${fullUserName(item.banned_person)}`,
} }
} else if ('mod_add' in item) { } else if ('mod_add' in item) {