{"version":3,"sources":["../../src/user-sessions.client.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport {\n  UserSessionsError,\n  UserSessionsLoading,\n  UserSessions as UserSessionsPresentational,\n} from \"./lib/user-sessions.js\";\nimport type {\n  UserSessionsErrorProps,\n  UserSessionsLoadingProps,\n} from \"./lib/user-sessions.js\";\nimport { useSessions } from \"./api/endpoint.js\";\nimport { ApiProvider, useApi, useApiReady } from \"./api/api-provider.js\";\nimport { useWorkOsApiUrl } from \"./lib/widgets-context.js\";\nimport { ErrorBoundary } from \"./lib/error-boundary.js\";\nimport { useQuery, skipToken } from \"@tanstack/react-query\";\nimport { getClaims } from \"./api/utils.js\";\nimport { WidgetRootDomProps } from \"./lib/utils.js\";\n\ntype UserSessionsWithoutCurrentSessionIdProps = {\n  authToken: () => Promise<string>;\n  currentSessionId?: never;\n};\n\ntype UserSessionsWithCurrentSessionIdProps = {\n  authToken: string;\n  currentSessionId: string;\n};\n\ntype UserSessionsProps = WidgetRootDomProps &\n  (\n    | UserSessionsWithoutCurrentSessionIdProps\n    | UserSessionsWithCurrentSessionIdProps\n  );\n\nconst UserSessions: React.FC<UserSessionsProps> = ({\n  authToken,\n  ...passthroughProps\n}) => {\n  const baseUrl = useWorkOsApiUrl();\n  return (\n    <ErrorBoundary\n      fallbackRender={({ error }) => {\n        const { currentSessionId, ...domProps } = passthroughProps;\n        return <UserSessionsError error={error} {...domProps} />;\n      }}\n    >\n      <ApiProvider\n        widgetType=\"user-sessions\"\n        authToken={authToken}\n        baseUrl={baseUrl}\n      >\n        <UserSessionsImpl {...passthroughProps} />\n      </ApiProvider>\n    </ErrorBoundary>\n  );\n};\n\ninterface UserSessionsImplProps extends WidgetRootDomProps {\n  currentSessionId?: string;\n}\n\nconst UserSessionsImpl: React.FC<UserSessionsImplProps> = ({\n  currentSessionId,\n  ...domProps\n}) => {\n  const isApiReady = useApiReady();\n  const sessionsQuery = useSessions();\n  const currentSessionIdQuery = useCurrentSessionIdQuery(currentSessionId);\n\n  if (\n    !isApiReady ||\n    sessionsQuery.isLoading ||\n    currentSessionIdQuery.isLoading\n  ) {\n    return <UserSessionsLoading {...domProps} />;\n  }\n\n  if (sessionsQuery.isError || currentSessionIdQuery.isError) {\n    return (\n      <UserSessionsError\n        error={sessionsQuery.error || currentSessionIdQuery.error}\n        {...domProps}\n      />\n    );\n  }\n\n  return (\n    <ErrorBoundary\n      fallbackRender={({ error }) => (\n        <UserSessionsError error={error} {...domProps} />\n      )}\n    >\n      <UserSessionsPresentational\n        sessionsData={sessionsQuery.data?.data || []}\n        currentSessionId={currentSessionIdQuery.data ?? \"\"}\n        {...domProps}\n      />\n    </ErrorBoundary>\n  );\n};\n\nfunction useCurrentSessionIdQuery(currentSessionId?: string) {\n  const { authToken } = useApi();\n\n  return useQuery({\n    queryKey: [\"authToken\", authToken],\n    queryFn: authToken\n      ? async () => {\n          if (currentSessionId) {\n            return currentSessionId;\n          }\n\n          const { sid } = getClaims(authToken);\n          return sid;\n        }\n      : skipToken,\n  });\n}\n\nexport type {\n  UserSessionsProps,\n  UserSessionsLoadingProps,\n  UserSessionsErrorProps,\n};\nexport { UserSessions, UserSessionsLoading, UserSessionsError };\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6Ce;AA1Cf,2BAIO;AAKP,sBAA4B;AAC5B,0BAAiD;AACjD,6BAAgC;AAChC,4BAA8B;AAC9B,yBAAoC;AACpC,mBAA0B;AAmB1B,MAAM,eAA4C,CAAC;AAAA,EACjD;AAAA,EACA,GAAG;AACL,MAAM;AACJ,QAAM,cAAU,wCAAgB;AAChC,SACE;AAAA,IAAC;AAAA;AAAA,MACC,gBAAgB,CAAC,EAAE,MAAM,MAAM;AAC7B,cAAM,EAAE,kBAAkB,GAAG,SAAS,IAAI;AAC1C,eAAO,4CAAC,0CAAkB,OAAe,GAAG,UAAU;AAAA,MACxD;AAAA,MAEA;AAAA,QAAC;AAAA;AAA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