Files
daedalOS/hooks/useLinkHandler.ts
Dustin Brett 89acca3982
Some checks failed
Tests / tests (push) Has been cancelled
Always open in browser with https
2025-12-09 20:47:49 -08:00

58 lines
1.6 KiB
TypeScript

import { relative } from "path";
import { useCallback } from "react";
import { isCorsUrl } from "components/apps/TinyMCE/functions";
import { getProcessByFileExtension } from "components/system/Files/FileEntry/functions";
import { useProcesses } from "contexts/process";
import { haltEvent, isYouTubeUrl, getExtension } from "utils/functions";
import { useSession } from "contexts/session";
type LinkHandler = (
event: Event,
rawUrl: string,
pathName: string,
title?: string
) => void;
export const useLinkHandler = (): LinkHandler => {
const { open } = useProcesses();
const { updateRecentFiles } = useSession();
return useCallback<LinkHandler>(
(event, rawUrl, pathName, title) => {
haltEvent(event);
const url = rawUrl.replace(/^http:/i, "https:");
if (isYouTubeUrl(url)) open("VideoPlayer", { url });
else if (isCorsUrl(url)) open("Browser", { initialTitle: title, url });
else if (
!pathName ||
relative(
decodeURI(
(url.startsWith("/") ? url : `/${url}`).replace(
window.location.origin,
""
)
),
decodeURI(pathName)
) === ""
) {
const defaultProcess = getProcessByFileExtension(
getExtension(pathName)
);
if (defaultProcess) {
const pathUrl = decodeURI(pathName);
open(defaultProcess, { url: pathUrl });
if (pathUrl) updateRecentFiles(pathUrl, defaultProcess);
}
} else {
window.open(rawUrl, "_blank", "noopener, noreferrer");
}
},
[open, updateRecentFiles]
);
};