securityos/components/system/Window/useTitle.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
import { useProcesses } from "contexts/process";
import processDirectory from "contexts/process/directory";
import { useCallback } from "react";
import { PROCESS_DELIMITER, SAVE_TITLE_CHAR } from "utils/constants";
type Title = {
appendFileToTitle: (url: string, unSaved?: boolean) => void;
prependFileToTitle: (url: string, unSaved?: boolean) => void;
};
const useTitle = (id: string): Title => {
const { title } = useProcesses();
const [pid] = id.split(PROCESS_DELIMITER);
const { title: originalTitle } = processDirectory[pid] || {};
const appendFileToTitle = useCallback(
(url: string, unSaved?: boolean) => {
const appendedFile = url
? ` - ${url}${unSaved ? ` ${SAVE_TITLE_CHAR}` : ""}`
: "";
title(id, `${originalTitle}${appendedFile}`);
},
[id, originalTitle, title]
);
const prependFileToTitle = useCallback(
(url: string, unSaved?: boolean) => {
const prependedFile = url
? `${unSaved ? `${SAVE_TITLE_CHAR} ` : ""}${url} - `
: "";
title(id, `${prependedFile}${originalTitle}`);
},
[id, originalTitle, title]
);
return {
appendFileToTitle,
prependFileToTitle,
};
};
export default useTitle;