Header.svelte 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <script>
  2. import { page } from '$app/stores';
  3. import profile from '$lib/images/profile.png';
  4. import { authentication } from '../routes/store.js';
  5. import { onMount } from 'svelte';
  6. let username = '';
  7. let profileTitle = 'Login';
  8. const unsubscribe = authentication.subscribe((value) => {
  9. username = value?.username || '';
  10. profileTitle = username ? `You are logged in as ${username}` : 'Login';
  11. });
  12. onMount(() => {
  13. const storedAuth = localStorage.getItem('authentication');
  14. if (storedAuth) {
  15. try {
  16. authentication.set(JSON.parse(storedAuth));
  17. } catch (e) {
  18. console.error('Failed to parse auth from localStorage', e);
  19. }
  20. }
  21. return () => unsubscribe();
  22. });
  23. </script>
  24. <header class="bg-white dark:bg-gray-900 shadow-md">
  25. <div class="max-w-7xl mx-auto flex items-center justify-between px-4 py-3">
  26. <!-- Left: logo / placeholder -->
  27. <div class="flex items-center">
  28. <!-- Insert logo or branding here -->
  29. <span class="text-lg font-bold text-theme-light dark:text-theme-dark"><a
  30. href="/stocks"
  31. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors">Stocks</a
  32. ></span>
  33. </div>
  34. <!-- Center: navigation -->
  35. <nav>
  36. <ul class="flex gap-6 text-sm font-medium text-gray-700 dark:text-gray-300">
  37. {#if username}
  38. <li aria-current={$page.url.pathname === '/stocks' ? 'page' : undefined}>
  39. <a
  40. href="/stocks"
  41. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors">Stocks</a
  42. >
  43. </li>
  44. <li aria-current={$page.url.pathname === '/portfolio' ? 'page' : undefined}>
  45. <a
  46. href="/portfolio"
  47. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors"
  48. >Portfolio</a
  49. >
  50. </li>
  51. <li aria-current={$page.url.pathname === '/insights' ? 'page' : undefined}>
  52. <a
  53. href="/insights"
  54. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors"
  55. >Insights</a
  56. >
  57. </li>
  58. {/if}
  59. </ul>
  60. </nav>
  61. <!-- Right: user profile -->
  62. <div class="flex items-center gap-3">
  63. <a href="/profile" class="flex items-center gap-2 group">
  64. <span
  65. class="text-sm text-gray-600 dark:text-gray-300 group-hover:text-theme-light dark:group-hover:text-theme-dark"
  66. >
  67. {username}
  68. </span>
  69. <img
  70. src={profile}
  71. alt="Profile"
  72. class="w-6 h-6 rounded-full border border-gray-300 dark:border-gray-700"
  73. title={profileTitle}
  74. />
  75. </a>
  76. </div>
  77. </div>
  78. </header>