all repos

onasty @ d2c87a8

a one-time notes service

onasty/web/src/Pages/Secret/Slug_.elm(view raw)

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
290
291
292
293
294
295
296
module Pages.Secret.Slug_ exposing (Model, Msg, PageVariant, page)

import Api
import Api.Note
import Components.Box
import Components.Form
import Components.Icon
import Components.Utils
import Data.Note exposing (Metadata, Note)
import Effect exposing (Effect)
import Html as H exposing (Html)
import Html.Attributes as A
import Html.Events as E
import Layouts
import Page exposing (Page)
import Route exposing (Route)
import Shared
import Time exposing (Zone)
import Time.Format as T
import View exposing (View)


page : Shared.Model -> Route { slug : String } -> Page Model Msg
page shared route =
    Page.new
        { init = init route.params.slug
        , update = update
        , subscriptions = subscriptions
        , view = view shared
        }
        |> Page.withLayout (\_ -> Layouts.Header {})



-- INIT


type PageVariant
    = RequestNote
    | ShowNote (Api.Response Note)
    | NotFound


type alias Model =
    { page : PageVariant
    , metadata : Api.Response Metadata
    , slug : String
    , password : Maybe String
    }


init : String -> () -> ( Model, Effect Msg )
init slug () =
    ( { page = RequestNote
      , metadata = Api.Loading
      , slug = slug
      , password = Nothing
      }
    , Api.Note.getMetadata
        { onResponse = ApiGetMetadataResponded
        , slug = slug
        }
    )



-- UPDATE


type Msg
    = UserClickedViewNote
    | UserClickedCopyContent
    | UserUpdatedPassword String
    | ApiGetNoteResponded (Result Api.Error Note)
    | ApiGetMetadataResponded (Result Api.Error Metadata)


update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
    case msg of
        UserClickedViewNote ->
            ( { model | page = ShowNote Api.Loading }
            , Api.Note.get
                { onResponse = ApiGetNoteResponded
                , password = model.password
                , slug = model.slug
                }
            )

        UserClickedCopyContent ->
            case model.page of
                ShowNote (Api.Success note) ->
                    ( model, Effect.sendToClipboard note.content )

                _ ->
                    ( model, Effect.none )

        UserUpdatedPassword password ->
            ( { model | password = Just password }, Effect.none )

        ApiGetNoteResponded (Ok note) ->
            ( { model | page = ShowNote (Api.Success note) }, Effect.none )

        ApiGetNoteResponded (Err error) ->
            ( { model | page = ShowNote (Api.Failure error) }, Effect.none )

        ApiGetMetadataResponded (Ok metadata) ->
            ( { model | metadata = Api.Success metadata }, Effect.none )

        ApiGetMetadataResponded (Err error) ->
            ( { model | page = NotFound, metadata = Api.Failure error }, Effect.none )


subscriptions : Model -> Sub Msg
subscriptions _ =
    Sub.none



-- VIEW


view : Shared.Model -> Model -> View Msg
view shared model =
    { title = "View note"
    , body =
        [ Components.Utils.commonContainer
            (case model.metadata of
                Api.Success metadata ->
                    viewPage shared.timeZone model.slug model.page metadata model.password

                Api.Loading ->
                    [ viewHeader { title = "View note", subtitle = "Loading note metadata..." }
                    , viewOpenNote { slug = model.slug, hasPassword = False, password = Nothing, isLoading = True }
                    ]

                Api.Failure error ->
                    [ viewHeader { title = "Note Not Found", subtitle = "The note you're looking for doesn't exist or has expired" }
                    , if Api.is404 error then
                        viewNoteNotFound

                      else
                        Components.Box.error (Api.errorMessage error)
                    ]
            )
        ]
    }


viewPage : Zone -> String -> PageVariant -> Metadata -> Maybe String -> List (Html Msg)
viewPage zone slug variant metadata password =
    case variant of
        RequestNote ->
            [ viewHeader { title = "View note", subtitle = "Click the button below to view the note content" }
            , viewOpenNote { slug = slug, hasPassword = metadata.hasPassword, password = password, isLoading = False }
            ]

        ShowNote apiResp ->
            case apiResp of
                Api.Success note ->
                    [ viewShowNoteHeader zone slug note
                    , viewNoteContent note
                    ]

                Api.Loading ->
                    [ viewHeader { title = "View note", subtitle = "Click the button below to view the note content" }
                    , viewOpenNote { slug = slug, hasPassword = metadata.hasPassword, password = password, isLoading = True }
                    ]

                Api.Failure _ ->
                    [ viewHeader { title = "Note Not Found", subtitle = "The note you're looking for doesn't exist or has expired" }
                    , viewNoteNotFound
                    ]

        NotFound ->
            [ viewNoteNotFound ]



