aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Announcement.svelte
blob: 4fbe9de0e679ebceb520f7db5d73c2a7707a25ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<script lang="ts">
  import Spacer from '$lib/Layout/Spacer.svelte';
  import Popup from './Layout/Popup.svelte';
  import announcementHash from '$stores/announcementHash';
  import { env } from '$env/dynamic/public';
  import identity from '$stores/identity';

  const announcement = env.PUBLIC_ANNOUNCEMENT;
  const dismissButton = env.PUBLIC_ANNOUNCEMENT_DISMISS;
  const loggedIn = $identity !== undefined && $identity.id !== -2;

  const hash = (s: string) =>
    s
      .split('')
      .reduce((previous, current) => ((previous << 5) - previous + current.charCodeAt(0)) | 0, 0);

  const dismiss = () => {
    if (announcement) announcementHash.set(hash(announcement));
  };

  const maxWidth = (input: string, max = 100) => {
    let output = '';
    let line = '';

    for (const word of input.split(' ')) {
      if (line.length + word.length > max) {
        output += line + '\n';
        line = '';
      }

      line += word + ' ';
    }

    return output + line;
  };
</script>

{#if loggedIn && announcement && $announcementHash !== hash(announcement) && $announcementHash !== 0}
  <Popup fullscreen onLeave={dismiss}>
    {#each maxWidth(announcement).split('\n') as line}
      {line}<br />
    {/each}

    <Spacer />

    <button onclick={dismiss} class="dismiss">{dismissButton || 'Dismiss'}</button>
  </Popup>
{/if}

<style>
  .dismiss {
    float: right;
  }
</style>