+page.svelte (8710B)
1 <script lang="ts"> 2 import { app_init, db, fs, nostr_keys, notif, radroots, route } from "$lib/utils/app"; 3 import { ls } from "$lib/utils/i18n"; 4 import { parse_file_path } from "@radroots/apps-lib"; 5 import { nostr_pubkey } from "@radroots/apps-nostr"; 6 import { Profile } from "@radroots/apps-lib-pwa"; 7 import type { IViewProfileData } from "@radroots/apps-lib-pwa/types/views/profile"; 8 import { handle_err, throw_err } from "@radroots/utils"; 9 import { onMount } from "svelte"; 10 11 let data: IViewProfileData | undefined = $state(undefined); 12 let loading_photo_upload = $state(false); 13 let loading_photo_upload_open = $state(false); 14 let photo_path = $state(``); 15 16 onMount(async () => { 17 try { 18 // await init(); 19 await app_init(); 20 data = await load_data(); 21 } catch (e) { 22 handle_err(e, `on_mount`); 23 // await route(`/`); 24 } 25 }); 26 27 const init = async (): Promise<void> => { 28 await app_init(); 29 }; 30 31 const load_data = async (): Promise<IViewProfileData | undefined> => { 32 const pubkey_val = $nostr_pubkey; 33 if (!pubkey_val) return undefined; 34 const nostr_profile = await db.nostr_profile_find_one({ 35 on: { 36 public_key: pubkey_val, 37 }, 38 }); 39 if ("err" in nostr_profile) throw_err(nostr_profile); 40 //await nostr_sync.metadata({ metadata: tb_nostr_profile.result }); // no await 41 return { profile: nostr_profile.result }; 42 }; 43 </script> 44 45 <Profile 46 bind:photo_path 47 basis={{ 48 data, 49 loading_photo_upload, 50 loading_photo_upload_open, 51 on_destroy: async () => { 52 try { 53 const pubkey_val = $nostr_pubkey; 54 if (!pubkey_val) return; 55 const tb_nostrprofile = await db.nostr_profile_find_one({ 56 on: { 57 public_key: pubkey_val, 58 }, 59 }); 60 if ("err" in tb_nostrprofile) throw_err(tb_nostrprofile); // @todo 61 /*await nostr_sync.metadata({ 62 metadata: tb_nostrprofile.result, 63 }); // no await */ 64 } catch (e) { 65 handle_err(e, `on_destroy`); 66 } 67 }, 68 on_handle_back: async ({ is_photo_existing }) => { 69 try { 70 const pubkey_val = $nostr_pubkey; 71 if (!photo_path || !pubkey_val) 72 return void (await route(`/`)); 73 const nostr_key = await nostr_keys.read(pubkey_val); 74 if ("err" in nostr_key) throw_err(nostr_key); 75 /*if (photo_path) { 76 const confirm = await notif.confirm({ 77 message: is_photo_existing 78 ? `${$ls( 79 `notification.profile.handle_back_with_selected_photo`, 80 )}` 81 : `${$ls( 82 `notification.profile.handle_back_with_selected_photo_no_existing`, 83 )}`, 84 ok: `${$ls(`common.upload_photo`)}`, 85 cancel: is_photo_existing 86 ? `${$ls(`common.keep_previous`)}` 87 : `${$ls(`common.continue`)}`, 88 }); 89 if (!confirm) return void (await route(`/`)); 90 }*/ 91 92 loading_photo_upload = true; 93 const parsed_photo_path = parse_file_path(photo_path); 94 if (!parsed_photo_path) throw_err("error.upload.no_photo_path"); 95 96 const upload_path = 97 "blob_path" in parsed_photo_path 98 ? parsed_photo_path.blob_path 99 : parsed_photo_path.file_path; 100 const upload_name = 101 "blob_path" in parsed_photo_path 102 ? parsed_photo_path.blob_name 103 : parsed_photo_path.file_name; 104 105 const profile_photo_curr = await db.media_image_find_one({ 106 on: { 107 file_path: upload_name, 108 }, 109 }); 110 if ("err" in profile_photo_curr) throw_err(profile_photo_curr); 111 if ( 112 "result" in profile_photo_curr && 113 profile_photo_curr.result 114 ) { 115 alert("todo, photo already added"); 116 return; 117 //upload_file_path = parse_file_path( 118 // profile_photo_curr.result.file_path, 119 //); 120 } 121 122 const upload_bin = await fs.read_bin(upload_path); 123 if ("err" in upload_bin) throw_err(upload_bin); 124 125 const media_upload = await radroots.media_image_upload({ 126 file_data: upload_bin, 127 secret_key: nostr_key.secret_key, 128 }); 129 console.log( 130 JSON.stringify(media_upload, null, 4), 131 `media_upload`, 132 ); 133 if ("err" in media_upload) throw_err(media_upload); 134 135 const media_image = await db.media_image_create({ 136 file_path: upload_path, 137 res_base: media_upload.base_url, 138 res_path: media_upload.hash, 139 mime_type: media_upload.ext, 140 }); 141 console.log( 142 JSON.stringify(media_image, null, 4), 143 `media_image`, 144 ); 145 146 if ("err" in media_image) throw_err(media_image); 147 148 const tb_nostr_profile_update = await db.nostr_profile_update({ 149 on: { public_key: pubkey_val }, 150 fields: { 151 picture: `${media_upload.base_url}/${media_upload.hash}.${media_upload.ext}`, 152 }, 153 }); 154 console.log( 155 JSON.stringify(tb_nostr_profile_update, null, 4), 156 `tb_nostr_profile_update`, 157 ); 158 if ("err" in tb_nostr_profile_update) 159 throw_err(tb_nostr_profile_update); 160 await route(`/`); 161 162 //let upload_bin: WebFilePath | undefined = undefined; 163 //if ("blob_path" in upload_path) 164 // 165 // else upload_bin = await fs.read_bin(upload_path.file_path); 166 167 /* 168 169 console.log(`photo_path `, photo_path); 170 171 console.log(`photo_path_bin `, photo_path_bin); 172 173 let file_path_upload: FilePath | undefined = undefined; 174 console.log(`upload_file_path `, upload_file_path); 175 176 177 178 console.log( 179 JSON.stringify(profile_photo_curr, null, 4), 180 `profile_photo_curr`, 181 ); 182 183 184 else upload_file_path = parse_file_path(photo_path); 185 if (!upload_file_path) throw_err(`error.util.parse_file_path`); 186 const file_data = await fs.read_bin(upload_file_path.file_path); 187 if ("err" in file_data) throw_err(file_data); 188 189 */ 190 } catch (e) { 191 handle_err(e, `on_handle_back`); 192 } finally { 193 loading_photo_upload = false; 194 } 195 }, 196 on_handle_edit_profile_field: async ({ field }) => { 197 try { 198 if (field === `name`) { 199 const confirm = await notif.confirm({ 200 message: `${$ls( 201 `notification.profile.update_name_confirmation`, 202 )}. ${$ls(`common.do_you_want_to_continue_q`)}`, 203 ok: `${$ls(`common.continue`)}`, 204 cancel: `${$ls(`common.cancel`)}`, 205 }); 206 if (!confirm) return; 207 } 208 const pubkey_val = $nostr_pubkey; 209 if (!pubkey_val) return; 210 await route(`/profile/edit`, [ 211 [`key_nostr`, pubkey_val], 212 [`field`, field], 213 ]); 214 } catch (e) { 215 handle_err(e, `on_handle_edit_profile_field`); 216 } 217 }, 218 on_handle_photo_options: async () => { 219 try { 220 } catch (e) { 221 handle_err(e, `on_handle_photo_options`); 222 } 223 }, 224 }} 225 />