-- HEADER


viewHeader : { title : String, subtitle : String } -> Html msg
viewHeader options =
    H.div [ A.class "p-6 pb-4 border-b border-gray-200" ]
        [ H.h1
            [ A.class "text-2xl font-bold text-gray-900" ]
            [ H.text options.title ]
        , H.p [ A.class "text-gray-600 mt-2" ] [ H.text options.subtitle ]
        ]


viewShowNoteHeader : Zone -> String -> Note -> Html Msg
viewShowNoteHeader zone slug note =
    H.div []
        [ Components.Utils.viewIf note.burnBeforeExpiration
            (H.div [ A.class "bg-orange-50 border-b border-orange-200 p-4" ]
                [ H.div [ A.class "flex items-center gap-3" ]
                    [ H.div [ A.class "w-6 h-6 bg-orange-100 rounded-full flex items-center justify-center flex-shrink-0" ]
                        [ Components.Icon.view Components.Icon.Warning "w-4 h-4 text-orange-600" ]
                    , H.p [ A.class "text-orange-800 text-sm font-medium" ]
                        [ H.text "This note was destroyed. If you need to keep it, copy it before closing this window." ]
                    ]
                ]
            )
        , H.div [ A.class "p-6 pb-4 border-b border-gray-200" ]
            [ H.div [ A.class "flex justify-between items-start" ]
                [ H.div []
                    [ H.h1 [ A.class "text-2xl font-bold text-gray-900" ] [ H.text ("Note: " ++ slug) ]
                    , H.div [ A.class "text-sm text-gray-500 mt-2 space-y-1" ]
                        [ H.p [] [ H.text ("Created: " ++ T.toString zone note.createdAt) ]
                        , Components.Utils.viewMaybe note.expiresAt (\n -> H.p [] [ H.text ("Expires at: " ++ T.toString zone n) ])
                        ]
                    ]
                , H.div [ A.class "flex gap-2" ]
                    [ Components.Form.button
                        { text = "Copy Content"
                        , style = Components.Form.SecondaryDisabled False
                        , onClick = UserClickedCopyContent
                        , disabled = False
                        }
                    ]
                ]
            ]
        ]



-- NOTE


viewNoteNotFound : Html msg
viewNoteNotFound =
    H.div [ A.class "p-6" ]
        [ H.div [ A.class "text-center py-12" ]
            [ H.div [ A.class "w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mx-auto mb-4" ]
                [ Components.Icon.view Components.Icon.NotFound "w-8 h-8 text-red-500" ]
            , H.h2 [ A.class "text-xl font-semibold text-gray-900 mb-2" ]
                [ H.text "Note not found" ]
            ]
        ]


viewOpenNote : { slug : String, hasPassword : Bool, isLoading : Bool, password : Maybe String } -> Html Msg
viewOpenNote opts =
    let
        isDisabled =
            (opts.hasPassword && Maybe.withDefault "" opts.password == "") || opts.isLoading
    in
    H.div [ A.class "p-6" ]
        [ H.div [ A.class "text-center py-12" ]
            [ H.div [ A.class "mb-6" ]
                [ H.div [ A.class "w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4" ]
                    [ Components.Icon.view Components.Icon.NoteIcon "w-8 h-8 text-gray-400" ]
                , H.h2 [ A.class "text-lg font-semibold text-gray-900 mb-2" ] [ H.text opts.slug ]
                , H.p [ A.class "text-gray-600 mb-6" ] [ H.text "You're about read and destroy the note." ]
                ]
            , H.form
                [ E.onSubmit UserClickedViewNote
                , A.class "max-w-sm mx-auto space-y-4"
                ]
                [ Components.Utils.viewIf opts.hasPassword
                    (H.div [ A.class "space-y-2" ]
                        [ H.label [ A.class "block text-sm font-medium text-gray-700 text-left" ] [ H.text "Password" ]
                        , H.input
                            [ E.onInput UserUpdatedPassword
                            , A.class "w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-black focus:border-transparent"
                            ]
                            []
                        ]
                    )
                , Components.Form.submitButton
                    { text =
                        if opts.isLoading then
                            "Loading Note..."

                        else
                            "View Note"
                    , style = Components.Form.Primary isDisabled
                    , disabled = isDisabled
                    , class = "py-3"
                    }
                ]
            ]
        ]


viewNoteContent : Note -> Html msg
viewNoteContent note =
    H.div [ A.class "p-6" ]
        [ H.div [ A.class "bg-gray-50 border border-gray-200 rounded-md p-4" ]
            [ H.pre
                [ A.class "whitespace-pre-wrap font-mono text-sm text-gray-800 overflow-x-auto" ]
                [ H.text note.content ]
            ]
        ]