Header.svelte 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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"
  30. ><a
  31. href="/stocks"
  32. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors">Stocks</a
  33. ></span
  34. >
  35. </div>
  36. <!-- Center: navigation -->
  37. <nav>
  38. <ul class="flex gap-6 text-sm font-medium text-gray-700 dark:text-gray-300">
  39. {#if username}
  40. <li aria-current={$page.url.pathname === '/stocks' ? 'page' : undefined}>
  41. <a
  42. href="/stocks"
  43. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors">Stocks</a
  44. >
  45. </li>
  46. <li aria-current={$page.url.pathname === '/portfolio' ? 'page' : undefined}>
  47. <a
  48. href="/portfolio"
  49. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors"
  50. >Portfolio</a
  51. >
  52. </li>
  53. <li aria-current={$page.url.pathname === '/insights' ? 'page' : undefined}>
  54. <a
  55. href="/insights"
  56. class="hover:text-theme-light dark:hover:text-theme-dark transition-colors"
  57. >Insights</a
  58. >
  59. </li>
  60. {/if}
  61. </ul>
  62. </nav>
  63. <!-- Right: user profile -->
  64. <div class="flex items-center gap-3">
  65. <a href="/profile" class="flex items-center gap-2 group">
  66. <span
  67. class="text-sm text-gray-600 dark:text-gray-300 group-hover:text-theme-light dark:group-hover:text-theme-dark"
  68. >
  69. {username}
  70. </span>
  71. <img
  72. src={profile}
  73. alt="Profile"
  74. class="w-6 h-6 rounded-full border border-gray-300 dark:border-gray-700"
  75. title={profileTitle}
  76. />
  77. </a>
  78. </div>
  79. </div>
  80. </header>