Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 93 additions & 3 deletions src/lib/components/friends/CreateMeetingEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import SelectOutlined from '$lib/components/ui/SelectOutlined.svelte';
import DateField from '$lib/components/ui/DateField.svelte';
import type { ComponentProps } from 'svelte';
import { dateLabel, formatTime, minutesTime } from '$lib/datetime';
import { dateLabel, formatTime, minutesTime, shiftDate, weekDates } from '$lib/datetime';
import { DAYS, WEEKDAYS } from '$lib/calendar/days';
import type { Weekday } from '$lib/types';
type SelectOption = Pick<
ComponentProps<typeof SelectOutlined>['options'][number],
'text' | 'value'
Expand All @@ -16,6 +18,9 @@
start = $bindable(''),
duration = $bindable('30'),
frequency = $bindable('one_time'),
repeatInterval = $bindable(1),
repeatDays = $bindable<Weekday[]>([]),
repeatUntil = $bindable(''),
calendar = $bindable(''),
invite = $bindable(true),
participants,
Expand Down Expand Up @@ -47,6 +52,9 @@
start?: string;
duration?: string;
frequency?: string;
repeatInterval?: number;
repeatDays?: Weekday[];
repeatUntil?: string;
calendar?: string;
invite?: boolean;
participants: Array<{ id: string; name: string; busy: number[][]; message?: string }>;
Expand All @@ -72,6 +80,35 @@
onretry?: () => void;
} = $props();
const end = $derived(Number(start) + Number(duration));
const startDay = $derived(WEEKDAYS[weekDates(date).indexOf(date)]);
const monthlyDay = $derived(
`${['first', 'second', 'third', 'fourth', 'last'][Math.ceil(Number(date.slice(8, 10)) / 7) - 1]} ${DAYS.find((day) => day.key === startDay)?.label}`
);
let customRepeat = $state(false);
const repeatPreset = $derived.by(() => {
if (frequency === 'one_time') return 'one_time';
if (customRepeat || repeatUntil || repeatDays.some((day) => day !== startDay)) return 'custom';
if (frequency === 'weekly' && repeatInterval <= 2)
return repeatInterval === 2 ? 'biweekly' : 'weekly';
if (frequency === 'monthly' && repeatInterval === 1) return 'monthly';
return 'custom';
});
function chooseRepeat(value: string) {
customRepeat = value === 'custom';
if (customRepeat) {
if (frequency === 'one_time') frequency = 'weekly';
return;
}
frequency = value === 'biweekly' ? 'weekly' : value;
repeatInterval = value === 'biweekly' ? 2 : 1;
repeatDays = [];
repeatUntil = '';
}
function toggleRepeatDay(day: Weekday) {
repeatDays = repeatDays.includes(day)
? repeatDays.filter((item) => item !== day)
: [...repeatDays, day];
}
const conflict = $derived(Boolean(message));
const ticks = $derived(
Array.from({ length: 5 }, (_, index) => rangeStart + ((rangeEnd - rangeStart) * index) / 4)
Expand Down Expand Up @@ -232,12 +269,56 @@
<SelectOutlined
disabled={locked || submitting}
label="Repeat"
bind:value={frequency}
bind:value={() => repeatPreset, chooseRepeat}
options={[
{ text: 'Once', value: 'one_time' },
{ text: 'Weekly through the term', value: 'weekly' }
{ text: 'Every week', value: 'weekly' },
{ text: 'Every other week', value: 'biweekly' },
{ text: `Once a month on the ${monthlyDay}`, value: 'monthly' },
{ text: 'Custom', value: 'custom' }
]}
/>
{#if repeatPreset === 'custom'}
<div class="repeat-custom">
<SelectOutlined
disabled={locked || submitting}
label="Repeats"
bind:value={frequency}
options={[
{ text: 'Daily', value: 'daily' },
{ text: 'Weekly', value: 'weekly' },
{ text: 'Monthly', value: 'monthly' }
]}
/>
<TextFieldOutlined
disabled={locked || submitting}
label={`Every how many ${frequency === 'daily' ? 'days' : frequency === 'monthly' ? 'months' : 'weeks'}`}
type="number"
bind:value={
() => String(repeatInterval), (value) => (repeatInterval = Number(value))
}
/>
{#if frequency === 'weekly'}
<div class="repeat-days" role="group" aria-label="Repeat on">
{#each DAYS as day (day.key)}<Chip
variant="general"
selected={day.key === startDay || repeatDays.includes(day.key)}
aria-pressed={day.key === startDay || repeatDays.includes(day.key)}
aria-label={day.label}
disabled={locked || submitting || day.key === startDay}
onclick={() => toggleRepeatDay(day.key)}>{day.abbr}</Chip
>{/each}
</div>
{/if}
<DateField
disabled={locked || submitting}
label="Ends on (optional, defaults to the end of the term)"
bind:value={repeatUntil}
min={date}
max={date ? shiftDate(date, 366) : undefined}
/>
</div>
{/if}
<SelectOutlined
disabled={locked || submitting}
label="Add to calendar"
Expand Down Expand Up @@ -497,6 +578,15 @@
.options-content :global(input) {
box-sizing: border-box;
}
.repeat-custom {
display: grid;
gap: 1rem;
}
.repeat-days {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.invite-label {
display: flex;
align-items: center;
Expand Down
10 changes: 8 additions & 2 deletions src/lib/components/friends/FriendsPlanner.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { PEOPLE_ICON, REFRESH_ICON } from '$lib/icons';
import { dateLabel, formatTime, minutesTime, normalizeTime, timeMinutes } from '$lib/datetime';
import { validDate } from '$lib/friends/schedule';
import { providerLabel } from '$lib/friends/savedMeetings';
import { providerLabel, repeatLabel, type SavedMeeting } from '$lib/friends/savedMeetings';
import type {
Participant,
FriendGroup,
Expand Down Expand Up @@ -297,7 +297,13 @@
function draftMeta(item: PlannerDraft) {
const parts: string[] = [];
if (item.location?.trim()) parts.push(item.location.trim());
if (item.frequency === 'weekly') parts.push('Weekly');
if (item.frequency && item.frequency !== 'one_time')
parts.push(
repeatLabel({
frequency: item.frequency as SavedMeeting['frequency'],
repeat_interval: item.repeatInterval
})
);
if (item.calendar) parts.push(item.calendar.split(',').map(providerLabel).join(', '));
if (item.invite === true) parts.push('Invites on');
else if (item.invite === false) parts.push('Invites off');
Expand Down
42 changes: 38 additions & 4 deletions src/lib/components/friends/MeetingEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import { getPanelSession } from '$lib/panel/session';
import { API, ApiError } from '$lib/api';
import {
FREQUENCIES,
providerLabel,
repeatLabel,
type CalendarDestination,
type SavedMeeting,
type SavedMeetingInput
} from '$lib/friends/savedMeetings';
import { WEEKDAYS } from '$lib/calendar/days';
import type { MeetingDraft } from '$lib/friends/types';
import { processedData, termClasses, userSettings } from '$lib/stores';
import {
Expand Down Expand Up @@ -144,6 +148,18 @@
const start = timeMinutes(draft.slot.start)!;
const end = busyEndMinutes(draft.slot.end)!;
if (end - start > 720) throw new Error('Meetings can last up to 12 hours.');
const repeats = draft.frequency !== 'one_time';
const interval = draft.repeatInterval ?? 1;
if (repeats && (!Number.isInteger(interval) || interval < 1 || interval > 30))
throw new Error('Repeat every 1 to 30 days, weeks, or months.');
if (
repeats &&
draft.repeatUntil &&
(draft.repeatUntil < draft.slot.date ||
draft.repeatUntil > shiftDate(draft.slot.date, 366))
)
throw new Error('End the repeats within a year of the first meeting.');
const startDay = WEEKDAYS[weekDates(draft.slot.date).indexOf(draft.slot.date)];
const payload: SavedMeetingInput = {
title,
location,
Expand All @@ -154,6 +170,13 @@
end === 1440 ? '00:00' : minutesTime(end)
),
frequency: draft.frequency,
...(repeats && { repeat_interval: interval }),
...(repeats &&
draft.frequency === 'weekly' &&
draft.repeatDays?.length && {
repeat_days: [...new Set([startDay, ...draft.repeatDays])]
}),
...(repeats && draft.repeatUntil && { repeat_until: draft.repeatUntil }),
invite_friends: draft.inviteFriends,
destinations: [...draft.destinations]
};
Expand Down Expand Up @@ -366,9 +389,15 @@
bind:frequency={
() => draft!.frequency,
(value) => {
if (value === 'one_time' || value === 'weekly') draft!.frequency = value;
if (FREQUENCIES.includes(value as SavedMeeting['frequency']))
draft!.frequency = value as SavedMeeting['frequency'];
}
}
bind:repeatInterval={
() => draft!.repeatInterval ?? 1, (value) => (draft!.repeatInterval = value)
}
bind:repeatDays={() => draft!.repeatDays ?? [], (value) => (draft!.repeatDays = value)}
bind:repeatUntil={() => draft!.repeatUntil ?? '', (value) => (draft!.repeatUntil = value)}
bind:calendar={
() => draft!.destinations[0] ?? '',
(value) => {
Expand Down Expand Up @@ -443,9 +472,14 @@
<p>{participants.map((person) => person.name).join(', ')}</p>
{#if draft.location}<p>{draft.location}</p>{/if}
<p class="m3-font-body-small">
{draft.destinations.map(providerLabel).join(', ')} - {draft.frequency === 'weekly'
? 'Weekly through the term'
: 'Once'} - Invites {draft.inviteFriends ? 'on' : 'off'}
{draft.destinations.map(providerLabel).join(', ')} - {repeatLabel({
frequency: draft.frequency,
repeat_interval: draft.repeatInterval
})}{draft.frequency !== 'one_time'
? draft.repeatUntil
? ` through ${dateLabel(draft.repeatUntil)}`
: ' through the term'
: ''} - Invites {draft.inviteFriends ? 'on' : 'off'}
</p>
{#if draft.error || validation}<p class="text-error" role="alert">
{draft.error || validation}
Expand Down
7 changes: 6 additions & 1 deletion src/lib/friends/planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function copyDraft(draft: MeetingDraft): MeetingDraft {
...draft,
period: { ...draft.period },
slot: { ...draft.slot },
destinations: [...draft.destinations]
destinations: [...draft.destinations],
repeatDays: draft.repeatDays && [...draft.repeatDays]
};
}

Expand All @@ -43,6 +44,9 @@ export function openMeetingDraft(ui: PanelUi, period: FreePeriod, start: number,
destinationDefaultsPending: !saved,
inviteFriends: saved?.inviteFriends ?? true,
frequency: saved?.frequency ?? 'one_time',
repeatInterval: saved?.repeatInterval,
repeatDays: saved?.repeatDays && [...saved.repeatDays],
repeatUntil: saved?.repeatUntil,
idempotencyKey: crypto.randomUUID()
};
ui.highlightedSlot = undefined;
Expand Down Expand Up @@ -70,6 +74,7 @@ export function draftSummary(ui: PanelUi): PlannerDraft | null {
.map((person) => person.name),
location: draft.location,
frequency: draft.frequency,
repeatInterval: draft.repeatInterval,
calendar: draft.destinations.join(','),
invite: draft.inviteFriends
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/friends/savedMeetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { record, timestamp } from './validate';

export type CalendarDestination = 'google' | 'microsoft' | 'ics';

const FREQUENCIES = ['one_time', 'daily', 'weekly', 'monthly'] as const;
export const FREQUENCIES = ['one_time', 'daily', 'weekly', 'monthly'] as const;
const repeatUnits = { daily: 'day', weekly: 'week', monthly: 'month' } as const;

export function repeatLabel(meeting: Pick<SavedMeeting, 'frequency' | 'repeat_interval'>) {
Expand Down Expand Up @@ -80,6 +80,9 @@ export type SavedMeetingInput = {
end_time: string;
friend_ids: string[];
frequency?: SavedMeeting['frequency'];
repeat_interval?: number;
repeat_days?: Weekday[];
repeat_until?: string;
invite_friends?: boolean;
destinations?: SavedMeeting['destinations'];
};
Expand Down
6 changes: 5 additions & 1 deletion src/lib/friends/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FriendIdentity } from '../types';
import type { FriendIdentity, Weekday } from '../types';
import type { CalendarDestination, SavedMeeting, SavedMeetingInput } from './savedMeetings';

export type Participant = FriendIdentity & {
Expand Down Expand Up @@ -46,6 +46,7 @@ export type PlannerDraft = MinuteRange & {
participantNames: string[];
location?: string;
frequency?: string;
repeatInterval?: number;
calendar?: string;
invite?: boolean;
};
Expand All @@ -66,6 +67,9 @@ export type MeetingDraft = {
destinations: CalendarDestination[];
inviteFriends: boolean;
frequency: SavedMeeting['frequency'];
repeatInterval?: number;
repeatDays?: Weekday[];
repeatUntil?: string;
idempotencyKey: string;
destinationDefaultsPending?: boolean;
submission?: SavedMeetingInput;
Expand Down
Loading