Skip to content

Your favorite shift management application!

chore(dev): allow extra dev origins and add a temp-id fallback (#193)

panteL6a7a62df3927
diff --git a/.env.example b/.env.example
index 88e9724..da8d043 100644
--- a/.env.example
+++ b/.env.example
@@ -19,6 +19,7 @@
# 8. Storage -- database location
# 9. GitHub integration -- version and release check
# 10. Rate limiting -- all optional, defaults are sensible
+# 11. Local development -- dev-server settings, ignored in production
# ==============================================================================
@@ -238,3 +239,15 @@ ALLOW_GUEST_ACCESS=false # Allow viewing calendars without login
# RATE_LIMIT_ADMIN_BULK_OPERATIONS_WINDOW=300 # 5 minutes
# RATE_LIMIT_ADMIN_CALENDAR_MUTATIONS=10 # Calendar delete/transfer/edit
# RATE_LIMIT_ADMIN_CALENDAR_MUTATIONS_WINDOW=60 # 1 minute
+
+
+# ==============================================================================
+# 11. LOCAL DEVELOPMENT (optional, ignored in production)
+# ==============================================================================
+
+# Extra hosts allowed to request Next.js dev-only resources (e.g. HMR),
+# comma-separated. Needed when the dev server is reached through something
+# other than localhost -- a remote/SSH dev session accessed via LAN IP, a
+# port-forwarding tunnel, or a device on the network. Next.js logs the
+# blocked origin in its warning; put that value here.
+# DEV_ALLOWED_ORIGINS=192.168.1.1
diff --git a/hooks/useCalendarShares.ts b/hooks/useCalendarShares.ts
index 77eb03a..e35e9dc 100644
--- a/hooks/useCalendarShares.ts
+++ b/hooks/useCalendarShares.ts
@@ -10,6 +10,7 @@ import {
isRateLimitError,
} from "@/lib/rate-limit-client";
import { ApiError } from "@/lib/api-error";
+import { generateTempId } from "@/lib/utils";
import { useIsCalendarAccessible } from "@/hooks/useCalendars";
export interface CalendarShare {
@@ -185,7 +186,7 @@ export function useCalendarShares(calendarId: string) {
// Optimistic update
const optimisticShare: CalendarShare = {
- id: `temp-${crypto.randomUUID()}`,
+ id: `temp-${generateTempId()}`,
calendarId,
userId,
permission,
diff --git a/hooks/useCalendars.ts b/hooks/useCalendars.ts
index c5c3712..e803656 100644
--- a/hooks/useCalendars.ts
+++ b/hooks/useCalendars.ts
@@ -7,6 +7,7 @@ import {
} from "@tanstack/react-query";
import { CalendarWithCount } from "@/lib/types";
import type { CalendarViewSettings } from "@/lib/view-settings";
+import { generateTempId } from "@/lib/utils";
import { toast } from "sonner";
import { useTranslations } from "next-intl";
import {
@@ -203,7 +204,7 @@ export function useCalendars(initialCalendarId?: string | null) {
);
const optimisticCalendar: CalendarWithCount = {
- id: `temp-${crypto.randomUUID()}`,
+ id: `temp-${generateTempId()}`,
name,
color,
guestPermission: "none",
diff --git a/hooks/useNotes.ts b/hooks/useNotes.ts
index 1f4d8c4..3b91fb5 100644
--- a/hooks/useNotes.ts
+++ b/hooks/useNotes.ts
@@ -5,6 +5,7 @@ import { formatDateToLocal, parseLocalDate, toLocalDate } from "@/lib/date-utils
import { toast } from "sonner";
import { queryKeys } from "@/lib/query-keys";
import { ApiError } from "@/lib/api-error";
+import { generateTempId } from "@/lib/utils";
import { useIsCalendarAccessible } from "@/hooks/useCalendars";
import { LIVE_REFETCH_INTERVAL } from "@/lib/query-client";
@@ -152,7 +153,7 @@ export function useNotes(calendarId: string | undefined) {
// Create optimistic note
const optimisticNote: CalendarNote = {
- id: `temp-${crypto.randomUUID()}`,
+ id: `temp-${generateTempId()}`,
date: parseLocalDate(formData.date),
note: formData.note,
type: formData.type || "note",
diff --git a/hooks/usePresets.ts b/hooks/usePresets.ts
index 278d351..4127109 100644
--- a/hooks/usePresets.ts
+++ b/hooks/usePresets.ts
@@ -4,6 +4,7 @@ import { ShiftPreset } from "@/lib/db/schema";
import { toast } from "sonner";
import { queryKeys } from "@/lib/query-keys";
import { ApiError } from "@/lib/api-error";
+import { generateTempId } from "@/lib/utils";
import { useIsCalendarAccessible } from "@/hooks/useCalendars";
// Form data interface
@@ -102,7 +103,7 @@ function createOptimisticPreset(
formData: PresetFormData
): ShiftPreset {
return {
- id: `temp-${crypto.randomUUID()}`,
+ id: `temp-${generateTempId()}`,
calendarId,
title: formData.title,
startTime: formData.startTime,
diff --git a/hooks/useShifts.ts b/hooks/useShifts.ts
index e00f9a9..3546369 100644
--- a/hooks/useShifts.ts
+++ b/hooks/useShifts.ts
@@ -6,6 +6,7 @@ import { useTranslations } from "next-intl";
import { parseLocalDate, toLocalDate } from "@/lib/date-utils";
import { queryKeys } from "@/lib/query-keys";
import { ApiError } from "@/lib/api-error";
+import { generateTempId } from "@/lib/utils";
import { useIsCalendarAccessible } from "@/hooks/useCalendars";
import { LIVE_REFETCH_INTERVAL } from "@/lib/query-client";
@@ -145,7 +146,7 @@ export function useShifts(calendarId: string | undefined) {
// Create optimistic shift
const optimisticShift: ShiftWithCalendar = {
- id: `temp-${crypto.randomUUID()}`,
+ id: `temp-${generateTempId()}`,
date: parseLocalDate(formData.date),
startTime: formData.startTime,
endTime: formData.endTime,
diff --git a/lib/utils.ts b/lib/utils.ts
index de0cf23..e64715b 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -46,6 +46,19 @@ export function removeStorage(key: string): void {
}
}
+/**
+ * A short unique id for optimistic-UI placeholders (discarded once the real
+ * server id comes back). Prefers crypto.randomUUID(), but that API is only
+ * defined in secure contexts -- https, or http on localhost -- so a plain
+ * http dev server reached via LAN IP or hostname needs a fallback.
+ */
+export function generateTempId(): string {
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
+ return crypto.randomUUID();
+ }
+ return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
+}
+
/** Up to two initials from a name, falling back to the e-mail. */
export function getUserInitials(user: { name?: string | null; email?: string }): string {
const source = user.name?.trim();
diff --git a/next.config.ts b/next.config.ts
index df62c79..0a50b10 100644
--- a/next.config.ts
+++ b/next.config.ts
@@ -7,6 +7,16 @@ const nextConfig: NextConfig = {
/* config options here */
output: "standalone",
+ // Extra hosts allowed to hit dev-only endpoints (e.g. HMR) besides localhost.
+ // Needed for remote dev setups (VS Code Remote, port forwarding, LAN devices).
+ ...(process.env.NODE_ENV !== "production" && process.env.DEV_ALLOWED_ORIGINS
+ ? {
+ allowedDevOrigins: process.env.DEV_ALLOWED_ORIGINS.split(",")
+ .map((origin) => origin.trim())
+ .filter(Boolean),
+ }
+ : {}),
+
// No <Image> anywhere: avatars are plain <img>. Declaring that lets the
// trace exclude below drop sharp (~46 MB) without breaking a reachable path.
images: { unoptimized: true },