Skip to content

Commit 6d9e2f7

Browse files
committed
useFaviconUrl now computes initial state synchronously
1 parent 09bbcd2 commit 6d9e2f7

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
import { useCallback, useEffect, useRef, useState } from "react";
22
import { extractDomain, faviconUrl } from "~/utils/favicon";
33

4+
function resolve(input: string, size: number): string | null {
5+
const domain = extractDomain(input);
6+
return domain && domain.includes(".") ? faviconUrl(domain, size) : null;
7+
}
8+
49
export function useFaviconUrl(urlInput: string, size: number = 64) {
5-
const [url, setUrl] = useState<string | null>(null);
10+
const [url, setUrl] = useState<string | null>(() => resolve(urlInput, size));
611
const timeoutRef = useRef<ReturnType<typeof setTimeout>>();
712

8-
const update = useCallback(
9-
(value: string) => {
10-
if (timeoutRef.current) clearTimeout(timeoutRef.current);
11-
timeoutRef.current = setTimeout(() => {
12-
const domain = extractDomain(value);
13-
if (domain && domain.includes(".")) {
14-
setUrl(faviconUrl(domain, size));
15-
} else {
16-
setUrl(null);
17-
}
18-
}, 400);
19-
},
20-
[size]
21-
);
22-
2313
useEffect(() => {
24-
update(urlInput);
14+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
15+
timeoutRef.current = setTimeout(() => {
16+
setUrl(resolve(urlInput, size));
17+
}, 400);
2518
return () => {
2619
if (timeoutRef.current) clearTimeout(timeoutRef.current);
2720
};
28-
}, [urlInput, update]);
21+
}, [urlInput, size]);
2922

3023
return url;
3124
}

apps/webapp/app/routes/_app.orgs.$organizationSlug.settings._index/route.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,9 @@ function LogoForm({
442442
const [faviconError, setFaviconError] = useState(false);
443443
const logoFormRef = useRef<HTMLFormElement>(null);
444444
const submit = useSubmit();
445-
const initializedRef = useRef(false);
446445
const prevFaviconRef = useRef(faviconPreview);
447446

448447
useEffect(() => {
449-
if (!initializedRef.current) {
450-
initializedRef.current = true;
451-
prevFaviconRef.current = faviconPreview;
452-
return;
453-
}
454448
if (faviconPreview === prevFaviconRef.current) return;
455449
prevFaviconRef.current = faviconPreview;
456450
if (mode === "logo" && logoFormRef.current) {

0 commit comments

Comments
 (0)