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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
module Pages.Secret.Slug_ exposing (Model, Msg, PageVariant, page)
import Api
import Api.Note
import Components.Error
import Components.Note
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
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none
-- VIEW
view : Shared.Model -> Model -> View Msg
view shared model =
{ title = "View note"
, body =
[ H.div
[ A.class "w-full max-w-4xl mx-auto" ]
[ H.div
[ A.class "bg-white rounded-lg border border-gray-200 shadow-sm" ]
(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 model.slug
else
Components.Error.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 slug
]
NotFound ->
[ viewNoteNotFound slug ]
-- 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 []
[ if note.burnBeforeExpiration then
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.Note.warningSvg ]
, 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." ]
]
]
else
H.text ""
, 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) ]
, case note.expiresAt of
Just expiresAt ->
H.p [] [ H.text ("Expires at: " ++ T.toString zone expiresAt) ]
Nothing ->
H.text ""
]
]
, H.div [ A.class "flex gap-2" ]
[ H.button
[ E.onClick UserClickedCopyContent
, A.class "px-3 py-2 text-sm border border-gray-300 text-gray-700 rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-black focus:ring-offset-2 transition-colors"
]
[ H.text "Copy Content" ]
]
]
]
]
-- NOTE
viewNoteNotFound : String -> Html msg
viewNoteNotFound slug =
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.Note.noteNotFoundSvg ]
, H.h2 [ A.class "text-xl font-semibold text-gray-900 mb-2" ]
[ H.text ("Note " ++ slug ++ " Not Found") ]
, H.div [ A.class "text-gray-600 mb-6 space-y-2" ]
[ H.p []
[ H.span [ A.class "font-bold" ] [ H.text "This note may have:" ]
, H.ul [ A.class "text-sm space-y-1 list-disc list-inside text-left max-w-md mx-auto" ]
[ H.li [] [ H.text "Expired and been deleted" ]
, H.li [] [ H.text "Have different password" ]
, H.li [] [ H.text "Been deleted by the creator" ]
, H.li [] [ H.text "Been burned after reading" ]
, H.li [] [ H.text "Never existed or the URL is incorrect" ]
]
]
]
]
]
viewOpenNote :
{ slug : String
, hasPassword : Bool
, isLoading : Bool
, password : Maybe String
}
-> Html Msg
viewOpenNote opts =
let
isDisabled =
opts.hasPassword && Maybe.withDefault "" opts.password == ""
buttonData =
let
base =
"px-6 py-3 rounded-md focus:outline-none focus:ring-2 focus:ring-black focus:ring-offset-2 transition-colors"
in
if opts.isLoading then
{ text = "Loading Note...", class = base ++ " bg-gray-300 text-gray-500 cursor-not-allowed" }
else if isDisabled then
{ text = "View Note", class = base ++ " bg-gray-300 text-gray-500 cursor-not-allowed" }
else
{ text = "View Note", class = base ++ " bg-black text-white hover:bg-gray-800" }
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.Note.noteIconSvg ]
, 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"
]
[ if opts.hasPassword then
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"
]
[]
]
else
H.text ""
, H.button
[ A.class buttonData.class
, A.type_ "submit"
, A.disabled isDisabled
]
[ H.text buttonData.text ]
]
]
]
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 ]
]
]
|