@@ -25,6 +25,49 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
2525
2626const MAX_BLUR = 30 ;
2727
28+ async function resolveWallpaperImage ( source : string | File ) : Promise < string > {
29+ let nextImage : string ;
30+ if ( typeof source === "string" ) {
31+ const parsed = new URL ( source . trim ( ) ) ;
32+ if ( ! [ "https:" , "http:" ] . includes ( parsed . protocol ) ) {
33+ throw new Error ( "Use an http or https image URL, or choose a local file." ) ;
34+ }
35+ nextImage = parsed . href ;
36+ } else {
37+ const result = await compressImageForWallpaper ( source ) ;
38+ if ( ! result . ok ) {
39+ throw new Error (
40+ result . reason === "too-large"
41+ ? "Choose an image no larger than 50 MB and 64 megapixels."
42+ : "Could not read this image. Choose a PNG, JPEG, GIF, or WebP file." ,
43+ ) ;
44+ }
45+ nextImage = result . image . dataUrl ;
46+ }
47+ await new Promise < void > ( ( resolve , reject ) => {
48+ const probe = new Image ( ) ;
49+ const timeout = window . setTimeout ( ( ) => finish ( false ) , 15000 ) ;
50+ function finish ( loaded : boolean ) {
51+ window . clearTimeout ( timeout ) ;
52+ probe . removeEventListener ( "load" , onLoad ) ;
53+ probe . removeEventListener ( "error" , onError ) ;
54+ if ( loaded ) resolve ( ) ;
55+ else reject ( new Error ( "Could not load this image. Check the URL or choose another file." ) ) ;
56+ }
57+ const onLoad = ( ) => finish ( true ) ;
58+ const onError = ( ) => finish ( false ) ;
59+ probe . addEventListener ( "load" , onLoad ) ;
60+ probe . addEventListener ( "error" , onError ) ;
61+ probe . src = nextImage ;
62+ } ) ;
63+ return nextImage ;
64+ }
65+
66+ function wallpaperErrorMessage ( cause : unknown ) : string {
67+ if ( cause instanceof TypeError ) return "Enter a full image URL, or choose a local file." ;
68+ return cause instanceof Error ? cause . message : "Could not load this image." ;
69+ }
70+
2871export function TimelineBackgroundSettings ( ) {
2972 const image = useClientSettings ( ( settings ) => settings . timelineBackgroundImage ) ;
3073 const opacity = useClientSettings ( ( settings ) => settings . timelineBackgroundOpacity ) ;
@@ -71,6 +114,7 @@ export function TimelineBackgroundSettings() {
71114 if ( value !== shadowBlur ) updateSettings ( { timelineTextShadowBlur : value } ) ;
72115 }
73116
117+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- invalidates an in-flight image import on unmount so it cannot write settings afterwards
74118 useEffect (
75119 ( ) => ( ) => {
76120 request . current ++ ;
@@ -82,68 +126,28 @@ export function TimelineBackgroundSettings() {
82126 const generation = ++ request . current ;
83127 setBusy ( true ) ;
84128 setError ( "" ) ;
85- try {
86- let nextImage : string ;
87- if ( typeof source === "string" ) {
88- const parsed = new URL ( source . trim ( ) ) ;
89- if ( ! [ "https:" , "http:" ] . includes ( parsed . protocol ) ) {
90- throw new Error ( "Use an http or https image URL, or choose a local file." ) ;
91- }
92- nextImage = parsed . href ;
93- } else {
94- const result = await compressImageForWallpaper ( source ) ;
95- if ( ! result . ok ) {
96- throw new Error (
97- result . reason === "too-large"
98- ? "Choose an image no larger than 50 MB and 64 megapixels."
99- : "Could not read this image. Choose a PNG, JPEG, GIF, or WebP file." ,
100- ) ;
101- }
102- nextImage = result . image . dataUrl ;
103- }
104- if ( generation !== request . current ) return ;
105- await new Promise < void > ( ( resolve , reject ) => {
106- const probe = new Image ( ) ;
107- const timeout = window . setTimeout ( ( ) => finish ( false ) , 15000 ) ;
108- function finish ( loaded : boolean ) {
109- window . clearTimeout ( timeout ) ;
110- probe . removeEventListener ( "load" , onLoad ) ;
111- probe . removeEventListener ( "error" , onError ) ;
112- if ( loaded ) resolve ( ) ;
113- else
114- reject ( new Error ( "Could not load this image. Check the URL or choose another file." ) ) ;
115- }
116- const onLoad = ( ) => finish ( true ) ;
117- const onError = ( ) => finish ( false ) ;
118- probe . addEventListener ( "load" , onLoad ) ;
119- probe . addEventListener ( "error" , onError ) ;
120- probe . src = nextImage ;
121- } ) ;
122- if ( generation !== request . current ) return ;
123- try {
124- await persistClientSettingsUpdate ( ( current ) =>
125- generation === request . current
126- ? { ...current , timelineBackgroundImage : nextImage }
127- : current ,
128- ) ;
129- } catch {
130- throw new Error (
131- "Could not save this image. Client storage may be full. Try a smaller image." ,
132- ) ;
133- }
134- } catch ( cause ) {
135- if ( generation === request . current ) {
136- setError (
137- cause instanceof TypeError
138- ? "Enter a full image URL, or choose a local file."
139- : cause instanceof Error
140- ? cause . message
141- : "Could not load this image." ,
142- ) ;
143- }
144- } finally {
145- if ( generation === request . current ) setBusy ( false ) ;
129+ const resolved = await resolveWallpaperImage ( source ) . then (
130+ ( nextImage ) => ( { nextImage, error : null } ) ,
131+ ( cause : unknown ) => ( { nextImage : null , error : wallpaperErrorMessage ( cause ) } ) ,
132+ ) ;
133+ if ( generation !== request . current ) return ;
134+ const { nextImage } = resolved ;
135+ if ( nextImage === null ) {
136+ setError ( resolved . error ) ;
137+ setBusy ( false ) ;
138+ return ;
146139 }
140+ const saved = await persistClientSettingsUpdate ( ( current ) =>
141+ generation === request . current ? { ...current , timelineBackgroundImage : nextImage } : current ,
142+ ) . then (
143+ ( ) => true ,
144+ ( ) => false ,
145+ ) ;
146+ if ( generation !== request . current ) return ;
147+ if ( ! saved ) {
148+ setError ( "Could not save this image. Client storage may be full. Try a smaller image." ) ;
149+ }
150+ setBusy ( false ) ;
147151 }
148152
149153 function removeImage ( ) {
0 commit comments