-
Notifications
You must be signed in to change notification settings - Fork 42
[WIP] Coral embed #3427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[WIP] Coral embed #3427
Changes from 18 commits
1596c1f
3ff6867
44be7fa
945883b
2ba2ea0
279bb52
21cf40d
8924840
0c96afc
85c086e
bd8fd75
0fcf16f
fffd683
5359c5c
b034ef0
8a9fbb5
a4e3c07
2e71f60
4c0faf6
00e12aa
0d94a89
d0a041e
bea1398
51f1e87
fca0504
04c78b2
ad1428c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #coral { | ||
| --palette-primary-200: #b5cbd3; | ||
| --palette-primary-400: #377ed2; | ||
| --palette-primary-500: #1c4550; | ||
| --palette-primary-700: #163a45; | ||
| --palette-primary-800: #102f38; | ||
| --palette-primary-900: #0b252c; | ||
|
|
||
| --palette-error-600: #cc322f; | ||
| --palette-error-700: #b32c29; | ||
| --palette-error-800: #992523; | ||
| --palette-error-900: #7f1f1d; | ||
|
|
||
| --palette-success-600: #28b463; | ||
| --palette-success-700: #239b56; | ||
| --palette-success-800: #1d8348; | ||
| --palette-success-900: #186a3b; | ||
|
|
||
| /* FONTS */ | ||
| --font-family-primary: 'Open Sans', sans-serif; | ||
| --font-family-secondary: 'Nunito', sans-serif; | ||
|
|
||
| --font-weight-primary-bold: 700; | ||
| --font-weight-primary-semi-bold: 600; | ||
| --font-weight-primary-regular: 500; | ||
|
|
||
| --font-weight-secondary-bold: 700; | ||
| --font-weight-secondary-regular: 500; | ||
|
|
||
| /* SHADOWS */ | ||
| --shadow-popover: 1px 0px 4px rgba(0, 0, 0, 0.25); | ||
| } | ||
|
|
||
|
|
||
| #coral .coral.coral-viewerBox{ | ||
| display: none; | ||
| } | ||
|
|
||
| #coral{ | ||
| --palette-primary-500: #1c4550; | ||
| } | ||
|
|
||
| #coral.meg{ | ||
| --palette-primary-500:rgba(156, 57, 0, 1); | ||
| } | ||
|
|
||
| #coral.eeg{ | ||
| --palette-primary-500:rgba(134, 31, 55, 1); | ||
| } | ||
|
|
||
| #coral.pet{ | ||
| --palette-primary-500:rgba(0, 105, 192, 1); | ||
| } | ||
|
|
||
| #coral.ieeg{ | ||
| --palette-primary-500:rgba(18, 109, 62, 1); | ||
| } | ||
| #coral.mri{ | ||
| --palette-primary-500:rgba(79, 51, 130, 1); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import React from "react" | ||
| import { render, waitFor } from "@testing-library/react" | ||
| import { CoralEmbed } from "../coral-embed" | ||
| import { UserModalOpenCtx } from "../../../utils/user-login-modal-ctx" | ||
| import { vi } from "vitest" | ||
|
|
||
| vi.mock("../../../utils/user-login-modal-ctx", () => ({ | ||
| UserModalOpenCtx: React.createContext({ setUserModalOpen: vi.fn() }), | ||
| })) | ||
|
|
||
| global.fetch = vi.fn().mockResolvedValue(Promise.resolve({ | ||
| json: vi.fn().mockResolvedValue({ token: "mock-sso-token" }), | ||
| })) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably use the fetch mocking library.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am having an issue with vitest-fetch-mock. Is this some sort of version related vite issue? just adding
|
||
|
|
||
| // Mock the global Coral object | ||
| const mockCoralCreateStreamEmbed = vi.fn() | ||
| global.window.Coral = { | ||
| createStreamEmbed: mockCoralCreateStreamEmbed, | ||
| } | ||
|
|
||
| describe("CoralEmbed - Simple Embed.js Check (Vitest)", () => { | ||
| const mockStoryID = "simple-test-story" | ||
| const mockModalities = ["test"] | ||
|
|
||
| it("should check if window.Coral exists and initializeCoralEmbed is called", async () => { | ||
| render( | ||
| <UserModalOpenCtx.Provider value={{ setUserModalOpen: vi.fn() }}> | ||
| <CoralEmbed storyID={mockStoryID} modalities={mockModalities} /> | ||
| </UserModalOpenCtx.Provider>, | ||
| ) | ||
|
|
||
| // fetch and Coral initialization | ||
| await waitFor(() => { | ||
| expect(global.window.Coral).toBeDefined() | ||
| expect(mockCoralCreateStreamEmbed).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| expect(mockCoralCreateStreamEmbed).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| storyID: mockStoryID, | ||
| }), | ||
| ) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React, { useContext, useEffect, useRef } from "react" | ||
| import * as Sentry from "@sentry/react" | ||
| import { isAdmin } from "../../authentication/admin-user" | ||
| import { UserModalOpenCtx } from "../../utils/user-login-modal-ctx" | ||
|
|
||
| export const CoralEmbed: React.FC<{ storyID: string; modalities: string[] }> = ( | ||
| { storyID, modalities }, | ||
| ) => { | ||
| const coralContainerRef = useRef<HTMLDivElement>(null) | ||
| const isAdminUser = isAdmin() | ||
| const { setUserModalOpen } = useContext(UserModalOpenCtx) | ||
|
|
||
| useEffect(() => { | ||
| const fetchAndInitializeCoral = async () => { | ||
| const accessToken = document.cookie | ||
| .split("; ") | ||
| .find((cookie) => cookie.startsWith("accessToken=")) | ||
| ?.split("=")[1] | ||
| const headers = accessToken | ||
| ? { Authorization: `Bearer ${accessToken}` } | ||
| : {} | ||
thinknoack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| const response = await fetch("/api/auth/coral-sso", { headers }) | ||
| const data = await response.json() | ||
| initializeCoralEmbed(data.token) | ||
| } catch (error) { | ||
| Sentry.captureException(error) | ||
| initializeCoralEmbed(undefined) | ||
| } | ||
| } | ||
|
|
||
| const initializeCoralEmbed = (token: string | undefined) => { | ||
| if (coralContainerRef.current && window.Coral) { | ||
| window.Coral.createStreamEmbed({ | ||
| id: coralContainerRef.current.id, | ||
| autoRender: true, | ||
| rootURL: "http://localhost:5001", | ||
| storyID: storyID, | ||
| storyURL: window.location.href, | ||
| accessToken: token, | ||
| role: isAdminUser ? "ADMIN" : "COMMENTER", | ||
| containerClassName: modalities, | ||
| events: function (events) { | ||
| events.on("loginPrompt", function () { | ||
| setUserModalOpen(true) | ||
| }) | ||
| }, | ||
| }) | ||
| } | ||
| } | ||
| fetchAndInitializeCoral() | ||
| }, [storyID, isAdminUser, setUserModalOpen]) | ||
|
|
||
| return <div id="coral_thread" ref={coralContainerRef}></div> | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.