jitsi-roomsv2/frontend/src/hooks/useRoomName.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-04-08 15:58:11 +02:00
import { atom, useAtom } from "jotai";
2023-01-07 19:06:04 +01:00
import { useCallback, useState } from "react";
2023-04-08 15:58:11 +02:00
const roomNameAtom = atom(getRoomNameFromUrl())
2023-01-07 19:06:04 +01:00
function useRoomName() {
2023-04-08 15:58:11 +02:00
const [roomName, setRoomName] = useAtom(roomNameAtom)
2023-01-07 19:06:04 +01:00
const updateRoomName = useCallback(
(newName: string) => {
setRoomName(newName);
console.log("[Rooms] update room name", newName);
},
[setRoomName]
);
2023-04-07 16:21:15 +02:00
const updateAndSubmitRoomName = useCallback(
(newName: string) => {
setRoomName(newName);
setRoomNameInUrl(newName);
setRoomNameInTitle(newName);
console.log("[Rooms] update and submit room name", newName);
},
[setRoomName]
)
2023-02-07 20:31:07 +01:00
const submitRoomName = useCallback(() => {
setRoomNameInUrl(roomName);
setRoomNameInTitle(roomName);
}, [roomName]);
2023-01-07 19:06:04 +01:00
2023-04-07 16:21:15 +02:00
return { roomName, updateRoomName, updateAndSubmitRoomName, submitRoomName };
2023-01-07 19:06:04 +01:00
}
function getRoomNameFromUrl(): string {
const pathName = location.pathname;
const roomName = pathName
.substring(pathName.lastIndexOf("/"), pathName.length)
.replace("/", "");
console.log("[Rooms] Got roomName from url", roomName);
setRoomNameInTitle(roomName);
return roomName;
}
function setRoomNameInUrl(roomName: string) {
history.pushState(history.state, "_", roomName);
}
function setRoomNameInTitle(roomName: string) {
2023-01-10 19:09:24 +01:00
if (!!roomName) {
document.title = roomName;
}
2023-01-07 19:06:04 +01:00
}
export { useRoomName };