File size: 9,201 Bytes
8da2481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
defmodule PlausibleWeb.Team.Notice do
  @moduledoc """
  Components with teams related notices.
  """
  use PlausibleWeb, :component
  import PlausibleWeb.Components.Icons

  alias Plausible.Teams
  alias PlausibleWeb.Components.PrimaDropdown

  def owner_cta_banner(assigns) do
    ~H"""
    <aside class="mt-4 mb-4">
      <.notice
        title="A Better Way of Inviting People to Your Team"
        class="shadow-md dark:shadow-none mt-4"
      >
        <p>
          You can also create a team and assign different roles to team members, such as admin,
          editor, viewer or billing. Team members will gain access to all your sites. <.styled_link href={
            Routes.team_setup_path(PlausibleWeb.Endpoint, :setup)
          }>
            Create your team here
          </.styled_link>.
        </p>
      </.notice>
    </aside>
    """
  end

  def guest_cta_banner(assigns) do
    ~H"""
    <aside class="mt-4 mb-4">
      <.notice
        title="A Better Way of Inviting People to a Team"
        class="shadow-md dark:shadow-none mt-4"
      >
        <p>
          It is also possible to create a team and assign different roles to team members, such as
          admin, editor, viewer or billing. Team members can gain access to all the sites. Please
          contact the site owner to create your team.
        </p>
      </.notice>
    </aside>
    """
  end

  def team_members_notice(assigns) do
    ~H"""
    <aside class="mt-4 mb-4">
      <.notice theme={:gray} class="mt-4">
        <p>
          Team members automatically have access to this site.
          <.styled_link href={Routes.settings_path(PlausibleWeb.Endpoint, :team_general)}>
            View team members
          </.styled_link>
        </p>
      </.notice>
    </aside>
    """
  end

  def team_invitations(assigns) do
    ~H"""
    <aside :if={not Enum.empty?(@team_invitations)} class="flex flex-col gap-y-4">
      <.notice
        :for={i <- @team_invitations}
        id={"invitation-#{i.invitation_id}"}
        title="Team invitation"
        theme={:white}
      >
        <:icon>
          <div class="shrink-0 -mt-1 bg-green-100/80 dark:bg-green-900/30 rounded-lg p-1.5">
            <.envelope_icon class="size-4 text-green-600 dark:text-green-400" />
          </div>
        </:icon>
        {i.inviter.name} has invited you to join the "{i.team.name}" as {i.role}.
        <:actions>
          <.button_link
            method="post"
            href={Routes.invitation_path(PlausibleWeb.Endpoint, :reject_invitation, i.invitation_id)}
            phx-value-invitation-id={i.invitation_id}
            theme="ghost"
            size="sm"
            class="order-2 md:order-1"
            mt?={false}
          >
            Reject
          </.button_link>
          <.button_link
            method="post"
            href={Routes.invitation_path(PlausibleWeb.Endpoint, :accept_invitation, i.invitation_id)}
            theme="secondary"
            size="sm"
            class="order-1 md:order-2"
            mt?={false}
          >
            Accept
          </.button_link>
        </:actions>
      </.notice>
    </aside>
    """
  end

  def site_ownership_invitations(assigns) do
    ~H"""
    <aside :if={not Enum.empty?(@site_ownership_invitations)} class="flex flex-col gap-y-4">
      <.site_ownership_invitation
        :for={i <- @site_ownership_invitations}
        invitation={i}
        current_team={@current_team}
      />
    </aside>
    """
  end

  defp site_ownership_invitation(assigns) do
    {can_accept?, can_accept_without_members?, exceeded_limits} =
      case assigns.invitation.ownership_check do
        :ok ->
          {true, false, nil}

        {:error, {:over_plan_limits, limits}} ->
          {false, limits == [:team_member_limit], PlausibleWeb.TextHelpers.pretty_list(limits)}

        _ ->
          {false, false, nil}
      end

    assigns =
      assign(assigns,
        can_accept?: can_accept?,
        can_accept_without_members?: can_accept_without_members?,
        exceeded_limits: exceeded_limits
      )

    ~H"""
    <.notice
      id={"site-ownership-invitation-#{@invitation.transfer_id}"}
      title={"#{@invitation.initiator.name} has invited you to own #{@invitation.site.domain}"}
      theme={:white}
      icon_class="hidden md:block"
    >
      <:icon>
        <div class="shrink-0 -mt-1 bg-green-100/80 dark:bg-green-900/30 rounded-lg p-1.5">
          <.envelope_icon class="size-4 text-green-600 dark:text-green-400" />
        </div>
      </:icon>
      <p :if={@can_accept?}>
        On acceptance, you'll be responsible for billing and this site will join "{Teams.name(
          @current_team
        )}"
      </p>
      <p :if={@invitation.ownership_check == {:error, :no_plan}} class="text-sm font-medium">
        You don't have an active subscription. Upgrade to accept ownership and take over billing.
      </p>
      <p :if={@exceeded_limits} class="mt-1 text-sm font-medium">
        This exceeds your current {@exceeded_limits} limits. Upgrade to accept ownership.
      </p>
      <:actions>
        <.button_link
          method="post"
          href={
            Routes.invitation_path(
              PlausibleWeb.Endpoint,
              :reject_invitation,
              @invitation.transfer_id
            )
          }
          theme="ghost"
          size="sm"
          class="order-3 md:order-1"
          mt?={false}
        >
          Reject
        </.button_link>
        <.button_link
          :if={@can_accept?}
          method="post"
          href={
            Routes.invitation_path(
              PlausibleWeb.Endpoint,
              :accept_invitation,
              @invitation.transfer_id
            )
          }
          theme="secondary"
          size="sm"
          class="order-1 md:order-2"
          mt?={false}
        >
          Accept
        </.button_link>
        <PrimaDropdown.dropdown
          :if={@can_accept_without_members?}
          id={"ownership-accept-#{@invitation.transfer_id}"}
          class="order-1 md:order-2"
        >
          <PrimaDropdown.dropdown_trigger
            id={"ownership-accept-trigger-#{@invitation.transfer_id}"}
            theme="secondary"
            size="sm"
          >
            Accept <Heroicons.chevron_down mini class="size-4 mt-0.5" />
          </PrimaDropdown.dropdown_trigger>
          <PrimaDropdown.dropdown_menu id={"ownership-accept-menu-#{@invitation.transfer_id}"}>
            <PrimaDropdown.dropdown_item
              as={&link/1}
              id={"ownership-accept-item-upgrade-#{@invitation.transfer_id}"}
              href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)}
            >
              Upgrade to accept
            </PrimaDropdown.dropdown_item>
            <PrimaDropdown.dropdown_item
              as={fn a -> link(Map.put(a, :method, "post")) end}
              id={"ownership-accept-item-members-#{@invitation.transfer_id}"}
              href={
                Routes.invitation_path(
                  PlausibleWeb.Endpoint,
                  :accept_invitation,
                  @invitation.transfer_id,
                  skip_site_members_transfer: "true"
                )
              }
            >
              Accept without members
            </PrimaDropdown.dropdown_item>
          </PrimaDropdown.dropdown_menu>
        </PrimaDropdown.dropdown>
        <.button_link
          :if={not @can_accept? and not @can_accept_without_members?}
          href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)}
          theme="secondary"
          size="sm"
          class="order-1 md:order-3"
          mt?={false}
        >
          Upgrade to accept
        </.button_link>
      </:actions>
    </.notice>
    """
  end

  def site_invitations(assigns) do
    ~H"""
    <aside :if={not Enum.empty?(@site_invitations)} class="flex flex-col gap-y-4">
      <.notice
        :for={i <- @site_invitations}
        id={"site-invitation-#{i.invitation_id}"}
        title={"Invitation to #{i.site.domain}"}
        theme={:white}
      >
        <:icon>
          <div class="shrink-0 -mt-1 bg-green-100/80 dark:bg-green-900/30 rounded-lg p-1.5">
            <.envelope_icon class="size-4 text-green-600 dark:text-green-400" />
          </div>
        </:icon>
        {i.team_invitation.inviter.name} has invited you to join the {i.site.domain} analytics
        dashboard as a {i.role}.
        <:actions>
          <.button_link
            method="post"
            href={Routes.invitation_path(PlausibleWeb.Endpoint, :reject_invitation, i.invitation_id)}
            theme="ghost"
            size="sm"
            class="order-2 md:order-1"
            mt?={false}
          >
            Reject
          </.button_link>
          <.button_link
            method="post"
            href={Routes.invitation_path(PlausibleWeb.Endpoint, :accept_invitation, i.invitation_id)}
            theme="secondary"
            size="sm"
            class="order-1 md:order-2"
            mt?={false}
          >
            Accept
          </.button_link>
        </:actions>
      </.notice>
    </aside>
    """
  end
end