diff --git a/deeptutor/api/router_index.json b/deeptutor/api/router_index.json index 06955b938..de0013e73 100644 --- a/deeptutor/api/router_index.json +++ b/deeptutor/api/router_index.json @@ -1,5 +1,5 @@ { - "fingerprint": "83363f052eafc8a39b8d91e575ee819a8aa01b7b86d06a19167c718f5abcf757", + "fingerprint": "553b7ccd8070c649ca44126cc942f7600f0df6e5c054e902b0d84e5136f59b99", "routers": { "deeptutor.api.routers.auth:router": [ { @@ -1923,6 +1923,14 @@ } ], "deeptutor.api.routers.calendar:router": [ + { + "path": "/export.ics", + "name": "export_ics", + "kind": "http", + "methods": [ + "GET" + ] + }, { "path": "/calendars", "name": "calendars", diff --git a/web/features/calendar/components/CalendarPage.tsx b/web/features/calendar/components/CalendarPage.tsx index bb4f858f1..b87ddd2a0 100644 --- a/web/features/calendar/components/CalendarPage.tsx +++ b/web/features/calendar/components/CalendarPage.tsx @@ -11,6 +11,7 @@ import { useActiveWorkspaceId } from '@/hooks/useActiveWorkspaceId' import { useTaskBoard } from '@/lib/task-board-store' import { DEFAULT_TASK_COLORS, updateTaskCard } from '@/lib/task-board-api' import * as api from '../transport/api' +import { exportIcs } from '../transport/export' import { noteCalendarRevision, refreshCalendars, @@ -294,7 +295,7 @@ export default function CalendarPage() { const downloadIcs = async (calendarId?: string) => { setError('') try { - await api.exportIcs(calendarId) + await exportIcs(calendarId) } catch (e) { setError( e instanceof api.CalendarRequestError && e.detail diff --git a/web/features/calendar/transport/api.ts b/web/features/calendar/transport/api.ts index c3acc2b5d..bb1196c03 100644 --- a/web/features/calendar/transport/api.ts +++ b/web/features/calendar/transport/api.ts @@ -225,45 +225,3 @@ export function restoreEvent(token: string) { export function getTaskBlocks(signal?: AbortSignal) { return json>('/task-blocks', { signal }) } - -function filenameFromDisposition(header: string | null, fallback: string): string { - if (!header) return fallback - const encoded = /filename\*=UTF-8''([^;]+)/i.exec(header) - if (encoded?.[1]) { - try { - return decodeURIComponent(encoded[1].trim()) - } catch { - /* malformed percent-encoding — use the quoted form */ - } - } - const plain = /filename="?([^";]+)"?/i.exec(header) - return plain?.[1]?.trim() || fallback -} - -function saveBlob(blob: Blob, filename: string): void { - const url = URL.createObjectURL(blob) - const anchor = document.createElement('a') - anchor.href = url - anchor.download = filename - anchor.click() - URL.revokeObjectURL(url) -} - -export async function exportIcs(calendarId?: string): Promise { - const response = await apiFetch(`${endpoint}/export.ics${query({ calendar_id: calendarId })}`, { - cache: 'no-store', - }) - if (!response.ok) { - let detail = '' - try { - detail = String((await response.json())?.detail ?? '') - } catch { - /* no body */ - } - throw new CalendarRequestError(response.status, detail) - } - saveBlob( - await response.blob(), - filenameFromDisposition(response.headers.get('content-disposition'), 'calendar.ics') - ) -} diff --git a/web/features/calendar/transport/export.ts b/web/features/calendar/transport/export.ts new file mode 100644 index 000000000..41f6040b5 --- /dev/null +++ b/web/features/calendar/transport/export.ts @@ -0,0 +1,52 @@ +import { apiFetch } from '@/shared/api/client' +import { CalendarRequestError } from './api' + +/** + * Calendar-page download helper. Keep this out of `api.ts` so the workspace + * reminder runtime does not pull blob-saving code into every layout. + */ + +const endpoint = '/api/calendar' + +function filenameFromDisposition(header: string | null, fallback: string): string { + if (!header) return fallback + const encoded = /filename\*=UTF-8''([^;]+)/i.exec(header) + if (encoded?.[1]) { + try { + return decodeURIComponent(encoded[1].trim()) + } catch { + /* malformed percent-encoding — use the quoted form */ + } + } + const plain = /filename="?([^";]+)"?/i.exec(header) + return plain?.[1]?.trim() || fallback +} + +function saveBlob(blob: Blob, filename: string): void { + const url = URL.createObjectURL(blob) + const anchor = document.createElement('a') + anchor.href = url + anchor.download = filename + anchor.click() + URL.revokeObjectURL(url) +} + +export async function exportIcs(calendarId?: string): Promise { + const search = calendarId ? `?calendar_id=${encodeURIComponent(calendarId)}` : '' + const response = await apiFetch(`${endpoint}/export.ics${search}`, { + cache: 'no-store', + }) + if (!response.ok) { + let detail = '' + try { + detail = String((await response.json())?.detail ?? '') + } catch { + /* no body */ + } + throw new CalendarRequestError(response.status, detail) + } + saveBlob( + await response.blob(), + filenameFromDisposition(response.headers.get('content-disposition'), 'calendar.ics') + ) +}