Skip to content

Commit

Permalink
Merge pull request #371 from openchatai/widget/minor-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
faltawy committed Dec 6, 2023
2 parents a753676 + 32903d3 commit 259e55a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-widget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
paths:
- "copilot-widget/**"
jobs:
build-and-publish:
build-and-upload-script:
runs-on: ubuntu-latest

steps:
Expand All @@ -18,7 +18,7 @@ jobs:
run: cd copilot-widget/ && npm install

- name: Build copilot-widget
run: cd copilot-widget/ && npm run build
run: cd copilot-widget/ && npm run build:embed

- name: Upload embed as artifact.
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion copilot-widget/lib/contexts/axiosInstance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const AxiosContext = createContext<AxiosInstanceProps | undefined>(undefined);
// prefred it separated for the future.
export function AxiosProvider({ children }: { children: ReactNode }) {
const config = useConfigData();
const { sessionId } = useSessionId();
const { sessionId } = useSessionId(config?.token || 'defaultToken');
const axiosInstance: AxiosInstance = createAxiosInstance(config?.apiUrl, sessionId, config?.token);

return (
Expand Down
26 changes: 10 additions & 16 deletions copilot-widget/lib/hooks/useSessionId.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { useLayoutEffect, useState } from "react";
import { useEffect, useState } from "react";

export const SESSION_ID_KEY = "@openchatai:session_id";
function gtSessionId() {
return sessionStorage.getItem(SESSION_ID_KEY);
// the session id will be copilotId:uniqueId
function randomString(length: number = 10) {
return Math.random().toString(36).substring(2, length + 2);
}
export function useSessionId() {
const [sessionId, setSessionId] = useState<string | undefined | null>(gtSessionId);
useLayoutEffect(() => {
const $sessionId = sessionStorage.getItem(SESSION_ID_KEY);
if ($sessionId) {
setSessionId(sessionId);
} else {
const newSessionId = Math.random().toString(36).substring(2, 15);
sessionStorage.setItem(SESSION_ID_KEY, newSessionId);
setSessionId(newSessionId);
}
}, []);

export function useSessionId(copilotToken: string) {
const [sessionId, setSessionId] = useState<string>(sessionStorage.getItem(copilotToken) || randomString());
useEffect(() => {
sessionStorage.setItem(copilotToken, sessionId);
}, [sessionId, copilotToken]);
return { sessionId, setSessionId };
}
2 changes: 1 addition & 1 deletion copilot-widget/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@openchatai/copilot-widget",
"private": false,
"version": "1.3.1",
"version": "1.4.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@hookform/resolvers": "^3.3.1",
"@openchatai/copilot-widget": "^1.3.1",
"@openchatai/copilot-widget": "^1.4.0",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.0.4",
Expand Down
8 changes: 4 additions & 4 deletions dashboard/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 259e55a

Please sign in to comment.