Skip to content

Commit

Permalink
fix: desktop: service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Dec 4, 2024
1 parent 835832d commit 9ec9ffe
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 66 deletions.
54 changes: 22 additions & 32 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@filen/web",
"private": false,
"version": "0.1.78",
"version": "0.1.79",
"type": "module",
"description": "Filen Web & Desktop app",
"scripts": {
Expand All @@ -23,7 +23,7 @@
"@alptugidin/react-circular-progress-bar": "^1.1.2",
"@emoji-mart/data": "^1.1.2",
"@emoji-mart/react": "^1.1.1",
"@filen/desktop": "^3.0.36",
"@filen/desktop": "^3.0.38",
"@filen/network-drive": "^0.9.38",
"@filen/sdk": "^0.1.189",
"@filen/sync": "^0.1.91",
Expand Down
8 changes: 5 additions & 3 deletions src/components/dialogs/previewDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { cn, isValidFileName } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import useErrorToast from "@/hooks/useErrorToast"
import useIsServiceWorkerOnline from "@/hooks/useIsServiceWorkerOnline"
import useIsDesktopHTTPServerOnline from "@/hooks/useIsDesktopHTTPServerOnline"

const goToPreviewTypes = ["audio", "docx", "image", "pdf"]

Expand Down Expand Up @@ -83,6 +84,7 @@ export const PreviewDialog = memo(() => {
const location = useLocation()
const errorToast = useErrorToast()
const isServiceWorkerOnline = useIsServiceWorkerOnline()
const isDesktopHTTPServerOnline = useIsDesktopHTTPServerOnline()

const itemsOrdered = useMemo(() => {
if (!open) {
Expand Down Expand Up @@ -272,7 +274,7 @@ export const PreviewDialog = memo(() => {

const previewType = fileNameToPreviewType(itm.name)
const maxPreviewSize =
isServiceWorkerOnline && itm.type === "file" && isFileStreamable(itm.name, itm.mime)
(isServiceWorkerOnline || isDesktopHTTPServerOnline) && itm.type === "file" && isFileStreamable(itm.name, itm.mime)
? MAX_PREVIEW_SIZE_SW
: MAX_PREVIEW_SIZE_WEB

Expand All @@ -282,7 +284,7 @@ export const PreviewDialog = memo(() => {

try {
if (
isServiceWorkerOnline &&
(isServiceWorkerOnline || isDesktopHTTPServerOnline) &&
(previewType === "audio" || previewType === "video" || previewType === "image") &&
isFileStreamable(itm.name, itm.mime)
) {
Expand Down Expand Up @@ -333,7 +335,7 @@ export const PreviewDialog = memo(() => {
cleanup()
}
},
[cleanup, isServiceWorkerOnline]
[cleanup, isServiceWorkerOnline, isDesktopHTTPServerOnline]
)

const saveFile = useCallback(async () => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/drive/list/item/contextMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { type WorkerToMainMessage } from "@/lib/worker/types"
import Input from "@/components/input"
import useIsServiceWorkerOnline from "@/hooks/useIsServiceWorkerOnline"
import worker from "@/lib/worker"
import useIsDesktopHTTPServerOnline from "@/hooks/useIsDesktopHTTPServerOnline"

const iconSize = 16

Expand Down Expand Up @@ -97,6 +98,7 @@ export const ContextMenu = memo(
const successToast = useSuccessToast()
const publicLinkURLState = usePublicLinkURLState()
const isServiceWorkerOnline = useIsServiceWorkerOnline()
const isDesktopHTTPServerOnline = useIsDesktopHTTPServerOnline()

const isInsidePublicLink = useMemo(() => {
return location.includes("/f/") || location.includes("/d/")
Expand Down Expand Up @@ -692,7 +694,7 @@ export const ContextMenu = memo(
const contextMenuContent = useMemo((): React.ReactNode => {
const groups: Record<string, React.ReactNode[]> = {}
const maxPreviewSize =
isServiceWorkerOnline && item.type === "file" && isFileStreamable(item.name, item.mime)
(isServiceWorkerOnline || isDesktopHTTPServerOnline) && item.type === "file" && isFileStreamable(item.name, item.mime)
? MAX_PREVIEW_SIZE_SW
: MAX_PREVIEW_SIZE_WEB

Expand Down Expand Up @@ -1091,7 +1093,8 @@ export const ContextMenu = memo(
isServiceWorkerOnline,
item,
manageShareOut,
removeShared
removeShared,
isDesktopHTTPServerOnline
])

useEffect(() => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/drive/list/item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import useDriveListColumnSize from "@/hooks/useDriveListColumnSize"
import { useDoubleTap } from "use-double-tap"
import useIsMobile from "@/hooks/useIsMobile"
import useIsServiceWorkerOnline from "@/hooks/useIsServiceWorkerOnline"
import useIsDesktopHTTPServerOnline from "@/hooks/useIsDesktopHTTPServerOnline"

let draggedItems: DriveCloudItem[] = []

Expand Down Expand Up @@ -89,6 +90,7 @@ export const ListItem = memo(({ item, index, type }: { item: DriveCloudItem; ind
const driveListColumnSize = useDriveListColumnSize()
const isMobile = useIsMobile()
const isServiceWorkerOnline = useIsServiceWorkerOnline()
const isDesktopHTTPServerOnline = useIsDesktopHTTPServerOnline()

const previewType = useMemo(() => {
return fileNameToPreviewType(item.name)
Expand Down Expand Up @@ -135,7 +137,7 @@ export const ListItem = memo(({ item, index, type }: { item: DriveCloudItem; ind
const onDoubleClick = useCallback(
(e?: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
const maxPreviewSize =
isServiceWorkerOnline && item.type === "file" && isFileStreamable(item.name, item.mime)
(isServiceWorkerOnline || isDesktopHTTPServerOnline) && item.type === "file" && isFileStreamable(item.name, item.mime)
? MAX_PREVIEW_SIZE_SW
: MAX_PREVIEW_SIZE_WEB

Expand Down Expand Up @@ -191,7 +193,8 @@ export const ListItem = memo(({ item, index, type }: { item: DriveCloudItem; ind
navigating,
setSearch,
isServiceWorkerOnline,
triggerMoreIconContextMenu
triggerMoreIconContextMenu,
isDesktopHTTPServerOnline
]
)

Expand Down
18 changes: 12 additions & 6 deletions src/components/publicLink/file/file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import DocXPreview from "@/components/dialogs/previewDialog/docx"
import VideoPreview from "@/components/dialogs/previewDialog/video"
import useIsMobile from "@/hooks/useIsMobile"
import AudioPreview from "@/components/dialogs/previewDialog/audio"
import { MAX_PREVIEW_SIZE_WEB, MAX_PREVIEW_SIZE_SW } from "@/constants"
import { MAX_PREVIEW_SIZE_WEB, MAX_PREVIEW_SIZE_SW, DESKTOP_HTTP_SERVER_PORT } from "@/constants"
import useIsServiceWorkerOnline from "@/hooks/useIsServiceWorkerOnline"
import useIsDesktopHTTPServerOnline from "@/hooks/useIsDesktopHTTPServerOnline"

export const File = memo(({ info }: { info?: Omit<FileLinkInfoResponse, "size"> & { size: number } }) => {
const filePublicLinkInfo = useFilePublicLinkInfo(info)
Expand All @@ -33,6 +34,7 @@ export const File = memo(({ info }: { info?: Omit<FileLinkInfoResponse, "size">
const [hidePreview, setHidePreview] = useState<boolean>(false)
const isMobile = useIsMobile()
const isServiceWorkerOnline = useIsServiceWorkerOnline()
const isDesktopHTTPServerOnline = useIsDesktopHTTPServerOnline()

const downloadEnabled = useMemo(() => {
if (!filePublicLinkInfo.status) {
Expand Down Expand Up @@ -77,10 +79,10 @@ export const File = memo(({ info }: { info?: Omit<FileLinkInfoResponse, "size">
return null
}

return isServiceWorkerOnline && item.type === "file" && isFileStreamable(item.name, item.mime)
return (isServiceWorkerOnline || isDesktopHTTPServerOnline) && item.type === "file" && isFileStreamable(item.name, item.mime)
? MAX_PREVIEW_SIZE_SW
: MAX_PREVIEW_SIZE_WEB
}, [isServiceWorkerOnline, item])
}, [isServiceWorkerOnline, item, isDesktopHTTPServerOnline])

const previewType = useMemo(() => {
if (!item || urlState.hidePreview) {
Expand Down Expand Up @@ -244,7 +246,7 @@ export const File = memo(({ info }: { info?: Omit<FileLinkInfoResponse, "size">

try {
if (
isServiceWorkerOnline &&
(isServiceWorkerOnline || isDesktopHTTPServerOnline) &&
(previewType === "audio" || previewType === "video" || previewType === "image") &&
isFileStreamable(item.name, item.mime)
) {
Expand All @@ -263,7 +265,11 @@ export const File = memo(({ info }: { info?: Omit<FileLinkInfoResponse, "size">
"utf-8"
).toString("base64")

setURLObject(`${window.location.origin}/sw/stream?file=${encodeURIComponent(fileBase64)}`)
setURLObject(
isServiceWorkerOnline
? `${window.location.origin}/sw/stream?file=${encodeURIComponent(fileBase64)}`
: `http://localhost:${DESKTOP_HTTP_SERVER_PORT}/stream?file=${encodeURIComponent(fileBase64)}`
)

return
}
Expand All @@ -287,7 +293,7 @@ export const File = memo(({ info }: { info?: Omit<FileLinkInfoResponse, "size">
} catch (e) {
console.error(e)
}
}, [item, canLoadItem, maxPreviewSize, isServiceWorkerOnline])
}, [item, canLoadItem, maxPreviewSize, isServiceWorkerOnline, isDesktopHTTPServerOnline])

useEffect(() => {
if (item && canLoadItem && !didLoadItemRef.current) {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useIsDesktopHTTPServerOnline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export async function isDesktopHTTPServerOnline(): Promise<boolean> {
url: `http://localhost:${DESKTOP_HTTP_SERVER_PORT}/ping`,
expectedStatusCode: 200,
method: "GET",
timeout: 5000
timeout: 5000,
expectedBodyText: "pong"
})
: false
}
Expand Down
17 changes: 10 additions & 7 deletions src/hooks/useIsServiceWorkerOnline.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import worker from "@/lib/worker"
import { useQuery } from "@tanstack/react-query"
import { IS_DESKTOP } from "@/constants"

export async function isServiceWorkerOnline(): Promise<boolean> {
return await worker.httpHealthCheck({
url: `${window.origin}/sw/ping`,
expectedStatusCode: 200,
method: "GET",
timeout: 5000,
expectedBodyText: "OK"
})
return IS_DESKTOP
? false
: await worker.httpHealthCheck({
url: `${window.origin}/sw/ping`,
expectedStatusCode: 200,
method: "GET",
timeout: 5000,
expectedBodyText: "OK"
})
}

export default function useIsServiceWorkerOnline(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export async function setup(config?: FilenSDKConfig, connectToSocket: boolean =
await Promise.allSettled([
setItem(sdkConfigLocalStorageKey, initConfig),
setItem(desktopConfigLocalStorageKey, desktopConfig),
registerServiceWorker()
IS_DESKTOP ? Promise.resolve() : registerServiceWorker()
]).catch(console.error)

await Promise.all([worker.initializeSDK(initConfig), IS_DESKTOP ? window.desktopAPI.setConfig(desktopConfig) : Promise.resolve()])
Expand Down
Loading

0 comments on commit 9ec9ffe

Please sign in to comment.