All files / discord-guild-workspace/lib liveNotifications.ts

100% Statements 85/85
100% Branches 43/43
100% Functions 3/3
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861x 1x 1x 53x 53x 53x 53x 53x 53x 53x 41x 53x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 53x 4x 4x 4x 4x 4x 53x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 53x 53x 1x 1x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 1x 46x 1x 45x 91x 91x 91x 91x 91x 91x 91x 91x 91x 13x 10x 2x 8x 7x 1x 91x  
import type { LiveNotificationsView } from "../sections/useLiveNotifications"
 
export function getTwitchSourceFeedback(
  status: LiveNotificationsView["source"]["status"]
): {
  description: string | null
  recovery: "retry" | "connect" | "reconnect" | null
} {
  switch (status) {
    case "ready":
      return { description: null, recovery: null }
    case "configurationUnavailable":
      return {
        description:
          "Cleo's Twitch server configuration is unavailable. Try again later.",
        recovery: "retry" as const,
      }
    case "unavailable":
      return {
        description:
          "Twitch connection verification is temporarily unavailable. Try again later.",
        recovery: "retry" as const,
      }
    case "needsLink":
      return {
        description:
          "The Discord server owner must connect Twitch to enable this feature.",
        recovery: "connect" as const,
      }
    case "stale":
      return {
        description:
          "The owner's saved Twitch connection is stale. Reconnect and sync it in Cleo.",
        recovery: "reconnect" as const,
      }
    case "missingPermission":
      return {
        description:
          "The server owner must reconnect Twitch to grant the required permission, then sync the connection.",
        recovery: "reconnect" as const,
      }
  }
}
 
export function getLiveNotificationState(
  view:
    | {
        botLeft: boolean
        config: {
          liveNotificationsEnabled: boolean
          liveNotificationChannelId?: string
        }
        source: { status: string }
        subscriptionStatus: string
        discordStatus: string
      }
    | undefined,
  error = false
): string {
  if (error || view?.botLeft) return "Provider unavailable"
  if (!view) return "Loading"
  if (view.source.status === "configurationUnavailable")
    return "Server configuration unavailable"
  if (!view.config.liveNotificationsEnabled)
    return view.subscriptionStatus === "providerUnavailable"
      ? "Provider unavailable"
      : view.subscriptionStatus === "failed"
        ? "Subscription failed"
        : "Disabled"
  if (view.source.status === "needsLink") return "Connect Twitch"
  if (view.source.status === "stale") return "Reconnect required"
  if (view.source.status === "missingPermission") return "Missing permission"
  if (view.source.status !== "ready") return "Provider unavailable"
  if (view.discordStatus === "unavailable") return "Provider unavailable"
  if (view.discordStatus === "needsChannel") return "Needs channel"
  if (view.discordStatus === "needsRole") return "Needs role"
  if (!view.config.liveNotificationChannelId) return "Needs channel"
  return view.subscriptionStatus === "ready"
    ? "Ready"
    : view.subscriptionStatus === "connecting"
      ? "Connecting"
      : view.subscriptionStatus === "providerUnavailable"
        ? "Provider unavailable"
        : "Subscription failed"
}