-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow custom css stylesheets (CT-000) (#80)
- Loading branch information
1 parent
db420fb
commit 7062e0a
Showing
9 changed files
with
86 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable no-console */ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { Assistant } from '@/common'; | ||
|
||
// used to add stylesheets dynamically, resolves when loaded | ||
export const addStyleSheetURL = async (url: string) => { | ||
const link = document.createElement('link'); | ||
const load = new Promise((resolve, reject) => { | ||
link.onload = resolve; | ||
link.onerror = reject; | ||
}); | ||
|
||
link.rel = 'stylesheet'; | ||
link.href = url; | ||
document.head.appendChild(link); | ||
|
||
await load; | ||
}; | ||
|
||
// do not load until stylesheet is resolved | ||
export const useResolveAssistantStyleSheet = (assistant?: Assistant): boolean => { | ||
const [isStyleSheetResolved, setStyleSheetResolved] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!assistant || isStyleSheetResolved) return; | ||
|
||
if (!assistant.stylesheet) { | ||
setStyleSheetResolved(true); | ||
return; | ||
} | ||
|
||
// inject stylesheet url | ||
(async () => { | ||
await addStyleSheetURL(assistant.stylesheet!).catch((error) => { | ||
console.error(`failed to load stylesheet: ${assistant.stylesheet}`); | ||
console.error(error); | ||
}); | ||
setStyleSheetResolved(true); | ||
})(); | ||
}, [assistant]); | ||
|
||
return isStyleSheetResolved; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters